public object Parse(System.Xml.XmlReader s, DatatypeFormatterParseResult result) { // Parse base (ANY) from the stream ANYFormatter baseFormatter = new ANYFormatter(); string pathName = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name; // Parse ED ED retVal = baseFormatter.Parse<ED>(s, result); // Now parse our data out... Attributes if (s.GetAttribute("representation") != null) retVal.Representation = (EncapsulatedDataRepresentation)Util.FromWireFormat(s.GetAttribute("representation"), typeof(EncapsulatedDataRepresentation)); if (s.GetAttribute("mediaType") != null) retVal.MediaType = s.GetAttribute("mediaType"); if (s.GetAttribute("language") != null) retVal.Language = s.GetAttribute("language"); if (s.GetAttribute("compression") != null) retVal.Compression = (EncapsulatedDataCompression?)Util.FromWireFormat(s.GetAttribute("compression"), typeof(EncapsulatedDataCompression)); if (s.GetAttribute("integrityCheckAlgorithm") != null) { switch(s.GetAttribute("integrityCheckAlgorithm")) { case "SHA-1": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; break; case "SHA-256": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA256; break; } } if (s.GetAttribute("integrityCheck") != null) retVal.IntegrityCheck = Convert.FromBase64String(s.GetAttribute("integrityCheck")); // Elements and inner data #region Elements string innerData = ""; if (!s.IsEmptyElement) { // Exit markers int sDepth = s.Depth; string sName = s.Name; s.Read(); // Read until exit condition is fulfilled while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName)) { string oldName = s.Name; // Name try { if (s.LocalName == "thumbnail") // Format using ED { EDFormatter edFormatter = new EDFormatter(); edFormatter.Host = this.Host; retVal.Thumbnail = (ED)edFormatter.Parse(s, result); // Parse ED } else if (s.LocalName == "reference") // Format using TEL { TELFormatter telFormatter = new TELFormatter(); telFormatter.Host = this.Host; retVal.Reference = (TEL)telFormatter.Parse(s, result); } else if (s.NodeType == System.Xml.XmlNodeType.Text || s.NodeType == System.Xml.XmlNodeType.CDATA) innerData += s.Value; else if (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName) && (s.NodeType == System.Xml.XmlNodeType.Element || s.NodeType == System.Xml.XmlNodeType.EndElement)) { retVal.Representation = EncapsulatedDataRepresentation.XML; innerData += s.ReadOuterXml(); } } catch (MessageValidationException e) { result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, s.ToString(), e)); } finally { if (s.Name == oldName) s.Read(); } } } #endregion Encoding textEncoding = System.Text.Encoding.UTF8; // Parse the innerData string into something meaningful if(innerData.Length > 0) if (retVal.Representation == EncapsulatedDataRepresentation.B64) retVal.Data = Convert.FromBase64String(innerData); else retVal.Data = textEncoding.GetBytes(innerData); // Finally, the hash, this will validate the data if(!retVal.ValidateIntegrityCheck()) result.AddResultDetail(new ResultDetail(ResultDetailType.Warning, string.Format("Encapsulated data with content starting with '{0}' failed integrity check!", retVal.ToString().PadRight(10, ' ').Substring(0, 10)), s.ToString(), null)); // Validate baseFormatter.Validate(retVal, pathName, result); return retVal; }
public object Parse(System.Xml.XmlReader s, DatatypeFormatterParseResult result) { // Parse base (ANY) from the stream ANYFormatter baseFormatter = new ANYFormatter(); string pathName = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name; // Parse ED ED retVal = baseFormatter.Parse <ED>(s, result); // Now parse our data out... Attributes if (s.GetAttribute("representation") != null) { retVal.Representation = (EncapsulatedDataRepresentation)Util.FromWireFormat(s.GetAttribute("representation"), typeof(EncapsulatedDataRepresentation)); } if (s.GetAttribute("mediaType") != null) { retVal.MediaType = s.GetAttribute("mediaType"); } if (s.GetAttribute("language") != null) { retVal.Language = s.GetAttribute("language"); } if (s.GetAttribute("compression") != null) { retVal.Compression = (EncapsulatedDataCompression?)Util.FromWireFormat(s.GetAttribute("compression"), typeof(EncapsulatedDataCompression)); } if (s.GetAttribute("integrityCheckAlgorithm") != null) { switch (s.GetAttribute("integrityCheckAlgorithm")) { case "SHA-1": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; break; case "SHA-256": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA256; break; } } if (s.GetAttribute("integrityCheck") != null) { retVal.IntegrityCheck = Convert.FromBase64String(s.GetAttribute("integrityCheck")); } // Elements and inner data #region Elements string innerData = ""; if (!s.IsEmptyElement) { // Exit markers int sDepth = s.Depth; string sName = s.Name; s.Read(); // Read until exit condition is fulfilled while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName)) { string oldName = s.Name; // Name try { if (s.LocalName == "thumbnail") // Format using ED { EDFormatter edFormatter = new EDFormatter(); edFormatter.Host = this.Host; retVal.Thumbnail = (ED)edFormatter.Parse(s, result); // Parse ED } else if (s.LocalName == "reference") // Format using TEL { TELFormatter telFormatter = new TELFormatter(); telFormatter.Host = this.Host; retVal.Reference = (TEL)telFormatter.Parse(s, result); } else if (s.NodeType == System.Xml.XmlNodeType.Text || s.NodeType == System.Xml.XmlNodeType.CDATA) { innerData += s.Value; } else if (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName) && (s.NodeType == System.Xml.XmlNodeType.Element || s.NodeType == System.Xml.XmlNodeType.EndElement)) { retVal.Representation = EncapsulatedDataRepresentation.XML; innerData += s.ReadOuterXml(); } } catch (MessageValidationException e) { result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, s.ToString(), e)); } finally { if (s.Name == oldName) { s.Read(); } } } } #endregion Encoding textEncoding = System.Text.Encoding.UTF8; // Parse the innerData string into something meaningful if (innerData.Length > 0) { if (retVal.Representation == EncapsulatedDataRepresentation.B64) { retVal.Data = Convert.FromBase64String(innerData); } else { retVal.Data = textEncoding.GetBytes(innerData); } } // Finally, the hash, this will validate the data if (!retVal.ValidateIntegrityCheck()) { result.AddResultDetail(new ResultDetail(ResultDetailType.Warning, string.Format("Encapsulated data with content starting with '{0}' failed integrity check!", retVal.ToString().PadRight(10, ' ').Substring(0, 10)), s.ToString(), null)); } // Validate baseFormatter.Validate(retVal, pathName, result); return(retVal); }