001package ca.uhn.fhir.parser;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
024import ca.uhn.fhir.context.BaseRuntimeDeclaredChildDefinition;
025import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
026import ca.uhn.fhir.context.ConfigurationException;
027import ca.uhn.fhir.context.FhirContext;
028import ca.uhn.fhir.context.RuntimeChildContainedResources;
029import ca.uhn.fhir.context.RuntimeChildExtension;
030import ca.uhn.fhir.context.RuntimeChildNarrativeDefinition;
031import ca.uhn.fhir.context.RuntimeChildUndeclaredExtensionDefinition;
032import ca.uhn.fhir.context.RuntimeResourceDefinition;
033import ca.uhn.fhir.i18n.Msg;
034import ca.uhn.fhir.model.api.IResource;
035import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
036import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
037import ca.uhn.fhir.model.api.Tag;
038import ca.uhn.fhir.model.api.TagList;
039import ca.uhn.fhir.model.base.composite.BaseCodingDt;
040import ca.uhn.fhir.model.primitive.IdDt;
041import ca.uhn.fhir.model.primitive.InstantDt;
042import ca.uhn.fhir.model.primitive.XhtmlDt;
043import ca.uhn.fhir.narrative.INarrativeGenerator;
044import ca.uhn.fhir.rest.api.EncodingEnum;
045import ca.uhn.fhir.util.ElementUtil;
046import ca.uhn.fhir.util.NonPrettyPrintWriterWrapper;
047import ca.uhn.fhir.util.PrettyPrintWriterWrapper;
048import ca.uhn.fhir.util.XmlUtil;
049import org.apache.commons.lang3.StringUtils;
050import org.hl7.fhir.instance.model.api.IAnyResource;
051import org.hl7.fhir.instance.model.api.IBase;
052import org.hl7.fhir.instance.model.api.IBaseBinary;
053import org.hl7.fhir.instance.model.api.IBaseDatatype;
054import org.hl7.fhir.instance.model.api.IBaseExtension;
055import org.hl7.fhir.instance.model.api.IBaseHasExtensions;
056import org.hl7.fhir.instance.model.api.IBaseHasModifierExtensions;
057import org.hl7.fhir.instance.model.api.IBaseResource;
058import org.hl7.fhir.instance.model.api.IBaseXhtml;
059import org.hl7.fhir.instance.model.api.IIdType;
060import org.hl7.fhir.instance.model.api.IPrimitiveType;
061
062import javax.xml.namespace.QName;
063import javax.xml.stream.FactoryConfigurationError;
064import javax.xml.stream.XMLEventReader;
065import javax.xml.stream.XMLStreamConstants;
066import javax.xml.stream.XMLStreamException;
067import javax.xml.stream.XMLStreamWriter;
068import javax.xml.stream.events.Attribute;
069import javax.xml.stream.events.Characters;
070import javax.xml.stream.events.Comment;
071import javax.xml.stream.events.EntityReference;
072import javax.xml.stream.events.Namespace;
073import javax.xml.stream.events.StartElement;
074import javax.xml.stream.events.XMLEvent;
075import java.io.Reader;
076import java.io.Writer;
077import java.util.ArrayList;
078import java.util.Iterator;
079import java.util.List;
080import java.util.Optional;
081
082import static org.apache.commons.lang3.StringUtils.isBlank;
083import static org.apache.commons.lang3.StringUtils.isNotBlank;
084
085/**
086 * This class is the FHIR XML parser/encoder. Users should not interact with this class directly, but should use
087 * {@link FhirContext#newXmlParser()} to get an instance.
088 */
089public class XmlParser extends BaseParser {
090
091        static final String FHIR_NS = "http://hl7.org/fhir";
092        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(XmlParser.class);
093        private boolean myPrettyPrint;
094
095        /**
096         * Do not use this constructor, the recommended way to obtain a new instance of the XML parser is to invoke
097         * {@link FhirContext#newXmlParser()}.
098         *
099         * @param theParserErrorHandler
100         */
101        public XmlParser(FhirContext theContext, IParserErrorHandler theParserErrorHandler) {
102                super(theContext, theParserErrorHandler);
103        }
104
105        private XMLEventReader createStreamReader(Reader theReader) {
106                try {
107                        return XmlUtil.createXmlReader(theReader);
108                } catch (FactoryConfigurationError e1) {
109                        throw new ConfigurationException(Msg.code(1848) + "Failed to initialize STaX event factory", e1);
110                } catch (XMLStreamException e1) {
111                        throw new DataFormatException(Msg.code(1849) + e1);
112                }
113        }
114
115        private XMLStreamWriter createXmlWriter(Writer theWriter) throws XMLStreamException {
116                XMLStreamWriter eventWriter;
117                eventWriter = XmlUtil.createXmlStreamWriter(theWriter);
118                eventWriter = decorateStreamWriter(eventWriter);
119                return eventWriter;
120        }
121
122        private XMLStreamWriter decorateStreamWriter(XMLStreamWriter eventWriter) {
123                if (myPrettyPrint) {
124                        PrettyPrintWriterWrapper retVal = new PrettyPrintWriterWrapper(eventWriter);
125                        return retVal;
126                }
127                NonPrettyPrintWriterWrapper retVal = new NonPrettyPrintWriterWrapper(eventWriter);
128                return retVal;
129        }
130
131        @Override
132        public void doEncodeResourceToWriter(IBaseResource theResource, Writer theWriter, EncodeContext theEncodeContext) throws DataFormatException {
133                XMLStreamWriter eventWriter;
134                try {
135                        eventWriter = createXmlWriter(theWriter);
136
137                        encodeResourceToXmlStreamWriter(theResource, eventWriter, false, theEncodeContext);
138                        eventWriter.flush();
139                } catch (XMLStreamException e) {
140                        throw new ConfigurationException(Msg.code(1850) + "Failed to initialize STaX event factory", e);
141                }
142        }
143
144        @Override
145        public <T extends IBaseResource> T doParseResource(Class<T> theResourceType, Reader theReader) {
146                XMLEventReader streamReader = createStreamReader(theReader);
147                return parseResource(theResourceType, streamReader);
148        }
149
150        private <T> T doXmlLoop(XMLEventReader streamReader, ParserState<T> parserState) {
151                ourLog.trace("Entering XML parsing loop with state: {}", parserState);
152
153                try {
154                        List<String> heldComments = new ArrayList<>(1);
155
156                        while (streamReader.hasNext()) {
157                                XMLEvent nextEvent = streamReader.nextEvent();
158                                try {
159
160                                        switch (nextEvent.getEventType()) {
161                                                case XMLStreamConstants.START_ELEMENT: {
162                                                        StartElement elem = nextEvent.asStartElement();
163
164                                                        String namespaceURI = elem.getName().getNamespaceURI();
165
166                                                        if ("extension".equals(elem.getName().getLocalPart())) {
167                                                                Attribute urlAttr = elem.getAttributeByName(new QName("url"));
168                                                                String url;
169                                                                if (urlAttr == null || isBlank(urlAttr.getValue())) {
170                                                                        getErrorHandler().missingRequiredElement(new ParseLocation().setParentElementName("extension"), "url");
171                                                                        url = null;
172                                                                } else {
173                                                                        url = urlAttr.getValue();
174                                                                }
175                                                                parserState.enteringNewElementExtension(elem, url, false, getServerBaseUrl());
176                                                        } else if ("modifierExtension".equals(elem.getName().getLocalPart())) {
177                                                                Attribute urlAttr = elem.getAttributeByName(new QName("url"));
178                                                                String url;
179                                                                if (urlAttr == null || isBlank(urlAttr.getValue())) {
180                                                                        getErrorHandler().missingRequiredElement(new ParseLocation().setParentElementName("modifierExtension"), "url");
181                                                                        url = null;
182                                                                } else {
183                                                                        url = urlAttr.getValue();
184                                                                }
185                                                                parserState.enteringNewElementExtension(elem, url, true, getServerBaseUrl());
186                                                        } else {
187                                                                String elementName = elem.getName().getLocalPart();
188                                                                parserState.enteringNewElement(namespaceURI, elementName);
189                                                        }
190
191                                                        if (!heldComments.isEmpty()) {
192                                                                for (String next : heldComments) {
193                                                                        parserState.commentPre(next);
194                                                                }
195                                                                heldComments.clear();
196                                                        }
197
198                                                        for (Iterator<Attribute> attributes = elem.getAttributes(); attributes.hasNext(); ) {
199                                                                Attribute next = attributes.next();
200                                                                parserState.attributeValue(next.getName().getLocalPart(), next.getValue());
201                                                        }
202
203                                                        break;
204                                                }
205                                                case XMLStreamConstants.END_DOCUMENT:
206                                                case XMLStreamConstants.END_ELEMENT: {
207                                                        if (!heldComments.isEmpty()) {
208                                                                for (String next : heldComments) {
209                                                                        parserState.commentPost(next);
210                                                                }
211                                                                heldComments.clear();
212                                                        }
213                                                        parserState.endingElement();
214                                                        break;
215                                                }
216                                                case XMLStreamConstants.CHARACTERS: {
217                                                        parserState.string(nextEvent.asCharacters().getData());
218                                                        break;
219                                                }
220                                                case XMLStreamConstants.COMMENT: {
221                                                        Comment comment = (Comment) nextEvent;
222                                                        String commentText = comment.getText();
223                                                        heldComments.add(commentText);
224                                                        break;
225                                                }
226                                        }
227
228                                        parserState.xmlEvent(nextEvent);
229
230                                } catch (DataFormatException e) {
231                                        throw new DataFormatException(Msg.code(1851) + "DataFormatException at [" + nextEvent.getLocation().toString() + "]: " + e.getMessage(), e);
232                                }
233                        }
234                        return parserState.getObject();
235                } catch (XMLStreamException e) {
236                        throw new DataFormatException(Msg.code(1852) + e);
237                }
238        }
239
240        private void encodeChildElementToStreamWriter(IBaseResource theResource, XMLStreamWriter theEventWriter, BaseRuntimeChildDefinition theChildDefinition, IBase theElement, String theChildName, BaseRuntimeElementDefinition<?> childDef,
241                                                                                                                                 String theExtensionUrl, boolean theIncludedResource, CompositeChildElement theParent, EncodeContext theEncodeContext) throws XMLStreamException, DataFormatException {
242
243                /*
244                 * Often the two values below will be the same thing. There are cases though
245                 * where they will not be. An example would be Observation.value, which is
246                 * a choice type. If the value contains a Quantity, then:
247                 * childGenericName = "value"
248                 * theChildName = "valueQuantity"
249                 */
250                String childGenericName = theChildDefinition.getElementName();
251
252                theEncodeContext.pushPath(childGenericName, false);
253                try {
254
255                        if (theElement == null || theElement.isEmpty()) {
256                                if (isChildContained(childDef, theIncludedResource)) {
257                                        // We still want to go in..
258                                } else {
259                                        return;
260                                }
261                        }
262
263                        writeCommentsPre(theEventWriter, theElement);
264
265                        switch (childDef.getChildType()) {
266                                case ID_DATATYPE: {
267                                        IIdType value = IIdType.class.cast(theElement);
268                                        String encodedValue = "id".equals(theChildName) ? value.getIdPart() : value.getValue();
269                                        if (StringUtils.isNotBlank(encodedValue) || !super.hasNoExtensions(value)) {
270                                                theEventWriter.writeStartElement(theChildName);
271                                                if (StringUtils.isNotBlank(encodedValue)) {
272                                                        theEventWriter.writeAttribute("value", encodedValue);
273                                                }
274                                                encodeExtensionsIfPresent(theResource, theEventWriter, theElement, theIncludedResource, theEncodeContext);
275                                                theEventWriter.writeEndElement();
276                                        }
277                                        break;
278                                }
279                                case PRIMITIVE_DATATYPE: {
280                                        IPrimitiveType<?> pd = IPrimitiveType.class.cast(theElement);
281                                        String value = pd.getValueAsString();
282                                        if (value != null || !super.hasNoExtensions(pd)) {
283                                                theEventWriter.writeStartElement(theChildName);
284                                                String elementId = getCompositeElementId(theElement);
285                                                if (isNotBlank(elementId)) {
286                                                        theEventWriter.writeAttribute("id", elementId);
287                                                }
288                                                if (value != null) {
289                                                        theEventWriter.writeAttribute("value", value);
290                                                }
291                                                encodeExtensionsIfPresent(theResource, theEventWriter, theElement, theIncludedResource, theEncodeContext);
292                                                theEventWriter.writeEndElement();
293                                        }
294                                        break;
295                                }
296                                case RESOURCE_BLOCK:
297                                case COMPOSITE_DATATYPE: {
298                                        theEventWriter.writeStartElement(theChildName);
299                                        String elementId = getCompositeElementId(theElement);
300                                        if (isNotBlank(elementId)) {
301                                                theEventWriter.writeAttribute("id", elementId);
302                                        }
303                                        if (isNotBlank(theExtensionUrl)) {
304                                                theEventWriter.writeAttribute("url", theExtensionUrl);
305                                        }
306                                        encodeCompositeElementToStreamWriter(theResource, theElement, theEventWriter, theIncludedResource, theParent, theEncodeContext);
307                                        theEventWriter.writeEndElement();
308                                        break;
309                                }
310                                case CONTAINED_RESOURCE_LIST:
311                                case CONTAINED_RESOURCES: {
312                                        /*
313                                         * Disable per #103 for (IResource next : value.getContainedResources()) { if (getContainedResources().getResourceId(next) != null) { continue; }
314                                         * theEventWriter.writeStartElement("contained"); encodeResourceToXmlStreamWriter(next, theEventWriter, true, fixContainedResourceId(next.getId().getValue()));
315                                         * theEventWriter.writeEndElement(); }
316                                         */
317                                        for (IBaseResource next : getContainedResources().getContainedResources()) {
318                                                IIdType resourceId = getContainedResources().getResourceId(next);
319                                                theEventWriter.writeStartElement("contained");
320                                                String value = resourceId.getValue();
321                                                encodeResourceToXmlStreamWriter(next, theEventWriter, true, fixContainedResourceId(value), theEncodeContext);
322                                                theEventWriter.writeEndElement();
323                                        }
324                                        break;
325                                }
326                                case RESOURCE: {
327                                        IBaseResource resource = (IBaseResource) theElement;
328                                        String resourceName = getContext().getResourceType(resource);
329                                        if (!super.shouldEncodeResource(resourceName)) {
330                                                break;
331                                        }
332                                        theEventWriter.writeStartElement(theChildName);
333                                        theEncodeContext.pushPath(resourceName, true);
334                                        encodeResourceToXmlStreamWriter(resource, theEventWriter, theIncludedResource, theEncodeContext);
335                                        theEncodeContext.popPath();
336                                        theEventWriter.writeEndElement();
337                                        break;
338                                }
339                                case PRIMITIVE_XHTML: {
340                                        XhtmlDt dt = XhtmlDt.class.cast(theElement);
341                                        if (dt.hasContent()) {
342                                                encodeXhtml(dt, theEventWriter);
343                                        }
344                                        break;
345                                }
346                                case PRIMITIVE_XHTML_HL7ORG: {
347                                        IBaseXhtml dt = IBaseXhtml.class.cast(theElement);
348                                        if (!dt.isEmpty()) {
349                                                // TODO: this is probably not as efficient as it could be
350                                                XhtmlDt hdt = new XhtmlDt();
351                                                hdt.setValueAsString(dt.getValueAsString());
352                                                encodeXhtml(hdt, theEventWriter);
353                                        }
354                                        break;
355                                }
356                                case EXTENSION_DECLARED:
357                                case UNDECL_EXT: {
358                                        throw new IllegalStateException(Msg.code(1853) + "state should not happen: " + childDef.getName());
359                                }
360                        }
361
362                        writeCommentsPost(theEventWriter, theElement);
363
364                } finally {
365                        theEncodeContext.popPath();
366                }
367
368        }
369
370        private void encodeCompositeElementToStreamWriter(IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, boolean theContainedResource, CompositeChildElement theParent, EncodeContext theEncodeContext)
371                throws XMLStreamException, DataFormatException {
372
373                for (CompositeChildElement nextChildElem : super.compositeChildIterator(theElement, theContainedResource, theParent, theEncodeContext)) {
374
375                        BaseRuntimeChildDefinition nextChild = nextChildElem.getDef();
376
377                        if (nextChild.getElementName().equals("url") && theElement instanceof IBaseExtension) {
378                                /*
379                                 * XML encoding is a one-off for extensions. The URL element goes in an attribute
380                                 * instead of being encoded as a normal element, only for XML encoding
381                                 */
382                                continue;
383                        }
384
385                        if (nextChild instanceof RuntimeChildNarrativeDefinition) {
386                                Optional<IBase> narr = nextChild.getAccessor().getFirstValueOrNull(theElement);
387                                INarrativeGenerator gen = getContext().getNarrativeGenerator();
388                                if (gen != null && narr.isPresent() == false) {
389                                        gen.populateResourceNarrative(getContext(), theResource);
390                                }
391
392                                narr = nextChild.getAccessor().getFirstValueOrNull(theElement);
393                                if (narr.isPresent()) {
394                                        RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
395                                        String childName = nextChild.getChildNameByDatatype(child.getDatatype());
396                                        BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
397                                        encodeChildElementToStreamWriter(theResource, theEventWriter, nextChild, narr.get(), childName, type, null, theContainedResource, nextChildElem, theEncodeContext);
398                                        continue;
399                                }
400                        }
401
402                        if (nextChild instanceof RuntimeChildContainedResources) {
403                                encodeChildElementToStreamWriter(theResource, theEventWriter, nextChild, null, nextChild.getChildNameByDatatype(null), nextChild.getChildElementDefinitionByDatatype(null), null, theContainedResource, nextChildElem, theEncodeContext);
404                        } else {
405
406                                List<? extends IBase> values = nextChild.getAccessor().getValues(theElement);
407                                values = preProcessValues(nextChild, theResource, values, nextChildElem, theEncodeContext);
408
409                                if (values == null || values.isEmpty()) {
410                                        continue;
411                                }
412                                for (IBase nextValue : values) {
413                                        if ((nextValue == null || nextValue.isEmpty())) {
414                                                continue;
415                                        }
416
417                                        BaseParser.ChildNameAndDef childNameAndDef = super.getChildNameAndDef(nextChild, nextValue);
418                                        if (childNameAndDef == null) {
419                                                continue;
420                                        }
421
422                                        String childName = childNameAndDef.getChildName();
423                                        BaseRuntimeElementDefinition<?> childDef = childNameAndDef.getChildDef();
424                                        String extensionUrl = getExtensionUrl(nextChild.getExtensionUrl());
425
426                                        boolean isExtension = childName.equals("extension") || childName.equals("modifierExtension");
427                                        if (isExtension && nextValue instanceof IBaseExtension) {
428                                                IBaseExtension<?, ?> ext = (IBaseExtension<?, ?>) nextValue;
429                                                if (isBlank(ext.getUrl())) {
430                                                        ParseLocation loc = new ParseLocation(theEncodeContext.toString() + "." + childName);
431                                                        getErrorHandler().missingRequiredElement(loc, "url");
432                                                }
433                                                if (ext.getValue() != null && ext.getExtension().size() > 0) {
434                                                        ParseLocation loc = new ParseLocation(theEncodeContext.toString() + "." + childName);
435                                                        getErrorHandler().extensionContainsValueAndNestedExtensions(loc);
436                                                }
437                                        }
438
439                                        if (extensionUrl != null && isExtension == false) {
440                                                encodeExtension(theResource, theEventWriter, theContainedResource, nextChildElem, nextChild, nextValue, childName, extensionUrl, childDef, theEncodeContext);
441                                        } else if (nextChild instanceof RuntimeChildExtension) {
442                                                IBaseExtension<?, ?> extension = (IBaseExtension<?, ?>) nextValue;
443                                                if ((extension.getValue() == null || extension.getValue().isEmpty())) {
444                                                        if (extension.getExtension().isEmpty()) {
445                                                                continue;
446                                                        }
447                                                }
448                                                encodeChildElementToStreamWriter(theResource, theEventWriter, nextChild, nextValue, childName, childDef, getExtensionUrl(extension.getUrl()), theContainedResource, nextChildElem, theEncodeContext);
449                                        } else {
450                                                encodeChildElementToStreamWriter(theResource, theEventWriter, nextChild, nextValue, childName, childDef, extensionUrl, theContainedResource, nextChildElem, theEncodeContext);
451                                        }
452
453                                }
454                        }
455                }
456        }
457
458        private void encodeExtension(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theContainedResource, CompositeChildElement nextChildElem, BaseRuntimeChildDefinition nextChild, IBase nextValue, String childName, String extensionUrl, BaseRuntimeElementDefinition<?> childDef, EncodeContext theEncodeContext)
459                throws XMLStreamException {
460                BaseRuntimeDeclaredChildDefinition extDef = (BaseRuntimeDeclaredChildDefinition) nextChild;
461                if (extDef.isModifier()) {
462                        theEventWriter.writeStartElement("modifierExtension");
463                } else {
464                        theEventWriter.writeStartElement("extension");
465                }
466
467                String elementId = getCompositeElementId(nextValue);
468                if (isNotBlank(elementId)) {
469                        theEventWriter.writeAttribute("id", elementId);
470                }
471
472                if (isBlank(extensionUrl)) {
473                        ParseLocation loc = new ParseLocation(theEncodeContext.toString());
474                        getErrorHandler().missingRequiredElement(loc, "url");
475                } else {
476                        theEventWriter.writeAttribute("url", extensionUrl);
477                }
478
479                encodeChildElementToStreamWriter(theResource, theEventWriter, nextChild, nextValue, childName, childDef, null, theContainedResource, nextChildElem, theEncodeContext);
480                theEventWriter.writeEndElement();
481        }
482
483        private void encodeExtensionsIfPresent(IBaseResource theResource, XMLStreamWriter theWriter, IBase theElement, boolean theIncludedResource, EncodeContext theEncodeContext) throws XMLStreamException, DataFormatException {
484                if (theElement instanceof ISupportsUndeclaredExtensions) {
485                        ISupportsUndeclaredExtensions res = (ISupportsUndeclaredExtensions) theElement;
486                        encodeUndeclaredExtensions(theResource, theWriter, toBaseExtensionList(res.getUndeclaredExtensions()), "extension", theIncludedResource, theEncodeContext);
487                        encodeUndeclaredExtensions(theResource, theWriter, toBaseExtensionList(res.getUndeclaredModifierExtensions()), "modifierExtension", theIncludedResource, theEncodeContext);
488                }
489                if (theElement instanceof IBaseHasExtensions) {
490                        IBaseHasExtensions res = (IBaseHasExtensions) theElement;
491                        encodeUndeclaredExtensions(theResource, theWriter, res.getExtension(), "extension", theIncludedResource, theEncodeContext);
492                }
493                if (theElement instanceof IBaseHasModifierExtensions) {
494                        IBaseHasModifierExtensions res = (IBaseHasModifierExtensions) theElement;
495                        encodeUndeclaredExtensions(theResource, theWriter, res.getModifierExtension(), "modifierExtension", theIncludedResource, theEncodeContext);
496                }
497        }
498
499        private void encodeResourceToXmlStreamWriter(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theIncludedResource, EncodeContext theEncodeContext) throws XMLStreamException, DataFormatException {
500                IIdType resourceId = null;
501
502                if (StringUtils.isNotBlank(theResource.getIdElement().getIdPart())) {
503                        resourceId = theResource.getIdElement();
504                        if (theResource.getIdElement().getValue().startsWith("urn:")) {
505                                resourceId = null;
506                        }
507                }
508
509                if (!theIncludedResource) {
510                        if (super.shouldEncodeResourceId(theResource, theEncodeContext) == false) {
511                                resourceId = null;
512                        } else if (theEncodeContext.getResourcePath().size() == 1 && getEncodeForceResourceId() != null) {
513                                resourceId = getEncodeForceResourceId();
514                        }
515                }
516
517                encodeResourceToXmlStreamWriter(theResource, theEventWriter, theIncludedResource, resourceId, theEncodeContext);
518        }
519
520        private void encodeResourceToXmlStreamWriter(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theContainedResource, IIdType theResourceId, EncodeContext theEncodeContext) throws XMLStreamException {
521                RuntimeResourceDefinition resDef = getContext().getResourceDefinition(theResource);
522                if (resDef == null) {
523                        throw new ConfigurationException(Msg.code(1854) + "Unknown resource type: " + theResource.getClass());
524                }
525
526                if (!theContainedResource) {
527                        setContainedResources(getContext().newTerser().containResources(theResource));
528                }
529
530                theEventWriter.writeStartElement(resDef.getName());
531                theEventWriter.writeDefaultNamespace(FHIR_NS);
532
533                if (theResource instanceof IAnyResource) {
534                        // HL7.org Structures
535                        if (theResourceId != null) {
536                                writeCommentsPre(theEventWriter, theResourceId);
537                                theEventWriter.writeStartElement("id");
538                                theEventWriter.writeAttribute("value", theResourceId.getIdPart());
539                                encodeExtensionsIfPresent(theResource, theEventWriter, theResourceId, false, theEncodeContext);
540                                theEventWriter.writeEndElement();
541                                writeCommentsPost(theEventWriter, theResourceId);
542                        }
543
544                        encodeCompositeElementToStreamWriter(theResource, theResource, theEventWriter, theContainedResource, new CompositeChildElement(resDef, theEncodeContext), theEncodeContext);
545
546                } else {
547
548                        // DSTU2+
549
550                        IResource resource = (IResource) theResource;
551                        if (theResourceId != null) {
552          /*    writeCommentsPre(theEventWriter, theResourceId);
553              writeOptionalTagWithValue(theEventWriter, "id", theResourceId.getIdPart());
554                                            writeCommentsPost(theEventWriter, theResourceId);*/
555                                theEventWriter.writeStartElement("id");
556                                theEventWriter.writeAttribute("value", theResourceId.getIdPart());
557                                encodeExtensionsIfPresent(theResource, theEventWriter, theResourceId, false, theEncodeContext);
558                                theEventWriter.writeEndElement();
559                                writeCommentsPost(theEventWriter, theResourceId);
560                        }
561
562                        InstantDt updated = (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
563                        IdDt resourceId = resource.getId();
564                        String versionIdPart = resourceId.getVersionIdPart();
565                        if (isBlank(versionIdPart)) {
566                                versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource);
567                        }
568                        List<BaseCodingDt> securityLabels = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS);
569                        List<? extends IIdType> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES);
570                        profiles = super.getProfileTagsForEncoding(resource, profiles);
571
572                        TagList tags = getMetaTagsForEncoding((resource), theEncodeContext);
573
574                        if (super.shouldEncodeResourceMeta(resource) && ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
575                                theEventWriter.writeStartElement("meta");
576                                if (shouldEncodePath(resource, "meta.versionId")) {
577                                        writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart);
578                                }
579                                if (updated != null) {
580                                        if (shouldEncodePath(resource, "meta.lastUpdated")) {
581                                                writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString());
582                                        }
583                                }
584
585                                for (IIdType profile : profiles) {
586                                        theEventWriter.writeStartElement("profile");
587                                        theEventWriter.writeAttribute("value", profile.getValue());
588                                        theEventWriter.writeEndElement();
589                                }
590                                for (BaseCodingDt securityLabel : securityLabels) {
591                                        theEventWriter.writeStartElement("security");
592                                        encodeCompositeElementToStreamWriter(resource, securityLabel, theEventWriter, theContainedResource, null, theEncodeContext);
593                                        theEventWriter.writeEndElement();
594                                }
595                                if (tags != null) {
596                                        for (Tag tag : tags) {
597                                                if (tag.isEmpty()) {
598                                                        continue;
599                                                }
600                                                theEventWriter.writeStartElement("tag");
601                                                writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme());
602                                                writeOptionalTagWithValue(theEventWriter, "code", tag.getTerm());
603                                                writeOptionalTagWithValue(theEventWriter, "display", tag.getLabel());
604                                                theEventWriter.writeEndElement();
605                                        }
606                                }
607                                theEventWriter.writeEndElement();
608                        }
609
610                        if (theResource instanceof IBaseBinary) {
611                                IBaseBinary bin = (IBaseBinary) theResource;
612                                writeOptionalTagWithValue(theEventWriter, "contentType", bin.getContentType());
613                                writeOptionalTagWithValue(theEventWriter, "content", bin.getContentAsBase64());
614                        } else {
615                                encodeCompositeElementToStreamWriter(theResource, theResource, theEventWriter, theContainedResource, new CompositeChildElement(resDef, theEncodeContext), theEncodeContext);
616                        }
617
618                }
619
620                theEventWriter.writeEndElement();
621        }
622
623        private void encodeUndeclaredExtensions(IBaseResource theResource, XMLStreamWriter theEventWriter, List<? extends IBaseExtension<?, ?>> theExtensions, String tagName, boolean theIncludedResource, EncodeContext theEncodeContext)
624                throws XMLStreamException, DataFormatException {
625                for (IBaseExtension<?, ?> next : theExtensions) {
626                        if (next == null || (ElementUtil.isEmpty(next.getValue()) && next.getExtension().isEmpty())) {
627                                continue;
628                        }
629
630                        writeCommentsPre(theEventWriter, next);
631
632                        theEventWriter.writeStartElement(tagName);
633
634                        String elementId = getCompositeElementId(next);
635                        if (isNotBlank(elementId)) {
636                                theEventWriter.writeAttribute("id", elementId);
637                        }
638
639                        String url = getExtensionUrl(next.getUrl());
640                        if (isNotBlank(url)) {
641                                theEventWriter.writeAttribute("url", url);
642                        }
643
644                        if (next.getValue() != null) {
645                                IBaseDatatype value = next.getValue();
646                                RuntimeChildUndeclaredExtensionDefinition extDef = getContext().getRuntimeChildUndeclaredExtensionDefinition();
647                                String childName = extDef.getChildNameByDatatype(value.getClass());
648                                BaseRuntimeElementDefinition<?> childDef;
649                                if (childName == null) {
650                                        childDef = getContext().getElementDefinition(value.getClass());
651                                        if (childDef == null) {
652                                                throw new ConfigurationException(Msg.code(1855) + "Unable to encode extension, unrecognized child element type: " + value.getClass().getCanonicalName());
653                                        }
654                                        childName = RuntimeChildUndeclaredExtensionDefinition.createExtensionChildName(childDef);
655                                } else {
656                                        childDef = extDef.getChildElementDefinitionByDatatype(value.getClass());
657                                        if (childDef == null) {
658                                                throw new ConfigurationException(Msg.code(1856) + "Unable to encode extension, unrecognized child element type: " + value.getClass().getCanonicalName());
659                                        }
660                                }
661                                encodeChildElementToStreamWriter(theResource, theEventWriter, extDef, value, childName, childDef, null, theIncludedResource, null, theEncodeContext);
662                        }
663
664                        // child extensions
665                        encodeExtensionsIfPresent(theResource, theEventWriter, next, theIncludedResource, theEncodeContext);
666
667                        theEventWriter.writeEndElement();
668
669                        writeCommentsPost(theEventWriter, next);
670
671                }
672        }
673
674
675        private void encodeXhtml(XhtmlDt theDt, XMLStreamWriter theEventWriter) throws XMLStreamException {
676                if (theDt == null || theDt.getValue() == null) {
677                        return;
678                }
679
680                List<XMLEvent> events = XmlUtil.parse(theDt.getValue());
681                boolean firstElement = true;
682
683                for (XMLEvent event : events) {
684                        switch (event.getEventType()) {
685                                case XMLStreamConstants.ATTRIBUTE:
686                                        Attribute attr = (Attribute) event;
687                                        if (isBlank(attr.getName().getPrefix())) {
688                                                if (isBlank(attr.getName().getNamespaceURI())) {
689                                                        theEventWriter.writeAttribute(attr.getName().getLocalPart(), attr.getValue());
690                                                } else {
691                                                        theEventWriter.writeAttribute(attr.getName().getNamespaceURI(), attr.getName().getLocalPart(), attr.getValue());
692                                                }
693                                        } else {
694                                                theEventWriter.writeAttribute(attr.getName().getPrefix(), attr.getName().getNamespaceURI(), attr.getName().getLocalPart(), attr.getValue());
695                                        }
696
697                                        break;
698                                case XMLStreamConstants.CDATA:
699                                        theEventWriter.writeCData(((Characters) event).getData());
700                                        break;
701                                case XMLStreamConstants.CHARACTERS:
702                                case XMLStreamConstants.SPACE:
703                                        String data = ((Characters) event).getData();
704                                        theEventWriter.writeCharacters(data);
705                                        break;
706                                case XMLStreamConstants.COMMENT:
707                                        theEventWriter.writeComment(((Comment) event).getText());
708                                        break;
709                                case XMLStreamConstants.END_ELEMENT:
710                                        theEventWriter.writeEndElement();
711                                        break;
712                                case XMLStreamConstants.ENTITY_REFERENCE:
713                                        EntityReference er = (EntityReference) event;
714                                        theEventWriter.writeEntityRef(er.getName());
715                                        break;
716                                case XMLStreamConstants.NAMESPACE:
717                                        Namespace ns = (Namespace) event;
718                                        theEventWriter.writeNamespace(ns.getPrefix(), ns.getNamespaceURI());
719                                        break;
720                                case XMLStreamConstants.START_ELEMENT:
721                                        StartElement se = event.asStartElement();
722                                        if (firstElement) {
723                                                if (StringUtils.isBlank(se.getName().getPrefix())) {
724                                                        String namespaceURI = se.getName().getNamespaceURI();
725                                                        if (StringUtils.isBlank(namespaceURI)) {
726                                                                namespaceURI = "http://www.w3.org/1999/xhtml";
727                                                        }
728                                                        theEventWriter.writeStartElement(se.getName().getLocalPart());
729                                                        theEventWriter.writeDefaultNamespace(namespaceURI);
730                                                } else {
731                                                        String prefix = se.getName().getPrefix();
732                                                        String namespaceURI = se.getName().getNamespaceURI();
733                                                        theEventWriter.writeStartElement(prefix, se.getName().getLocalPart(), namespaceURI);
734                                                        theEventWriter.writeNamespace(prefix, namespaceURI);
735                                                }
736                                                firstElement = false;
737                                        } else {
738                                                if (isBlank(se.getName().getPrefix())) {
739                                                        if (isBlank(se.getName().getNamespaceURI())) {
740                                                                theEventWriter.writeStartElement(se.getName().getLocalPart());
741                                                        } else {
742                                                                if (StringUtils.isBlank(se.getName().getPrefix())) {
743                                                                        theEventWriter.writeStartElement(se.getName().getLocalPart());
744                                                                        // theEventWriter.writeDefaultNamespace(se.getName().getNamespaceURI());
745                                                                } else {
746                                                                        theEventWriter.writeStartElement(se.getName().getNamespaceURI(), se.getName().getLocalPart());
747                                                                }
748                                                        }
749                                                } else {
750                                                        theEventWriter.writeStartElement(se.getName().getPrefix(), se.getName().getLocalPart(), se.getName().getNamespaceURI());
751                                                }
752                                        }
753                                        for (Iterator<?> attrIter = se.getAttributes(); attrIter.hasNext(); ) {
754                                                Attribute next = (Attribute) attrIter.next();
755                                                if (isBlank(next.getName().getNamespaceURI())) {
756                                                        theEventWriter.writeAttribute(next.getName().getLocalPart(), next.getValue());
757                                                } else {
758                                                        theEventWriter.writeAttribute(next.getName().getPrefix(), next.getName().getNamespaceURI(), next.getName().getLocalPart(), next.getValue());
759                                                }
760                                        }
761                                        break;
762                                case XMLStreamConstants.DTD:
763                                case XMLStreamConstants.END_DOCUMENT:
764                                case XMLStreamConstants.ENTITY_DECLARATION:
765                                case XMLStreamConstants.NOTATION_DECLARATION:
766                                case XMLStreamConstants.PROCESSING_INSTRUCTION:
767                                case XMLStreamConstants.START_DOCUMENT:
768                                        break;
769                        }
770
771                }
772        }
773
774        @Override
775        public EncodingEnum getEncoding() {
776                return EncodingEnum.XML;
777        }
778
779        private <T extends IBaseResource> T parseResource(Class<T> theResourceType, XMLEventReader theStreamReader) {
780                ParserState<T> parserState = ParserState.getPreResourceInstance(this, theResourceType, getContext(), false, getErrorHandler());
781                return doXmlLoop(theStreamReader, parserState);
782        }
783
784        @Override
785        public IParser setPrettyPrint(boolean thePrettyPrint) {
786                myPrettyPrint = thePrettyPrint;
787                return this;
788        }
789
790        /**
791         * This is just to work around the fact that casting java.util.List<ca.uhn.fhir.model.api.ExtensionDt> to
792         * java.util.List<? extends org.hl7.fhir.instance.model.api.IBaseExtension<?, ?>> seems to be
793         * rejected by the compiler some of the time.
794         */
795        private <Q extends IBaseExtension<?, ?>> List<IBaseExtension<?, ?>> toBaseExtensionList(final List<Q> theList) {
796                List<IBaseExtension<?, ?>> retVal = new ArrayList<IBaseExtension<?, ?>>(theList.size());
797                retVal.addAll(theList);
798                return retVal;
799        }
800
801        private void writeCommentsPost(XMLStreamWriter theEventWriter, IBase theElement) throws XMLStreamException {
802                if (theElement != null && theElement.hasFormatComment()) {
803                        for (String next : theElement.getFormatCommentsPost()) {
804                                if (isNotBlank(next)) {
805                                        theEventWriter.writeComment(next);
806                                }
807                        }
808                }
809        }
810
811        private void writeCommentsPre(XMLStreamWriter theEventWriter, IBase theElement) throws XMLStreamException {
812                if (theElement != null && theElement.hasFormatComment()) {
813                        for (String next : theElement.getFormatCommentsPre()) {
814                                if (isNotBlank(next)) {
815                                        theEventWriter.writeComment(next);
816                                }
817                        }
818                }
819        }
820
821        private void writeOptionalTagWithValue(XMLStreamWriter theEventWriter, String theName, String theValue) throws XMLStreamException {
822                if (StringUtils.isNotBlank(theValue)) {
823                        theEventWriter.writeStartElement(theName);
824                        theEventWriter.writeAttribute("value", theValue);
825                        theEventWriter.writeEndElement();
826                }
827        }
828
829}