This commit is contained in:
jamesagnew 2022-01-16 14:40:51 -05:00 committed by James Agnew
parent f902d1d273
commit d3007ea73f
3 changed files with 4370 additions and 4378 deletions

View File

@ -30,6 +30,31 @@ package org.hl7.fhir.r5.elementmodel;
*/
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r5.conformance.ProfileUtilities;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.elementmodel.Element.SpecialElement;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.formats.JsonCreator;
import org.hl7.fhir.r5.formats.JsonCreatorCanonical;
import org.hl7.fhir.r5.formats.JsonCreatorGson;
import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.utils.FHIRPathEngine;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.i18n.I18nConstants;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import org.hl7.fhir.utilities.json.JsonTrackingParser.LocationData;
import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
import org.hl7.fhir.utilities.xhtml.XhtmlParser;
import java.io.IOException;
import java.io.InputStream;
@ -44,41 +69,8 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r5.conformance.ProfileUtilities;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.elementmodel.Element.SpecialElement;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.formats.JsonCreator;
import org.hl7.fhir.r5.formats.JsonCreatorCanonical;
import org.hl7.fhir.r5.formats.JsonCreatorGson;
import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent;
import org.hl7.fhir.r5.utils.FHIRPathEngine;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.i18n.I18nConstants;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
import org.hl7.fhir.utilities.json.JsonTrackingParser.LocationData;
import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
import org.hl7.fhir.utilities.xhtml.XhtmlParser;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.slf4j.LoggerFactory;
public class JsonParser extends ParserBase {
static {
// Helpful for troubleshooting why this class is being loaded
LoggerFactory.getLogger(JsonParser.class).debug("Loading JsonParser class");
}
private JsonCreator json;
private Map<JsonElement, LocationData> map;
private boolean allowComments;
@ -99,7 +91,7 @@ public class JsonParser extends ParserBase {
public Element parse(String source, String type) throws Exception {
JsonObject obj = (JsonObject) new com.google.gson.JsonParser().parse(source);
String path = "/"+type;
String path = "/" + type;
StructureDefinition sd = getDefinition(-1, -1, type);
if (sd == null)
return null;
@ -125,7 +117,7 @@ public class JsonParser extends ParserBase {
try {
obj = JsonTrackingParser.parse(source, map, false, allowComments);
} catch (Exception e) {
logError(-1, -1,context.formatMessage(I18nConstants.DOCUMENT), IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_, e.getMessage()), IssueSeverity.FATAL);
logError(-1, -1, context.formatMessage(I18nConstants.DOCUMENT), IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_, e.getMessage()), IssueSeverity.FATAL);
return null;
}
assert (map.containsKey(obj));
@ -213,11 +205,11 @@ public class JsonParser extends ParserBase {
public void parseChildItem(String path, JsonObject object, Element context, Set<String> processed, Property property) {
if (property.isChoice() || property.getDefinition().getPath().endsWith("data[x]")) {
for (TypeRefComponent type : property.getDefinition().getType()) {
String eName = property.getName().substring(0, property.getName().length()-3) + Utilities.capitalize(type.getWorkingCode());
String eName = property.getName().substring(0, property.getName().length() - 3) + Utilities.capitalize(type.getWorkingCode());
if (!isPrimitive(type.getWorkingCode()) && object.has(eName)) {
parseChildComplex(path, object, context, processed, property, eName);
break;
} else if (isPrimitive(type.getWorkingCode()) && (object.has(eName) || object.has("_"+eName))) {
} else if (isPrimitive(type.getWorkingCode()) && (object.has(eName) || object.has("_" + eName))) {
parseChildPrimitive(object, context, processed, property, path, eName);
break;
}
@ -231,8 +223,8 @@ public class JsonParser extends ParserBase {
private void parseChildComplex(String path, JsonObject object, Element element, Set<String> processed, Property property, String name) throws FHIRException {
processed.add(name);
String npath = path+"."+property.getName();
String fpath = element.getPath()+"."+property.getName();
String npath = path + "." + property.getName();
String fpath = element.getPath() + "." + property.getName();
JsonElement e = object.get(name);
if (property.isList() && (e instanceof JsonArray)) {
JsonArray arr = (JsonArray) e;
@ -241,7 +233,7 @@ public class JsonParser extends ParserBase {
}
int c = 0;
for (JsonElement am : arr) {
parseChildComplexInstance(npath+"["+c+"]", fpath+"["+c+"]", object, element, property, name, am);
parseChildComplexInstance(npath + "[" + c + "]", fpath + "[" + c + "]", object, element, property, name, am);
c++;
}
} else {
@ -293,12 +285,12 @@ public class JsonParser extends ParserBase {
}
private void parseChildPrimitive(JsonObject object, Element element, Set<String> processed, Property property, String path, String name) throws FHIRException {
String npath = path+"."+property.getName();
String fpath = element.getPath()+"."+property.getName();
String npath = path + "." + property.getName();
String fpath = element.getPath() + "." + property.getName();
processed.add(name);
processed.add("_"+name);
processed.add("_" + name);
JsonElement main = object.has(name) ? object.get(name) : null;
JsonElement fork = object.has("_"+name) ? object.get("_"+name) : null;
JsonElement fork = object.has("_" + name) ? object.get("_" + name) : null;
if (main != null || fork != null) {
if (property.isList()) {
boolean ok = true;
@ -406,14 +398,14 @@ public class JsonParser extends ParserBase {
}
private int line(JsonElement e) {
if (map == null|| !map.containsKey(e))
if (map == null || !map.containsKey(e))
return -1;
else
return map.get(e).getLine();
}
private int col(JsonElement e) {
if (map == null|| !map.containsKey(e))
if (map == null || !map.containsKey(e))
return -1;
else
return map.get(e).getCol();
@ -516,19 +508,19 @@ public class JsonParser extends ParserBase {
}
closeArray();
}
name = "_"+name;
name = "_" + name;
}
if (complex) {
openArray(name, linkResolver == null ? null : linkResolver.resolveProperty(list.get(0).getProperty()));
for (Element item : list) {
if (item.hasChildren()) {
open(null,null);
open(null, null);
if (item.getProperty().isResource()) {
prop("resourceType", item.getType(), linkResolver == null ? null : linkResolver.resolveType(item.getType()));
}
Set<String> done = new HashSet<String>();
for (Element child : item.getChildren()) {
compose(path+"."+name+"[]", item, done, child);
compose(path + "." + name + "[]", item, done, child);
}
close();
} else
@ -564,7 +556,7 @@ public class JsonParser extends ParserBase {
if (element.isPrimitive() || isPrimitive(element.getType())) {
if (element.hasValue())
primitiveValue(name, element);
name = "_"+name;
name = "_" + name;
if (element.getType().equals("xhtml"))
json.anchor("end-xhtml");
}
@ -575,7 +567,7 @@ public class JsonParser extends ParserBase {
}
Set<String> done = new HashSet<String>();
for (Element child : element.getChildren()) {
compose(path+"."+element.getName(), element, done, child);
compose(path + "." + element.getName(), element, done, child);
}
close();
}

View File

@ -53,7 +53,6 @@ import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r5.conformance.ProfileUtilities;
import org.hl7.fhir.r5.context.IWorkerContext;
import org.hl7.fhir.r5.elementmodel.Element.SpecialElement;
import org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement;
import org.hl7.fhir.r5.formats.FormatUtilities;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.model.DateTimeType;
@ -75,7 +74,6 @@ import org.hl7.fhir.utilities.xhtml.XhtmlParser;
import org.hl7.fhir.utilities.xml.IXMLWriter;
import org.hl7.fhir.utilities.xml.XMLUtil;
import org.hl7.fhir.utilities.xml.XMLWriter;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
@ -83,11 +81,6 @@ import org.xml.sax.XMLReader;
public class XmlParser extends ParserBase {
static {
// Helpful for troubleshooting why this class is being loaded
LoggerFactory.getLogger(XmlParser.class).debug("Loading JsonParser class");
}
private boolean allowXsiLocation;
private String version;

View File

@ -36,6 +36,7 @@ package org.hl7.fhir.r5.formats;
import org.hl7.fhir.r5.model.*;
import org.hl7.fhir.r5.model.Enumerations.FHIRVersion;
import org.slf4j.LoggerFactory;
import org.xmlpull.v1.*;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.exceptions.FHIRFormatError;
@ -44,6 +45,12 @@ import java.io.IOException;
public class XmlParser extends XmlParserBase {
static {
// Helpful for troubleshooting why this class is being loaded
LoggerFactory.getLogger(org.hl7.fhir.r5.elementmodel.XmlParser.class).debug("Loading XmlParser class");
}
public XmlParser() {
super();
}