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;
        }
Exemple #2
0
        internal static T Parse <T>(System.Xml.XmlReader s, IXmlStructureFormatter host, DatatypeFormatterParseResult result) where T : ANY, ICodedValue, new()
        {
            // Parse base (ANY) from the stream
            ANYFormatter anyFormatter = new ANYFormatter();
            string       pathName     = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name;

            // Parse CV
            anyFormatter.Host = host;
            T retVal = anyFormatter.Parse <T>(s, result);

            // Now parse our data out... Attributes
            // Was there a null flavor processed?
            if (s.GetAttribute("code") != null)
            {
                retVal.CodeValue = s.GetAttribute("code");
            }
            if (s.GetAttribute("codeSystem") != null)
            {
                retVal.CodeSystem = s.GetAttribute("codeSystem");
            }
            if (s.GetAttribute("codeSystemName") != null)
            {
                retVal.CodeSystemName = s.GetAttribute("codeSystemName");
            }
            if (s.GetAttribute("codeSystemVersion") != null)
            {
                retVal.CodeSystemVersion = s.GetAttribute("codeSystemVersion");
            }
            if (s.GetAttribute("displayName") != null)
            {
                retVal.DisplayName = Util.Convert <ST>(s.GetAttribute("displayName"));
            }

            // Elements
            #region Elements
            if (!s.IsEmptyElement)
            {
                int    sDepth = s.Depth;
                string sName  = s.Name;

                s.Read();
                // string Name
                while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName))
                {
                    string oldName = s.Name; // Name
                    try
                    {
                        if (s.LocalName == "originalText") // Format using ED
                        {
                            EDFormatter edFormatter = new EDFormatter();
                            edFormatter.Host    = host;
                            retVal.OriginalText = (ED)edFormatter.Parse(s, result);           // Parse ED
                        }
                        else if (s.LocalName == "translation" && retVal is ICodedEquivalents) // Translation
                        {
                            LISTFormatter setFormatter = new LISTFormatter();
                            setFormatter.GenericArguments = new Type[] { typeof(CD <String>) };
                            setFormatter.Host             = host;
                            if (retVal is ICodedEquivalents)
                            {
                                ((ICodedEquivalents)retVal).Translation = (LIST <IGraphable>)setFormatter.Parse(s, result); // Parse LIST
                            }
                            else
                            {
                                result.AddResultDetail(new NotImplementedElementResultDetail(ResultDetailType.Warning, s.LocalName, s.NamespaceURI, s.ToString(), null));
                            }
                        }
                        else if (s.LocalName == "qualifier" && retVal is IConceptDescriptor) // Qualifier
                        {
                            SETFormatter setFormatter = new SETFormatter();
                            setFormatter.GenericArguments = new Type[] { typeof(CR <String>) };
                            setFormatter.Host             = host;
                            if (retVal is IConceptDescriptor)
                            {
                                ((IConceptDescriptor)retVal).Qualifier = (LIST <IGraphable>)setFormatter.Parse(s, result); // Parse SET
                            }
                            else
                            {
                                result.AddResultDetail(new NotImplementedElementResultDetail(ResultDetailType.Warning, s.LocalName, s.NamespaceURI, s.ToString(), null));
                            }
                        }
                    }
                    catch (VocabularyException e)
                    {
                        result.AddResultDetail(new VocabularyIssueResultDetail(ResultDetailType.Error, e.Message, e));
                    }
                    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


            // Validate
            anyFormatter.Validate(retVal, pathName, result);


            // Add validation to details
            return(retVal);
        }
Exemple #3
0
        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);
        }