diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java index d795fb2a1..362cf8364 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/XmlParser.java @@ -1,33 +1,33 @@ package org.hl7.fhir.r5.elementmodel; -/* - Copyright (c) 2011+, HL7, Inc. - All rights reserved. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - * Neither the name of HL7 nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - */ +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + */ import java.io.IOException; @@ -210,6 +210,10 @@ public class XmlParser extends ParserBase { return "h:"; if (ns.equals("urn:hl7-org:v3")) return "v3:"; + if (ns.equals("urn:hl7-org:sdtc")) + return "sdtc:"; + if (ns.equals("urn:ihe:pharm")) + return "pharm:"; return "?:"; } @@ -538,11 +542,31 @@ public class XmlParser extends ParserBase { xml.setDefaultNamespace(e.getProperty().getXmlNamespace()); if (hasTypeAttr(e)) xml.namespace("http://www.w3.org/2001/XMLSchema-instance", "xsi"); + addNamespaces(xml, e); composeElement(xml, e, e.getType(), true); xml.end(); - } + private void addNamespaces(IXMLWriter xml, Element e) throws IOException { + String ns = e.getProperty().getXmlNamespace(); + if (ns!=null && !xml.getDefaultNamespace().equals(ns)){ + if (!xml.namespaceDefined(ns)) { + String prefix = pathPrefix(ns); + if (prefix.endsWith(":")) { + prefix = prefix.substring(0, prefix.length()-1); + } + if ("?".equals(prefix)) { + xml.namespace(ns); + } else { + xml.namespace(ns, prefix); + } + } + } + for (Element c : e.getChildren()) { + addNamespaces(xml, c); + } + } + private boolean hasTypeAttr(Element e) { if (isTypeAttr(e.getProperty())) return true; @@ -584,9 +608,9 @@ public class XmlParser extends ParserBase { if (isText(element.getProperty())) { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); - xml.enter(elementName); + xml.enter(element.getProperty().getXmlNamespace(),elementName); xml.text(element.getValue()); - xml.exit(elementName); + xml.exit(element.getProperty().getXmlNamespace(),elementName); } else if (!element.hasChildren() && !element.hasValue()) { if (element.getExplicitType() != null) xml.attribute("xsi:type", element.getExplicitType()); @@ -614,10 +638,10 @@ public class XmlParser extends ParserBase { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); if (element.hasChildren()) { - xml.enter(elementName); - for (Element child : element.getChildren()) + xml.enter(element.getProperty().getXmlNamespace(), elementName); + for (Element child : element.getChildren()) composeElement(xml, child, child.getName(), false); - xml.exit(elementName); + xml.exit(element.getProperty().getXmlNamespace(),elementName); } else xml.element(elementName); } @@ -630,16 +654,16 @@ public class XmlParser extends ParserBase { String av = child.getValue(); if (ToolingExtensions.hasExtension(child.getProperty().getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat")) av = convertForDateFormatToExternal(ToolingExtensions.readStringExtension(child.getProperty().getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"), av); - xml.attribute(child.getName(), av); - } + xml.attribute(child.getProperty().getXmlNamespace(),child.getProperty().getXmlName(), av); + } } if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); - xml.enter(elementName); + xml.enter(element.getProperty().getXmlNamespace(),elementName); if (!root && element.getSpecial() != null) { if (linkResolver != null) xml.link(linkResolver.resolveProperty(element.getProperty())); - xml.enter(element.getType()); + xml.enter(element.getProperty().getXmlNamespace(),element.getType()); } for (Element child : element.getChildren()) { if (isText(child.getProperty())) { @@ -650,8 +674,8 @@ public class XmlParser extends ParserBase { composeElement(xml, child, child.getName(), false); } if (!root && element.getSpecial() != null) - xml.exit(element.getType()); - xml.exit(elementName); + xml.exit(element.getProperty().getXmlNamespace(),element.getType()); + xml.exit(element.getProperty().getXmlNamespace(),elementName); } } diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/CDARoundTripTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/CDARoundTripTests.java index f9fd0efe2..57d676eb7 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/CDARoundTripTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/test/CDARoundTripTests.java @@ -214,8 +214,7 @@ public class CDARoundTripTests { ByteArrayOutputStream baosXml = new ByteArrayOutputStream(); Manager.compose(context, cda, baosXml, FhirFormat.XML, OutputStyle.PRETTY, null); Element cdaXmlRoundtrip = Manager.parse(context, new ByteArrayInputStream(baosXml.toString().getBytes()), FhirFormat.XML); - -// assertsExample(cdaXmlRoundtrip); + assertsExample(cdaXmlRoundtrip); } @Test