Avoid unnecessary JsonParser/XmlParser class load

This commit is contained in:
jamesagnew 2022-01-14 16:40:16 -05:00 committed by James Agnew
parent 15fcc4607e
commit 942d4f15aa
3 changed files with 92 additions and 71 deletions

View File

@ -70,9 +70,15 @@ 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;

View File

@ -75,12 +75,19 @@ 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;
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

@ -63,6 +63,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.net.URI;
@ -117,24 +118,31 @@ public abstract class FormatUtilities {
}
public static ParserBase makeParser(FhirFormat format) {
switch (format) {
case XML : return new XmlParser();
case JSON : return new JsonParser();
case TURTLE : throw new Error("unsupported Format "+format.toString()); // return new TurtleParser();
case VBAR : throw new Error("unsupported Format "+format.toString()); //
case TEXT : throw new Error("unsupported Format "+format.toString()); //
}
throw new Error("unsupported Format "+format.toString());
return makeParser(format.name());
}
public static ParserBase makeParser(String format) {
if ("XML".equalsIgnoreCase(format)) return new XmlParser();
if ("JSON".equalsIgnoreCase(format)) return new JsonParser();
if ("TURTLE".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // return new TurtleParser();
if ("JSONLD".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // return new JsonLdParser();
if ("VBAR".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); //
if ("TEXT".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); //
throw new Error("unsupported Format "+format);
/*
* Note: In this class we're instantiating the parsers using reflection. This is because the
* XmlParser and JsonParser are huuuuuge classes and classloading them is quite expensive
* in cases where they won't actually ever be instantiated (such as when using the
* validator in HAPI FHIR)
*/
try {
if ("XML".equalsIgnoreCase(format))
return (ParserBase) Class.forName("org.hl7.fhir.r5.formats.XmlParser").getConstructor().newInstance();
if ("JSON".equalsIgnoreCase(format))
return (ParserBase) Class.forName("org.hl7.fhir.r5.formats.JsonParser").getConstructor().newInstance();
if ("TURTLE".equalsIgnoreCase(format))
throw new Error("unsupported Format " + format.toString()); // return new TurtleParser();
if ("JSONLD".equalsIgnoreCase(format))
throw new Error("unsupported Format " + format.toString()); // return new JsonLdParser();
if ("VBAR".equalsIgnoreCase(format)) throw new Error("unsupported Format " + format.toString()); //
if ("TEXT".equalsIgnoreCase(format)) throw new Error("unsupported Format " + format.toString()); //
throw new Error("unsupported Format " + format);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("Failed to create parser", e);
}
}
public static FhirFormat determineFormat(byte[] source) throws FHIRException {