private void writeCategories(XMLStreamWriter eventWriter, TagList categories) throws XMLStreamException { if (categories != null) { for (Tag next : categories) { eventWriter.writeStartElement("category"); eventWriter.writeAttribute("term", defaultString(next.getTerm())); eventWriter.writeAttribute("label", defaultString(next.getLabel())); eventWriter.writeAttribute("scheme", defaultString(next.getScheme())); eventWriter.writeEndElement(); } } }
@Override public void encodeTagListToWriter(TagList theTagList, Writer theWriter) throws IOException { try { XMLStreamWriter eventWriter = createXmlWriter(theWriter); eventWriter.writeStartElement(TagList.ELEMENT_NAME_LC); eventWriter.writeDefaultNamespace(FHIR_NS); for (Tag next : theTagList) { eventWriter.writeStartElement(TagList.ATTR_CATEGORY); if (isNotBlank(next.getTerm())) { eventWriter.writeAttribute(Tag.ATTR_TERM, next.getTerm()); } if (isNotBlank(next.getLabel())) { eventWriter.writeAttribute(Tag.ATTR_LABEL, next.getLabel()); } if (isNotBlank(next.getScheme())) { eventWriter.writeAttribute(Tag.ATTR_SCHEME, next.getScheme()); } eventWriter.writeEndElement(); } eventWriter.writeEndElement(); eventWriter.close(); } catch (XMLStreamException e) { throw new ConfigurationException("Failed to initialize STaX event factory", e); } }
private void encodeResourceToXmlStreamWriter( IBaseResource theResource, XMLStreamWriter theEventWriter, boolean theContainedResource, String theResourceId) throws XMLStreamException { if (!theContainedResource) { super.containResourcesForEncoding(theResource); } RuntimeResourceDefinition resDef = myContext.getResourceDefinition(theResource); if (resDef == null) { throw new ConfigurationException("Unknown resource type: " + theResource.getClass()); } theEventWriter.writeStartElement(resDef.getName()); theEventWriter.writeDefaultNamespace(FHIR_NS); if (theResource instanceof IAnyResource) { // HL7.org Structures encodeCompositeElementToStreamWriter( theResource, theResource, theEventWriter, resDef, theContainedResource); } else { if (myContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU1)) { // DSTU2+ IResource resource = (IResource) theResource; writeOptionalTagWithValue(theEventWriter, "id", theResourceId); InstantDt updated = (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED); IdDt resourceId = resource.getId(); String versionIdPart = resourceId.getVersionIdPart(); if (isBlank(versionIdPart)) { versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource); } List<BaseCodingDt> securityLabels = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS); List<IdDt> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES); TagList tags = ResourceMetadataKeyEnum.TAG_LIST.get(resource); if (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, profiles) == false) { theEventWriter.writeStartElement("meta"); writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart); if (updated != null) { writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString()); } for (IdDt profile : profiles) { theEventWriter.writeStartElement("profile"); theEventWriter.writeAttribute("value", profile.getValue()); theEventWriter.writeEndElement(); } for (BaseCodingDt securityLabel : securityLabels) { theEventWriter.writeStartElement("security"); BaseRuntimeElementCompositeDefinition<?> def = (BaseRuntimeElementCompositeDefinition<?>) myContext.getElementDefinition(securityLabel.getClass()); encodeCompositeElementChildrenToStreamWriter( resource, securityLabel, theEventWriter, def.getChildren(), theContainedResource); theEventWriter.writeEndElement(); } if (tags != null) { for (Tag tag : tags) { theEventWriter.writeStartElement("tag"); writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme()); writeOptionalTagWithValue(theEventWriter, "code", tag.getTerm()); writeOptionalTagWithValue(theEventWriter, "display", tag.getLabel()); theEventWriter.writeEndElement(); } } theEventWriter.writeEndElement(); } if (theResource instanceof IBaseBinary) { IBaseBinary bin = (IBaseBinary) theResource; writeOptionalTagWithValue(theEventWriter, "contentType", bin.getContentType()); writeOptionalTagWithValue(theEventWriter, "content", bin.getContentAsBase64()); } else { encodeResourceToStreamWriterInDstu2Format( resDef, theResource, theResource, theEventWriter, resDef, theContainedResource); } } else { // DSTU1 if (theResourceId != null && theContainedResource) { theEventWriter.writeAttribute("id", theResourceId); } if (theResource instanceof IBaseBinary) { IBaseBinary bin = (IBaseBinary) theResource; if (bin.getContentType() != null) { theEventWriter.writeAttribute("contentType", bin.getContentType()); } theEventWriter.writeCharacters(bin.getContentAsBase64()); } else { encodeCompositeElementToStreamWriter( theResource, theResource, theEventWriter, resDef, theContainedResource); } } } theEventWriter.writeEndElement(); }