Remove redundant <meta/> element from XML resource encoding

This commit is contained in:
jamesagnew 2015-01-10 11:18:42 -05:00
parent 8f8f9ed308
commit 6dfb5d03e4
2 changed files with 59 additions and 38 deletions

View File

@ -101,7 +101,8 @@ public class XmlParser extends BaseParser implements IParser {
private boolean myPrettyPrint; private boolean myPrettyPrint;
/** /**
* Do not use this constructor, the recommended way to obtain a new instance of the XML parser is to invoke {@link FhirContext#newXmlParser()}. * Do not use this constructor, the recommended way to obtain a new instance of the XML parser is to invoke
* {@link FhirContext#newXmlParser()}.
*/ */
public XmlParser(FhirContext theContext) { public XmlParser(FhirContext theContext) {
super(theContext); super(theContext);
@ -245,13 +246,16 @@ public class XmlParser extends BaseParser implements IParser {
writeOptionalTagWithValue(theEventWriter, "id", theBundle.getId().getIdPart()); writeOptionalTagWithValue(theEventWriter, "id", theBundle.getId().getIdPart());
theEventWriter.writeStartElement("meta");
writeOptionalTagWithValue(theEventWriter, "versionId", theBundle.getId().getVersionIdPart());
InstantDt updated = (InstantDt) theBundle.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED); InstantDt updated = (InstantDt) theBundle.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
if (updated != null) { IdDt bundleId = theBundle.getId();
writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString()); if (bundleId != null && isNotBlank(bundleId.getVersionIdPart()) || (updated != null && !updated.isEmpty())) {
theEventWriter.writeStartElement("meta");
writeOptionalTagWithValue(theEventWriter, "versionId", bundleId.getVersionIdPart());
if (updated != null) {
writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString());
}
theEventWriter.writeEndElement();
} }
theEventWriter.writeEndElement();
String bundleBaseUrl = theBundle.getLinkBase().getValue(); String bundleBaseUrl = theBundle.getLinkBase().getValue();
@ -525,8 +529,8 @@ public class XmlParser extends BaseParser implements IParser {
} }
} }
private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theEventWriter, IBase nextValue, String childName, private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theEventWriter, IBase nextValue, String childName, BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl, boolean theIncludedResource)
BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl, boolean theIncludedResource) throws XMLStreamException, DataFormatException { throws XMLStreamException, DataFormatException {
if (nextValue.isEmpty()) { if (nextValue.isEmpty()) {
if (childDef.getChildType() == ChildTypeEnum.CONTAINED_RESOURCES && getContainedResources().isEmpty() == false && theIncludedResource == false) { if (childDef.getChildType() == ChildTypeEnum.CONTAINED_RESOURCES && getContainedResources().isEmpty() == false && theIncludedResource == false) {
// We still want to go in.. // We still want to go in..
@ -601,8 +605,8 @@ public class XmlParser extends BaseParser implements IParser {
} }
private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, List<? extends BaseRuntimeChildDefinition> children, boolean theIncludedResource) throws XMLStreamException,
List<? extends BaseRuntimeChildDefinition> children, boolean theIncludedResource) throws XMLStreamException, DataFormatException { DataFormatException {
for (BaseRuntimeChildDefinition nextChild : children) { for (BaseRuntimeChildDefinition nextChild : children) {
if (nextChild instanceof RuntimeChildNarrativeDefinition && !theIncludedResource) { if (nextChild instanceof RuntimeChildNarrativeDefinition && !theIncludedResource) {
INarrativeGenerator gen = myContext.getNarrativeGenerator(); INarrativeGenerator gen = myContext.getNarrativeGenerator();
@ -676,12 +680,10 @@ public class XmlParser extends BaseParser implements IParser {
} }
} }
private void encodeResourceToStreamWriterInDstu2Format(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, private void encodeResourceToStreamWriterInDstu2Format(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
/* /*
* DSTU2 requires extensions to come in a specific spot within the encoded content - * DSTU2 requires extensions to come in a specific spot within the encoded content - This is a bit of a messy
* This is a bit of a messy way to make that happen, but hopefully this won't * way to make that happen, but hopefully this won't matter as much once we use the HL7 structures
* matter as much once we use the HL7 structures
*/ */
List<BaseRuntimeChildDefinition> preExtensionChildren = new ArrayList<BaseRuntimeChildDefinition>(); List<BaseRuntimeChildDefinition> preExtensionChildren = new ArrayList<BaseRuntimeChildDefinition>();
@ -705,15 +707,13 @@ public class XmlParser extends BaseParser implements IParser {
} }
private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
encodeExtensionsIfPresent(theResDef, theResource, theEventWriter, theElement, theIncludedResource); encodeExtensionsIfPresent(theResDef, theResource, theEventWriter, theElement, theIncludedResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getExtensions(), theIncludedResource); encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getExtensions(), theIncludedResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getChildren(), theIncludedResource); encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getChildren(), theIncludedResource);
} }
private void encodeExtensionsIfPresent(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theWriter, IBase theElement, boolean theIncludedResource) private void encodeExtensionsIfPresent(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theWriter, IBase theElement, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
throws XMLStreamException, DataFormatException {
if (theElement instanceof ISupportsUndeclaredExtensions) { if (theElement instanceof ISupportsUndeclaredExtensions) {
ISupportsUndeclaredExtensions res = (ISupportsUndeclaredExtensions) theElement; ISupportsUndeclaredExtensions res = (ISupportsUndeclaredExtensions) theElement;
encodeUndeclaredExtensions(theResDef, theResource, theWriter, res.getUndeclaredExtensions(), "extension", theIncludedResource); encodeUndeclaredExtensions(theResDef, theResource, theWriter, res.getUndeclaredExtensions(), "extension", theIncludedResource);
@ -774,13 +774,16 @@ public class XmlParser extends BaseParser implements IParser {
IResource resource = (IResource) theResource; IResource resource = (IResource) theResource;
writeOptionalTagWithValue(theEventWriter, "id", resource.getId().getIdPart()); writeOptionalTagWithValue(theEventWriter, "id", resource.getId().getIdPart());
theEventWriter.writeStartElement("meta");
writeOptionalTagWithValue(theEventWriter, "versionId", resource.getId().getVersionIdPart());
InstantDt updated = (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED); InstantDt updated = (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
if (updated != null) { IdDt resourceId = resource.getId();
writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString()); if (resourceId != null && isNotBlank(resourceId.getVersionIdPart()) || (updated != null && !updated.isEmpty())) {
theEventWriter.writeStartElement("meta");
writeOptionalTagWithValue(theEventWriter, "versionId", resourceId.getVersionIdPart());
if (updated != null) {
writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString());
}
theEventWriter.writeEndElement();
} }
theEventWriter.writeEndElement();
} else { } else {
@ -815,22 +818,24 @@ public class XmlParser extends BaseParser implements IParser {
theEventWriter.writeEndElement(); theEventWriter.writeEndElement();
} }
private void encodeUndeclaredExtensions(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theWriter, List<ExtensionDt> theExtensions, String tagName, private void encodeUndeclaredExtensions(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theWriter, List<ExtensionDt> theExtensions, String tagName, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
boolean theIncludedResource) throws XMLStreamException, DataFormatException {
for (ExtensionDt next : theExtensions) { for (ExtensionDt next : theExtensions) {
theWriter.writeStartElement(tagName); theWriter.writeStartElement(tagName);
theWriter.writeAttribute("url", next.getUrl().getValue()); theWriter.writeAttribute("url", next.getUrl().getValue());
if (next.getValue() != null) { if (next.getValue() != null) {
IElement value = next.getValue(); IElement value = next.getValue();
// RuntimeChildUndeclaredExtensionDefinition extDef = myContext.getRuntimeChildUndeclaredExtensionDefinition(); // RuntimeChildUndeclaredExtensionDefinition extDef =
// String childName = extDef.getChildNameByDatatype(nextValue.getClass()); // myContext.getRuntimeChildUndeclaredExtensionDefinition();
// if (childName == null) { // String childName = extDef.getChildNameByDatatype(nextValue.getClass());
// throw new ConfigurationException("Unable to encode extension, unregognized child element type: " + nextValue.getClass().getCanonicalName()); // if (childName == null) {
// } // throw new ConfigurationException("Unable to encode extension, unregognized child element type: " +
// BaseRuntimeElementDefinition<?> childDef = extDef.getChildElementDefinitionByDatatype(nextValue.getClass()); // nextValue.getClass().getCanonicalName());
// // }
// // BaseRuntimeElementDefinition<?> childDef =
// extDef.getChildElementDefinitionByDatatype(nextValue.getClass());
//
//
RuntimeChildUndeclaredExtensionDefinition extDef = myContext.getRuntimeChildUndeclaredExtensionDefinition(); RuntimeChildUndeclaredExtensionDefinition extDef = myContext.getRuntimeChildUndeclaredExtensionDefinition();
String childName = extDef.getChildNameByDatatype(value.getClass()); String childName = extDef.getChildNameByDatatype(value.getClass());
BaseRuntimeElementDefinition<?> childDef; BaseRuntimeElementDefinition<?> childDef;

View File

@ -14,6 +14,8 @@ import org.junit.Test;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.dev.composite.DurationDt;
import ca.uhn.fhir.model.dev.resource.Encounter;
import ca.uhn.fhir.model.dev.resource.MedicationPrescription; import ca.uhn.fhir.model.dev.resource.MedicationPrescription;
import ca.uhn.fhir.model.dev.resource.Organization; import ca.uhn.fhir.model.dev.resource.Organization;
import ca.uhn.fhir.model.dstu.resource.Binary; import ca.uhn.fhir.model.dstu.resource.Binary;
@ -31,6 +33,20 @@ public class XmlParserTest {
XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreWhitespace(true);
} }
@Test
public void testDuration() {
Encounter enc = new Encounter();
DurationDt duration = new DurationDt();
duration.setUnits("day").setValue(123L);
enc.setLength(duration);
String str = ourCtx.newXmlParser().encodeResourceToString(enc);
ourLog.info(str);
assertThat(str, not(containsString("meta")));
assertThat(str, containsString("<length><value value=\"123\"/><units value=\"day\"/></length>"));
}
@Test @Test
public void testParseBundleWithBinary() { public void testParseBundleWithBinary() {
// TODO: implement this test, make sure we handle ID and meta correctly in Binary // TODO: implement this test, make sure we handle ID and meta correctly in Binary