private static Bundle ParseXML(string content, bool permissive) { Bundle bundle = null; // Grab all errors found by visiting all nodes and report if not permissive if (!permissive) { List <string> entries = new List <string>(); ISourceNode node = FhirXmlNode.Parse(content, new FhirXmlParsingSettings { PermissiveParsing = permissive }); foreach (Hl7.Fhir.Utility.ExceptionNotification problem in node.VisitAndCatch()) { entries.Add(problem.Message); } if (entries.Count > 0) { throw new System.ArgumentException(String.Join("; ", entries).TrimEnd()); } } // Try Parse try { FhirXmlParser parser = new FhirXmlParser(GetParserSettings(permissive)); bundle = parser.Parse <Bundle>(content); } catch (Exception e) { throw new System.ArgumentException(e.Message); } return(bundle); }
public void CatchesLowLevelErrors() { var tpXml = File.ReadAllText(Path.Combine("TestData", "with-errors.xml")); var patient = getXmlUntyped(tpXml); var result = patient.VisitAndCatch(); var originalCount = result.Count; Assert.AreEqual(11, result.Count); Assert.IsTrue(!result.Any(r => r.Message.Contains("schemaLocation"))); patient = getXmlUntyped(tpXml, new FhirXmlParsingSettings() { DisallowSchemaLocation = true, PermissiveParsing = false }); result = patient.VisitAndCatch(); Assert.IsTrue(result.Count == originalCount + 1); // one extra error about schemaLocation being present Assert.IsTrue(result.Any(r => r.Message.Contains("schemaLocation"))); patient = FhirXmlNode.Parse(tpXml, new FhirXmlParsingSettings() { PermissiveParsing = true }); result = patient.VisitAndCatch(); Assert.AreEqual(0, result.Count); }
public void CompareToOtherElementNavigator() { var json = TestDataHelper.ReadTestData("TestPatient.json"); var xml = TestDataHelper.ReadTestData("TestPatient.xml"); var pocoP = (new FhirJsonParser()).Parse <Patient>(json).ToTypedElement(); var jsonP = FhirJsonNode.Parse(json, settings: new FhirJsonParsingSettings { AllowJsonComments = true }) .ToTypedElement(new PocoStructureDefinitionSummaryProvider()); var xmlP = FhirXmlNode.Parse(xml).ToTypedElement(new PocoStructureDefinitionSummaryProvider()); doCompare(pocoP, jsonP, "poco<->json"); doCompare(pocoP, xmlP, "poco<->xml"); void doCompare(ITypedElement one, ITypedElement two, string what) { var compare = one.IsEqualTo(two); if (compare.Success == false) { Debug.WriteLine($"{what}: Difference in {compare.Details} at {compare.FailureLocation}"); Assert.Fail(); } } }
public void ReadsFromNav() { var tpXml = File.ReadAllText(@"TestData\fp-test-patient.xml"); var xmlnode = FhirXmlNode.Parse(tpXml); var nodes = SourceNode.FromNode(xmlnode); Assert.True(xmlnode.IsEqualTo(nodes).Success); }
public void PreservesParsingExceptionDetails() { try { var nav = FhirXmlNode.Parse("{"); var dummy = nav.Text; Assert.Fail(); } catch (FormatException fe) { Assert.IsInstanceOfType(fe.InnerException, typeof(XmlException)); } }
public static ITypedElement ParseToTypedElement(string xml, IStructureDefinitionSummaryProvider provider, FhirXmlParsingSettings settings = null, TypedElementSettings tnSettings = null) { if (xml == null) { throw Error.ArgumentNull(nameof(xml)); } if (provider == null) { throw Error.ArgumentNull(nameof(provider)); } return(FhirXmlNode.Parse(xml, settings).ToTypedElement(provider, null, tnSettings)); }
public void TryInvalidUntypedSource() { var xmlNav = FhirXmlNode.Parse("<Patient xmlns='http://hl7.org/fhir'><active value='true'/></Patient>"); try { var output = xmlNav.ToJson(); Assert.Fail(); } catch (NotSupportedException) { } }
private static ITypedElement parseResource(string bodyText, string contentType, IStructureDefinitionSummaryProvider provider, bool throwOnFormatException) { if (bodyText == null) { throw Error.ArgumentNull(nameof(bodyText)); } if (provider == null) { throw Error.ArgumentNull(nameof(provider)); } var fhirType = ContentType.GetResourceFormatFromContentType(contentType); if (fhirType == ResourceFormat.Unknown) { throw new UnsupportedBodyTypeException( "Endpoint returned a body with contentType '{0}', while a valid FHIR xml/json body type was expected. Is this a FHIR endpoint?" .FormatWith(contentType), contentType, bodyText); } if (!SerializationUtil.ProbeIsJson(bodyText) && !SerializationUtil.ProbeIsXml(bodyText)) { throw new UnsupportedBodyTypeException( "Endpoint said it returned '{0}', but the body is not recognized as either xml or json.".FormatWith(contentType), contentType, bodyText); } try { return((fhirType == ResourceFormat.Json) ? FhirJsonNode.Parse(bodyText).ToTypedElement(provider) : FhirXmlNode.Parse(bodyText).ToTypedElement(provider)); } catch (FormatException) when(!throwOnFormatException) { return(null); } }
public ITypedElement getXmlNode(string xml) => FhirXmlNode.Parse(xml).ToTypedElement(new PocoStructureDefinitionSummaryProvider());
public void TestSummary() { var p = new Patient { BirthDate = "1972-11-30", // present in both summary and full Photo = new List <Attachment>() { new Attachment() { ContentType = "text/plain" } } }; var full = FhirXmlSerializer.SerializeToString(p); Assert.IsTrue(full.Contains("<birthDate")); Assert.IsTrue(full.Contains("<photo")); Assert.IsNull(p.Meta, "Meta element should not be introduced here."); var summ = FhirXmlSerializer.SerializeToString(p, summary: Fhir.Rest.SummaryType.True); Assert.IsTrue(summ.Contains("<birthDate")); Assert.IsFalse(summ.Contains("<photo")); Assert.IsNull(p.Meta, "Meta element should not be introduced here."); var q = new Questionnaire { Text = new Narrative() { Div = "<div xmlns=\"http://www.w3.org/1999/xhtml\">Test Questionnaire</div>" }, Status = Questionnaire.QuestionnaireStatus.Published, Date = "2015-09-27", Group = new Questionnaire.GroupComponent { Title = "TITLE", Text = "TEXT", LinkId = "linkid" } }; Assert.IsNull(q.Meta, "Meta element has not been created."); var qfull = FhirXmlSerializer.SerializeToString(q); Assert.IsNull(q.Meta, "Meta element should not be introduced here."); Console.WriteLine("summary: Fhir.Rest.SummaryType.False"); Console.WriteLine(qfull); Assert.IsTrue(qfull.Contains("Test Questionnaire")); Assert.IsTrue(qfull.Contains("<status value=\"published\"")); Assert.IsTrue(qfull.Contains("<date value=\"2015-09-27\"")); Assert.IsTrue(qfull.Contains("<title value=\"TITLE\"")); Assert.IsTrue(qfull.Contains("<text value=\"TEXT\"")); Assert.IsTrue(qfull.Contains("<linkId value=\"linkid\"")); var qSum = FhirXmlSerializer.SerializeToString(q, summary: Fhir.Rest.SummaryType.True); Console.WriteLine("summary: Fhir.Rest.SummaryType.True"); Console.WriteLine(qSum); Assert.IsFalse(qSum.Contains("Test Questionnaire")); Assert.IsTrue(qSum.Contains("<status value=\"published\"")); Assert.IsTrue(qSum.Contains("<date value=\"2015-09-27\"")); Assert.IsTrue(qSum.Contains("<title value=\"TITLE\"")); Assert.IsFalse(qSum.Contains("<text value=\"TEXT\"")); Assert.IsFalse(qSum.Contains("<linkId value=\"linkid\"")); var qData = FhirXmlSerializer.SerializeToString(q, summary: Fhir.Rest.SummaryType.Data); Console.WriteLine("summary: Fhir.Rest.SummaryType.Data"); Console.WriteLine(qData); Assert.IsFalse(qData.Contains("Test Questionnaire")); Assert.IsTrue(qData.Contains("<meta")); Assert.IsTrue(qData.Contains("<text value=\"TEXT\"")); Assert.IsTrue(qData.Contains("<status value=\"published\"")); Assert.IsTrue(qData.Contains("<date value=\"2015-09-27\"")); Assert.IsTrue(qData.Contains("<title value=\"TITLE\"")); Assert.IsTrue(qData.Contains("<linkId value=\"linkid\"")); q.Meta = new Meta { VersionId = "v2" }; var qText = FhirXmlSerializer.SerializeToString(q, summary: Fhir.Rest.SummaryType.Text); Console.WriteLine("summary: Fhir.Rest.SummaryType.Text"); Console.WriteLine(qText); Assert.IsTrue(qText.Contains("Test Questionnaire")); Assert.IsTrue(qText.Contains("<meta")); Assert.IsTrue(qText.Contains("<status value=\"published\"")); Assert.IsFalse(qText.Contains("<text value=\"TEXT\"")); Assert.IsFalse(qText.Contains("<date value=\"2015-09-27\"")); Assert.IsFalse(qText.Contains("<title value=\"TITLE\"")); Assert.IsFalse(qText.Contains("<linkId value=\"linkid\"")); Assert.AreEqual(0, q.Meta.Tag.Where(t => t.System == "http://hl7.org/fhir/v3/ObservationValue" && t.Code == "SUBSETTED").Count(), "Subsetted Tag should not still be there."); // Verify that reloading the content into an object... // make sure we accept the crappy output with empty groups var nav = FhirXmlNode.Parse(qText, new FhirXmlParsingSettings { PermissiveParsing = true }); var qInflate = FhirXmlParser.Parse <Questionnaire>(nav); Assert.AreEqual(1, qInflate.Meta.Tag.Where(t => t.System == "http://hl7.org/fhir/v3/ObservationValue" && t.Code == "SUBSETTED").Count(), "Subsetted Tag should not still be there."); }
public void RoundtripXmlUntyped() { ParseDemoPatient.RoundtripXml(xmlText => FhirXmlNode.Parse(xmlText)); }
public ISourceNode getXmlUntyped(string xml, FhirXmlParsingSettings settings = null) => FhirXmlNode.Parse(xml, settings);
public ITypedElement getTestData() { var tpXml = TestData.ReadTextFile("fp-test-patient.xml"); return(FhirXmlNode.Parse(tpXml).ToTypedElement(new PocoStructureDefinitionSummaryProvider())); }
public ISourceNode getXmlUntyped(string xml, FhirXmlParsingSettings settings = null) { settings = settings ?? FhirXmlParsingSettings.CreateDefault(); settings.PermissiveParsing = false; return(FhirXmlNode.Parse(xml, settings)); }