예제 #1
0
        static void Main(string[] args)
        {
            var xml = "<Patient xmlns=\"http://hl7.org/fhir\"><identifier>" +
                      "<use value=\"official\" /></identifier></Patient>";
            MemoryStream memStream = new MemoryStream();

            byte[] data = Encoding.Default.GetBytes(xml);
            memStream.Write(data, 0, data.Length);
            memStream.Position = 0;
            XmlReader reader = XmlReader.Create(memStream);

            reader.Read();

            FhirXmlParsingSettings settings = new FhirXmlParsingSettings();

            ISourceNode patientNode = FhirXmlNode.Read(reader, settings);
            //IResourceResolver Resolver = new TestResourceResolver();
            IResourceResolver Resolver = ZipSource.CreateValidationSource();


            StructureDefinitionSummaryProvider Provider = new StructureDefinitionSummaryProvider(Resolver);



            ITypedElement patientRootElement = patientNode.ToTypedElement(Provider);

            var    r    = patientRootElement.Select("Patient.identifier.use");
            string test = (string)r.FirstOrDefault().Value;

            //ITypedElement activeElement = patientRootElement.Children("active").First();
            //Assert.AreEqual("boolean", activeElement.Type);

            //Assert.AreEqual("boolean", activeElement.Type);


            //var patientNode = FhirXmlNode.Parse(xml);
            //var use = patientNode.Children("identifier").Children("use").First();
            //Assert.AreEqual("official", use.Text);
            //Assert.AreEqual("Patient.identifier[0].use[0]", use.Location);
        }
예제 #2
0
        public void TestPermissiveParsing()
        {
            var tpXml = File.ReadAllText(@"TestData\all-xml-features.xml");

            // will allow whitespace and comments to come through
            var reader = XmlReader.Create(new StringReader(tpXml));
            var nav    = FhirXmlNode.Read(reader, new FhirXmlParsingSettings {
                PermissiveParsing = true
            });

            Assert.AreEqual("SomeResource", nav.Name);

            var xmldetails     = (nav as IAnnotated).Annotation <XmlSerializationDetails>();
            var commentdetails = (nav as IAnnotated).Annotation <SourceComments>();

            Assert.IsNotNull(xmldetails);
            Assert.AreEqual(XmlNodeType.Element, xmldetails.NodeType);
            Assert.AreEqual("http://hl7.org/fhir", xmldetails.Namespace.NamespaceName);
            Assert.IsTrue(commentdetails.CommentsBefore.Single().Contains("structural errors"));
            Assert.IsTrue(commentdetails.DocumentEndComments.Single().Contains("standard FHIR"));
            Assert.IsNull(nav.Text);

            // namespace attributes should not be found
            var children = nav.Children().ToList();

            Assert.AreEqual(3, children.Count);
            assertAnElement(children[0]);
            assertAnElementWithValueAndChildren(children[1]);
            assertDiv(children[2]);

            void assertAnElement(ISourceNode cn)
            {
                Assert.AreEqual("anElement", cn.Name);
                Assert.AreEqual("true", cn.Text);
                Assert.AreEqual(1, cn.Children().Count());
                cn = cn.Children().First();

                Assert.AreEqual("customAttribute", cn.Name);
                Assert.AreEqual("primitive", cn.Text);

                var xd = (cn as IAnnotated).Annotation <XmlSerializationDetails>();

                Assert.AreEqual(XmlNodeType.Attribute, xd.NodeType);
                Assert.AreEqual(xd.Namespace + "customAttribute", XName.Get("customAttribute", "http://example.org/some-ns"));
                Assert.IsFalse(cn.Children().Any());
            }

            void assertAnElementWithValueAndChildren(ISourceNode cn)
            {
                Assert.AreEqual("anElementWithValueAndChildren", cn.Name);
                Assert.AreEqual("4", cn.Text);

                var mylittledetails = (cn as IAnnotated).Annotation <XmlSerializationDetails>();

                Assert.IsTrue(mylittledetails.NodeText.Contains("Crap, mixed content!"));
                Assert.IsTrue(mylittledetails.NodeText.Contains("Is Merged"));

                var cnc = cn.Children().ToList();

                Assert.AreEqual(3, cnc.Count);
                firstChild(cnc[0]);
                secondChild(cnc[1]);
                thirdChild(cnc[2]);

                void firstChild(ISourceNode ccn)
                {
                    Assert.AreEqual("firstChild", ccn.Name);
                    Assert.IsNull(ccn.Text);
                    var ccnc = ccn.Children().ToList();

                    Assert.AreEqual(1, ccnc.Count);

                    var xd = (ccn as IAnnotated).Annotation <XmlSerializationDetails>();

                    Assert.AreEqual("I have text content", xd.NodeText);

                    Assert.AreEqual("customAttribute", ccnc[0].Name);
                    Assert.AreEqual("morning", ccnc[0].Text);
                }

                void secondChild(ISourceNode ccn)
                {
                    Assert.AreEqual("secondChild", ccn.Name);
                    Assert.AreEqual("afternoon", ccn.Text);
                    Assert.IsFalse(ccn.Children().Any());

                    var xd = (ccn as IAnnotated).Annotation <XmlSerializationDetails>();

                    Assert.AreEqual("I have text content too", xd.NodeText);
                }

                void thirdChild(ISourceNode ccn)
                {
                    Assert.AreEqual("ThirdChild", ccn.Name);
                    Assert.IsNull(ccn.Text);
                    Assert.IsTrue(ccn.Children().Any());

                    var xd = (ccn as IAnnotated).Annotation <XmlSerializationDetails>();
                    var cd = (ccn as IAnnotated).Annotation <SourceComments>();

                    Assert.AreEqual(" this should be possible ", cd.ClosingComments.Single());
                    Assert.IsFalse(cd.CommentsBefore.Any());
                }
            }

            void assertDiv(ISourceNode cnn)
            {
                var val = cnn.Text;

                Assert.IsTrue(val.StartsWith("<div") && val.Contains("Some html"));
                Assert.IsFalse(cnn.Children().Any());  // html should not be represented as children

                var xd = (cnn as IAnnotated).Annotation <XmlSerializationDetails>();
                var cd = (cnn as IAnnotated).Annotation <SourceComments>();

                Assert.AreEqual(XmlNs.XHTMLNS, xd.Namespace);
                Assert.AreEqual(2, cd.CommentsBefore.Length);
                Assert.AreEqual(" next line intentionally left empty ", cd.CommentsBefore.First());
                Assert.AreEqual(" Div is really special, since the value includes the node itself ", cd.CommentsBefore.Last());
            }
        }