/** * Create and return a new XML parser. * * <p> * Thread safety: <b>Parsers are not guaranteed to be thread safe</b>. Create a new parser instance for every thread * or every message being parsed/encoded. * </p> * <p> * Performance Note: <b>This method is cheap</b> to call, and may be called once for every message being processed * without incurring any performance penalty * </p> */ public IParser newXmlParser() { return new XmlParser(this, myParserErrorHandler); }
@Override public <T extends IBaseResource> T doParseResource(Class<T> theResourceType, Reader theReader) { XMLEventReader streamReader = createStreamReader(theReader); return parseResource(theResourceType, streamReader); }
@Override public void doEncodeResourceToWriter(IBaseResource theResource, Writer theWriter) throws DataFormatException { XMLStreamWriter eventWriter; try { eventWriter = createXmlWriter(theWriter); encodeResourceToXmlStreamWriter(theResource, eventWriter, false, false); eventWriter.flush(); } catch (XMLStreamException e) { throw new ConfigurationException("Failed to initialize STaX event factory", e); } }
private <T extends IBaseResource> T parseResource(Class<T> theResourceType, XMLEventReader theStreamReader) { ParserState<T> parserState = ParserState.getPreResourceInstance(this, theResourceType, myContext, false, getErrorHandler()); return doXmlLoop(theStreamReader, parserState); }
private void encodeChildElementToStreamWriter(IBaseResource theResource, XMLStreamWriter theEventWriter, IBase theElement, String childName, BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl, boolean theIncludedResource, boolean theSubResource, CompositeChildElement theParent) throws XMLStreamException, DataFormatException { if (theElement == null || theElement.isEmpty()) { if (isChildContained(childDef, theIncludedResource)) { writeCommentsPre(theEventWriter, theElement); encodeExtensionsIfPresent(theResource, theEventWriter, theElement, theIncludedResource, theSubResource); theEventWriter.writeEndElement(); if (value != null || super.hasExtensions(pd)) { theEventWriter.writeStartElement(childName); String elementId = getCompositeElementId(theElement); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); encodeExtensionsIfPresent(theResource, theEventWriter, theElement, theIncludedResource,theSubResource); theEventWriter.writeEndElement(); case COMPOSITE_DATATYPE: { theEventWriter.writeStartElement(childName); String elementId = getCompositeElementId(theElement); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); encodeCompositeElementToStreamWriter(theResource, theElement, theEventWriter, theIncludedResource, theSubResource, theParent); theEventWriter.writeEndElement(); break; for (IBaseResource next : getContainedResources().getContainedResources()) {
writeCommentsPre(theEventWriter, theResourceId); theEventWriter.writeStartElement("id"); theEventWriter.writeAttribute("value", theResourceId.getIdPart()); encodeExtensionsIfPresent(theResource, theEventWriter, theResourceId, false, false); theEventWriter.writeEndElement(); writeCommentsPost(theEventWriter, theResourceId); encodeCompositeElementToStreamWriter(theResource, theResource, theEventWriter, theContainedResource, theSubResource, new CompositeChildElement(resDef, theSubResource)); encodeExtensionsIfPresent(theResource, theEventWriter, theResourceId, false,false); theEventWriter.writeEndElement(); writeCommentsPost(theEventWriter, theResourceId); List<BaseCodingDt> securityLabels = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS); List<? extends IIdType> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES); profiles = super.getProfileTagsForEncoding(resource, profiles); TagList tags = getMetaTagsForEncoding((resource)); writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart); if (updated != null) { writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString()); encodeCompositeElementToStreamWriter(resource, securityLabel, theEventWriter, theContainedResource, theSubResource, null); theEventWriter.writeEndElement(); writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme()); writeOptionalTagWithValue(theEventWriter, "code", tag.getTerm()); writeOptionalTagWithValue(theEventWriter, "display", tag.getLabel());
writeCommentsPre(theEventWriter, next); String elementId = getCompositeElementId(next); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); String url = getExtensionUrl(next.getUrl()); theEventWriter.writeAttribute("url", url); encodeChildElementToStreamWriter(theResource, theEventWriter, value, childName, childDef, null, theIncludedResource, theSubResource, null); encodeExtensionsIfPresent(theResource, theEventWriter, next, theIncludedResource,theSubResource); writeCommentsPost(theEventWriter, next);
String childName = nextChild.getChildNameByDatatype(child.getDatatype()); BaseRuntimeElementDefinition<?> type = child.getChildByName(childName); encodeChildElementToStreamWriter(theResource, theEventWriter, narr, childName, type, null, theContainedResource, theSubResource, nextChildElem); continue; encodeChildElementToStreamWriter(theResource, theEventWriter, null, nextChild.getChildNameByDatatype(null), nextChild.getChildElementDefinitionByDatatype(null), null, theContainedResource, theSubResource, nextChildElem); } else { String extensionUrl = getExtensionUrl(nextChild.getExtensionUrl()); encodeExtension(theResource, theEventWriter, theContainedResource, theSubResource, nextChildElem, nextChild, nextValue, childName, extensionUrl, childDef); } else if (nextChild instanceof RuntimeChildExtension) { IBaseExtension<?, ?> extension = (IBaseExtension<?, ?>) nextValue; encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, getExtensionUrl(extension.getUrl()), theContainedResource, theSubResource, nextChildElem); } else if (nextChild instanceof RuntimeChildNarrativeDefinition && theContainedResource) { encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, extensionUrl, theContainedResource, theSubResource, nextChildElem);
private void encodeExtension(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theContainedResource, boolean theSubResource, CompositeChildElement nextChildElem, BaseRuntimeChildDefinition nextChild, IBase nextValue, String childName, String extensionUrl, BaseRuntimeElementDefinition<?> childDef) throws XMLStreamException { BaseRuntimeDeclaredChildDefinition extDef = (BaseRuntimeDeclaredChildDefinition) nextChild; if (extDef.isModifier()) { theEventWriter.writeStartElement("modifierExtension"); } else { theEventWriter.writeStartElement("extension"); } String elementId = getCompositeElementId(nextValue); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); } theEventWriter.writeAttribute("url", extensionUrl); encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, null, theContainedResource, theSubResource, nextChildElem); theEventWriter.writeEndElement(); }
private void encodeResourceToXmlStreamWriter(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theIncludedResource, boolean theSubResource) throws XMLStreamException, DataFormatException { IIdType resourceId = null; if (StringUtils.isNotBlank(theResource.getIdElement().getIdPart())) { resourceId = theResource.getIdElement(); if (theResource.getIdElement().getValue().startsWith("urn:")) { resourceId = null; } } if (!theIncludedResource) { if (super.shouldEncodeResourceId(theResource, theSubResource) == false) { resourceId = null; } else if (theSubResource == false && getEncodeForceResourceId() != null) { resourceId = getEncodeForceResourceId(); } } encodeResourceToXmlStreamWriter(theResource, theEventWriter, theIncludedResource, theSubResource, resourceId); }
private XMLStreamWriter createXmlWriter(Writer theWriter) throws XMLStreamException { XMLStreamWriter eventWriter; eventWriter = XmlUtil.createXmlStreamWriter(theWriter); eventWriter = decorateStreamWriter(eventWriter); return eventWriter; }
if (isChildContained(childDef, theIncludedResource)) { writeCommentsPre(theEventWriter, theElement); encodeExtensionsIfPresent(theResource, theEventWriter, theElement, theIncludedResource, theEncodeContext); theEventWriter.writeEndElement(); if (value != null || !super.hasNoExtensions(pd)) { theEventWriter.writeStartElement(childName); String elementId = getCompositeElementId(theElement); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); encodeExtensionsIfPresent(theResource, theEventWriter, theElement, theIncludedResource, theEncodeContext); theEventWriter.writeEndElement(); case COMPOSITE_DATATYPE: { theEventWriter.writeStartElement(childName); String elementId = getCompositeElementId(theElement); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); encodeCompositeElementToStreamWriter(theResource, theElement, theEventWriter, theIncludedResource, theParent, theEncodeContext); theEventWriter.writeEndElement(); break; for (IBaseResource next : getContainedResources().getContainedResources()) { IIdType resourceId = getContainedResources().getResourceId(next); theEventWriter.writeStartElement("contained");
writeCommentsPre(theEventWriter, theResourceId); theEventWriter.writeStartElement("id"); theEventWriter.writeAttribute("value", theResourceId.getIdPart()); encodeExtensionsIfPresent(theResource, theEventWriter, theResourceId, false, theEncodeContext); theEventWriter.writeEndElement(); writeCommentsPost(theEventWriter, theResourceId); encodeCompositeElementToStreamWriter(theResource, theResource, theEventWriter, theContainedResource, new CompositeChildElement(resDef, theEncodeContext), theEncodeContext); encodeExtensionsIfPresent(theResource, theEventWriter, theResourceId, false, theEncodeContext); theEventWriter.writeEndElement(); writeCommentsPost(theEventWriter, theResourceId); List<BaseCodingDt> securityLabels = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS); List<? extends IIdType> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES); profiles = super.getProfileTagsForEncoding(resource, profiles); TagList tags = getMetaTagsForEncoding((resource), theEncodeContext); if (shouldEncodePath(resource, "meta.versionId")) { writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart); if (shouldEncodePath(resource, "meta.lastUpdated")) { writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString()); encodeCompositeElementToStreamWriter(resource, securityLabel, theEventWriter, theContainedResource, null, theEncodeContext); theEventWriter.writeEndElement(); writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme());
writeCommentsPre(theEventWriter, next); String elementId = getCompositeElementId(next); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); String url = getExtensionUrl(next.getUrl()); theEventWriter.writeAttribute("url", url); encodeChildElementToStreamWriter(theResource, theEventWriter, value, childName, childDef, null, theIncludedResource, null, theEncodeContext); encodeExtensionsIfPresent(theResource, theEventWriter, next, theIncludedResource, theEncodeContext); writeCommentsPost(theEventWriter, next);
String childName = nextChild.getChildNameByDatatype(child.getDatatype()); BaseRuntimeElementDefinition<?> type = child.getChildByName(childName); encodeChildElementToStreamWriter(theResource, theEventWriter, narr, childName, type, null, theContainedResource, nextChildElem, theEncodeContext); continue; encodeChildElementToStreamWriter(theResource, theEventWriter, null, nextChild.getChildNameByDatatype(null), nextChild.getChildElementDefinitionByDatatype(null), null, theContainedResource, nextChildElem, theEncodeContext); } else { String extensionUrl = getExtensionUrl(nextChild.getExtensionUrl()); encodeExtension(theResource, theEventWriter, theContainedResource, nextChildElem, nextChild, nextValue, childName, extensionUrl, childDef, theEncodeContext); } else if (nextChild instanceof RuntimeChildExtension) { IBaseExtension<?, ?> extension = (IBaseExtension<?, ?>) nextValue; encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, getExtensionUrl(extension.getUrl()), theContainedResource, nextChildElem, theEncodeContext); } else if (nextChild instanceof RuntimeChildNarrativeDefinition && theContainedResource) { encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, extensionUrl, theContainedResource, nextChildElem, theEncodeContext);
private <T extends IBaseResource> T parseResource(Class<T> theResourceType, XMLEventReader theStreamReader) { ParserState<T> parserState = ParserState.getPreResourceInstance(this, theResourceType, myContext, false, getErrorHandler()); return doXmlLoop(theStreamReader, parserState); }
private void encodeExtension(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theContainedResource, CompositeChildElement nextChildElem, BaseRuntimeChildDefinition nextChild, IBase nextValue, String childName, String extensionUrl, BaseRuntimeElementDefinition<?> childDef, EncodeContext theEncodeContext) throws XMLStreamException { BaseRuntimeDeclaredChildDefinition extDef = (BaseRuntimeDeclaredChildDefinition) nextChild; if (extDef.isModifier()) { theEventWriter.writeStartElement("modifierExtension"); } else { theEventWriter.writeStartElement("extension"); } String elementId = getCompositeElementId(nextValue); if (isNotBlank(elementId)) { theEventWriter.writeAttribute("id", elementId); } theEventWriter.writeAttribute("url", extensionUrl); encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, null, theContainedResource, nextChildElem, theEncodeContext); theEventWriter.writeEndElement(); }
private void encodeResourceToXmlStreamWriter(IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theIncludedResource, EncodeContext theEncodeContext) throws XMLStreamException, DataFormatException { IIdType resourceId = null; if (StringUtils.isNotBlank(theResource.getIdElement().getIdPart())) { resourceId = theResource.getIdElement(); if (theResource.getIdElement().getValue().startsWith("urn:")) { resourceId = null; } } if (!theIncludedResource) { if (super.shouldEncodeResourceId(theResource, theEncodeContext) == false) { resourceId = null; } else if (theEncodeContext.getResourcePath().size() == 1 && getEncodeForceResourceId() != null) { resourceId = getEncodeForceResourceId(); } } encodeResourceToXmlStreamWriter(theResource, theEventWriter, theIncludedResource, resourceId, theEncodeContext); }
private XMLStreamWriter createXmlWriter(Writer theWriter) throws XMLStreamException { XMLStreamWriter eventWriter; eventWriter = XmlUtil.createXmlStreamWriter(theWriter); eventWriter = decorateStreamWriter(eventWriter); return eventWriter; }
@Override public void doEncodeResourceToWriter(IBaseResource theResource, Writer theWriter, EncodeContext theEncodeContext) throws DataFormatException { XMLStreamWriter eventWriter; try { eventWriter = createXmlWriter(theWriter); encodeResourceToXmlStreamWriter(theResource, eventWriter, false, theEncodeContext); eventWriter.flush(); } catch (XMLStreamException e) { throw new ConfigurationException("Failed to initialize STaX event factory", e); } }