fix up CDA tests, and add support for http://hl7.org/fhir/StructureDefinition/elementdefinition-xml-name for CDA
This commit is contained in:
parent
4af4925657
commit
b4fc944afc
|
@ -260,7 +260,7 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
|
|||
}
|
||||
|
||||
public void loadFromFile(InputStream stream, String name, IContextResourceLoader loader) throws IOException, FHIRException {
|
||||
loadFromFile(stream, name, null);
|
||||
loadFromFile(stream, name, loader, null);
|
||||
}
|
||||
|
||||
public void loadFromFile(InputStream stream, String name, IContextResourceLoader loader, ILoadFilter filter) throws IOException, FHIRException {
|
||||
|
|
|
@ -68,6 +68,24 @@ public class Property {
|
|||
return definition.getPath().substring(definition.getPath().lastIndexOf(".")+1);
|
||||
}
|
||||
|
||||
public String getXmlName() {
|
||||
if (definition.hasExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-xml-name")) {
|
||||
return ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-xml-name");
|
||||
} else {
|
||||
return getName();
|
||||
}
|
||||
}
|
||||
|
||||
public String getXmlNamespace() {
|
||||
if (ToolingExtensions.hasExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace")) {
|
||||
return ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
|
||||
} else if (ToolingExtensions.hasExtension(structure, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace")) {
|
||||
return ToolingExtensions.readStringExtension(structure, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
|
||||
} else {
|
||||
return FormatUtilities.FHIR_NS;
|
||||
}
|
||||
}
|
||||
|
||||
public ElementDefinition getDefinition() {
|
||||
return definition;
|
||||
}
|
||||
|
@ -199,14 +217,6 @@ public class Property {
|
|||
return definition.getBase().getPath();
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
if (ToolingExtensions.hasExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))
|
||||
return ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
|
||||
if (ToolingExtensions.hasExtension(structure, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))
|
||||
return ToolingExtensions.readStringExtension(structure, "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
|
||||
return FormatUtilities.FHIR_NS;
|
||||
}
|
||||
|
||||
private boolean isElementWithOnlyExtension(final ElementDefinition ed, final List<ElementDefinition> children) {
|
||||
boolean result = false;
|
||||
if (!ed.getType().isEmpty()) {
|
||||
|
|
|
@ -235,11 +235,7 @@ public class XmlParser extends ParserBase {
|
|||
if (policy == ValidationPolicy.EVERYTHING) {
|
||||
if (empty(element) && FormatUtilities.FHIR_NS.equals(element.getNamespaceURI())) // this rule only applies to FHIR Content
|
||||
logError(line(element), col(element), path, IssueType.INVALID, context.formatMessage(I18nConstants.ELEMENT_MUST_HAVE_SOME_CONTENT), IssueSeverity.ERROR);
|
||||
String ns = FormatUtilities.FHIR_NS;
|
||||
if (ToolingExtensions.hasExtension(prop.getDefinition(), "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))
|
||||
ns = ToolingExtensions.readStringExtension(prop.getDefinition(), "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
|
||||
else if (ToolingExtensions.hasExtension(prop.getStructure(), "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace"))
|
||||
ns = ToolingExtensions.readStringExtension(prop.getStructure(), "http://hl7.org/fhir/StructureDefinition/elementdefinition-namespace");
|
||||
String ns = prop.getXmlNamespace();
|
||||
if (!element.getNamespaceURI().equals(ns))
|
||||
logError(line(element), col(element), path, IssueType.INVALID, context.formatMessage(I18nConstants.WRONG_NAMESPACE__EXPECTED_, ns), IssueSeverity.ERROR);
|
||||
}
|
||||
|
@ -304,7 +300,7 @@ public class XmlParser extends ParserBase {
|
|||
logError(line, col, path, IssueType.STRUCTURE, context.formatMessage(I18nConstants.XML_ATTR_VALUE_INVALID, attr.getNodeName()), IssueSeverity.ERROR);
|
||||
}
|
||||
if (!(attr.getNodeName().equals("xmlns") || attr.getNodeName().startsWith("xmlns:"))) {
|
||||
Property property = getAttrProp(properties, attr.getNodeName());
|
||||
Property property = getAttrProp(properties, attr.getLocalName(), attr.getNamespaceURI());
|
||||
if (property != null) {
|
||||
String av = attr.getNodeValue();
|
||||
if (ToolingExtensions.hasExtension(property.getDefinition(), "http://www.healthintersections.com.au/fhir/StructureDefinition/elementdefinition-dateformat"))
|
||||
|
@ -331,7 +327,7 @@ public class XmlParser extends ParserBase {
|
|||
Node child = node.getFirstChild();
|
||||
while (child != null) {
|
||||
if (child.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Property property = getElementProp(properties, child.getLocalName());
|
||||
Property property = getElementProp(properties, child.getLocalName(), child.getNamespaceURI());
|
||||
if (property != null) {
|
||||
if (!property.isChoice() && "xhtml".equals(property.getType())) {
|
||||
XhtmlNode xhtml;
|
||||
|
@ -401,7 +397,7 @@ public class XmlParser extends ParserBase {
|
|||
}
|
||||
|
||||
|
||||
private Property getElementProp(List<Property> properties, String nodeName) {
|
||||
private Property getElementProp(List<Property> properties, String nodeName, String namespace) {
|
||||
List<Property> propsSortedByLongestFirst = new ArrayList<Property>(properties);
|
||||
// sort properties according to their name longest first, so .requestOrganizationReference comes first before .request[x]
|
||||
// and therefore the longer property names get evaluated first
|
||||
|
@ -411,22 +407,39 @@ public class XmlParser extends ParserBase {
|
|||
return o2.getName().length() - o1.getName().length();
|
||||
}
|
||||
});
|
||||
for (Property p : propsSortedByLongestFirst)
|
||||
if (!p.getDefinition().hasRepresentation(PropertyRepresentation.XMLATTR) && !p.getDefinition().hasRepresentation(
|
||||
PropertyRepresentation.XMLTEXT)) {
|
||||
if (p.getName().equals(nodeName))
|
||||
return p;
|
||||
if (p.getName().endsWith("[x]") && nodeName.length() > p.getName().length()-3 && p.getName().substring(0, p.getName().length()-3).equals(nodeName.substring(0, p.getName().length()-3)))
|
||||
return p;
|
||||
}
|
||||
// first scan, by namespace
|
||||
for (Property p : propsSortedByLongestFirst) {
|
||||
if (!p.getDefinition().hasRepresentation(PropertyRepresentation.XMLATTR) && !p.getDefinition().hasRepresentation(PropertyRepresentation.XMLTEXT)) {
|
||||
if (p.getXmlName().equals(nodeName) && p.getXmlNamespace().equals(namespace))
|
||||
return p;
|
||||
}
|
||||
}
|
||||
if (namespace == null || Utilities.existsInList(namespace, "http://www.w3.org/1999/xhtml", "http://hl7.org/fhir")) {
|
||||
for (Property p : propsSortedByLongestFirst) {
|
||||
if (!p.getDefinition().hasRepresentation(PropertyRepresentation.XMLATTR) && !p.getDefinition().hasRepresentation(PropertyRepresentation.XMLTEXT)) {
|
||||
if (p.getXmlName().equals(nodeName))
|
||||
return p;
|
||||
if (p.getName().endsWith("[x]") && nodeName.length() > p.getName().length()-3 && p.getName().substring(0, p.getName().length()-3).equals(nodeName.substring(0, p.getName().length()-3)))
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Property getAttrProp(List<Property> properties, String nodeName) {
|
||||
for (Property p : properties)
|
||||
if (p.getName().equals(nodeName) && p.getDefinition().hasRepresentation(
|
||||
PropertyRepresentation.XMLATTR))
|
||||
return p;
|
||||
private Property getAttrProp(List<Property> properties, String nodeName, String namespace) {
|
||||
for (Property p : properties) {
|
||||
if (p.getXmlName().equals(nodeName) && p.getDefinition().hasRepresentation(PropertyRepresentation.XMLATTR) && p.getXmlNamespace().equals(namespace)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
if (namespace == null) {
|
||||
for (Property p : properties) {
|
||||
if (p.getXmlName().equals(nodeName) && p.getDefinition().hasRepresentation(PropertyRepresentation.XMLATTR)) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -524,7 +537,7 @@ public class XmlParser extends ParserBase {
|
|||
xml.setSortAttributes(false);
|
||||
xml.setPretty(style == OutputStyle.PRETTY);
|
||||
xml.start();
|
||||
xml.setDefaultNamespace(e.getProperty().getNamespace());
|
||||
xml.setDefaultNamespace(e.getProperty().getXmlNamespace());
|
||||
if (hasTypeAttr(e))
|
||||
xml.namespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");
|
||||
composeElement(xml, e, e.getType(), true);
|
||||
|
@ -545,7 +558,7 @@ public class XmlParser extends ParserBase {
|
|||
|
||||
public void compose(Element e, IXMLWriter xml) throws Exception {
|
||||
xml.start();
|
||||
xml.setDefaultNamespace(e.getProperty().getNamespace());
|
||||
xml.setDefaultNamespace(e.getProperty().getXmlNamespace());
|
||||
composeElement(xml, e, e.getType(), true);
|
||||
xml.end();
|
||||
}
|
||||
|
|
|
@ -796,7 +796,7 @@ public class ValueSetRenderer extends TerminologyRenderer {
|
|||
} else {
|
||||
li.tx(f.getProperty()+" "+describe(f.getOp())+" ");
|
||||
if (e != null && codeExistsInValueSet(e, f.getValue())) {
|
||||
String href = getContext().getSpecificationLink()+getCsRef(e);
|
||||
String href = getContext().fixReference(getCsRef(e));
|
||||
if (href.contains("#"))
|
||||
href = href + "-"+Utilities.nmtokenize(f.getValue());
|
||||
else
|
||||
|
|
|
@ -174,24 +174,27 @@ public class CDARoundTripTests {
|
|||
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testSimple() throws IOException {
|
||||
PackageCacheManager pcm = new PackageCacheManager(true, ToolsVersion.TOOLS_VERSION);
|
||||
SimpleWorkerContext context = SimpleWorkerContext.fromPackage(pcm.loadPackage("hl7.fhir.r4.core", "4.0.1"));
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("r5", "cda", "any.xml"), "any.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("r5", "cda", "ii.xml"), "ii.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("r5", "cda", "cd.xml"), "cd.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("r5", "cda", "ce.xml"), "ce.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("r5", "cda", "cda.xml"), "cda.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "any.xml"), "any.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "ii.xml"), "ii.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "cd.xml"), "cd.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "ce.xml"), "ce.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "ed.xml"), "ed.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "st.xml"), "st.xml", null);
|
||||
context.loadFromFile(TestingUtilities.loadTestResourceStream("validator", "cda", "cda.xml"), "cda.xml", null);
|
||||
for (StructureDefinition sd : context.getStructures()) {
|
||||
if (!sd.hasSnapshot()) {
|
||||
System.out.println("generate snapshot for " + sd.getUrl());
|
||||
context.generateSnapshot(sd, true);
|
||||
}
|
||||
}
|
||||
Element cda = Manager.parse(context, TestingUtilities.loadTestResourceStream("r5", "cda", "example.xml"), FhirFormat.XML);
|
||||
Element cda = Manager.parse(context, TestingUtilities.loadTestResourceStream("validator", "cda", "example.xml"), FhirFormat.XML);
|
||||
FHIRPathEngine fp = new FHIRPathEngine(context);
|
||||
Assertions.assertEquals("2.16.840.1.113883.3.27.1776", fp.evaluateToString(null, cda, cda, cda, fp.parse("ClinicalDocument.templateId.root")));
|
||||
Assertions.assertEquals("SoEN", fp.evaluateToString(null, cda, cda, cda, fp.parse("ClinicalDocument.code.displayName")));
|
||||
Assertions.assertEquals("SoEN2", fp.evaluateToString(null, cda, cda, cda, fp.parse("ClinicalDocument.code.sdtcDisplayName")));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue