XMLParser allows passing a schema location

This commit is contained in:
Grahame Grieve 2021-05-05 12:17:02 +10:00
parent 6da83fa41e
commit b13b4932f9
7 changed files with 73 additions and 5 deletions

View File

@ -2,3 +2,4 @@
* Fix compartment definitions of ListResource.source and subject for R3 and R4
* Snapshot generator: fix problem checking types on logical models
* Do not flag internal references as suspicious
* XMLParser allows passing a schema location

View File

@ -329,6 +329,7 @@ public class ProfileUtilities extends TranslatingUtilities {
private String defWebRoot;
private boolean autoFixSliceNames;
private XVerExtensionManager xver;
private boolean wantFixDifferentialFirstElementType;
public ProfileUtilities(IWorkerContext context, List<ValidationMessage> messages, ProfileKnowledgeProvider pkp, FHIRPathEngine fpe) {
super();
@ -364,7 +365,13 @@ public class ProfileUtilities extends TranslatingUtilities {
this.igmode = igmode;
}
public boolean isWantFixDifferentialFirstElementType() {
return wantFixDifferentialFirstElementType;
}
public void setWantFixDifferentialFirstElementType(boolean wantFixDifferentialFirstElementType) {
this.wantFixDifferentialFirstElementType = wantFixDifferentialFirstElementType;
}
public boolean isAutoFixSliceNames() {
return autoFixSliceNames;
@ -772,7 +779,7 @@ public class ProfileUtilities extends TranslatingUtilities {
public void checkDifferentialBaseType(StructureDefinition derived) throws Error {
if (derived.hasDifferential() && !derived.getDifferential().getElementFirstRep().getPath().contains(".") && !derived.getDifferential().getElementFirstRep().getType().isEmpty()) {
if (typeMatchesAncestor(derived.getDifferential().getElementFirstRep().getType(), derived.getBaseDefinition())) {
if (wantFixDifferentialFirstElementType && typeMatchesAncestor(derived.getDifferential().getElementFirstRep().getType(), derived.getBaseDefinition())) {
derived.getDifferential().getElementFirstRep().getType().clear();
} else {
throw new Error(context.formatMessage(I18nConstants.TYPE_ON_FIRST_DIFFERENTIAL_ELEMENT));

View File

@ -87,6 +87,16 @@ public class XmlParser extends ParserBase {
super(context);
}
private String schemaPath;
public String getSchemaPath() {
return schemaPath;
}
public void setSchemaPath(String schemaPath) {
this.schemaPath = schemaPath;
}
public boolean isAllowXsiLocation() {
return allowXsiLocation;
@ -597,6 +607,9 @@ public class XmlParser extends ParserBase {
public void compose(Element e, IXMLWriter xml) throws Exception {
xml.start();
xml.setDefaultNamespace(e.getProperty().getXmlNamespace());
if (schemaPath != null) {
xml.setSchemaLocation(FormatUtilities.FHIR_NS, Utilities.pathURL(schemaPath, e.fhirType()+".xsd"));
}
composeElement(xml, e, e.getType(), true);
xml.end();
}

View File

@ -100,6 +100,7 @@ public abstract class XmlParserBase extends ParserBase implements IParser {
return ParserType.XML;
}
// -- in descendent generated code --------------------------------------
abstract protected Resource parseResource(XmlPullParser xpp) throws XmlPullParserException, IOException, FHIRFormatError ;
@ -268,6 +269,14 @@ public abstract class XmlParserBase extends ParserBase implements IParser {
protected IXMLWriter xml;
protected boolean htmlPretty;
private String schemaPath;
public String getSchemaPath() {
return schemaPath;
}
public void setSchemaPath(String schemaPath) {
this.schemaPath = schemaPath;
}
@ -382,6 +391,9 @@ public abstract class XmlParserBase extends ParserBase implements IParser {
this.htmlPretty = htmlPretty;
xml = writer;
xml.setDefaultNamespace(FHIR_NS);
if (schemaPath != null) {
xml.setSchemaLocation(FHIR_NS, Utilities.pathURL(schemaPath, resource.fhirType()+".xsd"));
}
composeResource(resource);
}

View File

@ -2,6 +2,10 @@ package org.hl7.fhir.r5.test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.formats.XmlParser;
import org.hl7.fhir.r5.model.CapabilityStatement;
import org.hl7.fhir.r5.model.CodeSystem;
import org.hl7.fhir.r5.model.CompartmentDefinition;
@ -12,6 +16,7 @@ import org.hl7.fhir.r5.model.ImplementationGuide;
import org.hl7.fhir.r5.model.MessageDefinition;
import org.hl7.fhir.r5.model.NamingSystem;
import org.hl7.fhir.r5.model.OperationDefinition;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.SearchParameter;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.StructureMap;
@ -40,4 +45,27 @@ class ResourceTests {
assertFalse(new GraphDefinition().supportsCopyright());
}
private String SRC = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n"+
"<Patient xmlns=\"http://hl7.org/fhir\">\r\n"+
" <name>\r\n"+
" <text value=\"Job Bloggs\"/>\r\n"+
" </name>\r\n"+
"</Patient>\r\n";
private String TGT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<Patient xmlns=\"http://hl7.org/fhir\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://hl7.org/fhir http://test.org/Patient.xsd\">"+
"<name>"+
"<text value=\"Job Bloggs\"/>"+
"</name>"+
"</Patient>";
@Test
void testSchemaLocation() throws IOException {
XmlParser xml = new XmlParser();
xml.setSchemaPath("http://test.org");
xml.setOutputStyle(OutputStyle.NORMAL);
Resource res = xml.parse(SRC);
String output = xml.composeString(res);
assertEquals(TGT, output);
}
}

View File

@ -60,6 +60,7 @@ public interface IXMLWriter {
public abstract void comment(String comment, boolean doPretty) throws IOException;
public abstract void decorate(ElementDecoration decoration) throws IOException;
public abstract void setSchemaLocation(String ns, String loc) throws IOException;
public abstract void enter(String name) throws IOException;
public abstract void enter(String namespace, String name) throws IOException;

View File

@ -900,6 +900,12 @@ public class XMLWriter extends OutputStreamWriter implements IXMLWriter {
public void decorate(ElementDecoration element) throws IOException {
// nothing...
}
@Override
public void setSchemaLocation(String ns, String loc) throws IOException {
namespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");
attribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", ns+" "+loc);
}
}