Avoid unnecessary JsonParser/XmlParser class load
This commit is contained in:
parent
15fcc4607e
commit
942d4f15aa
|
@ -70,9 +70,15 @@ import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonNull;
|
import com.google.gson.JsonNull;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class JsonParser extends ParserBase {
|
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 JsonCreator json;
|
||||||
private Map<JsonElement, LocationData> map;
|
private Map<JsonElement, LocationData> map;
|
||||||
private boolean allowComments;
|
private boolean allowComments;
|
||||||
|
|
|
@ -75,12 +75,19 @@ import org.hl7.fhir.utilities.xhtml.XhtmlParser;
|
||||||
import org.hl7.fhir.utilities.xml.IXMLWriter;
|
import org.hl7.fhir.utilities.xml.IXMLWriter;
|
||||||
import org.hl7.fhir.utilities.xml.XMLUtil;
|
import org.hl7.fhir.utilities.xml.XMLUtil;
|
||||||
import org.hl7.fhir.utilities.xml.XMLWriter;
|
import org.hl7.fhir.utilities.xml.XMLWriter;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Node;
|
import org.w3c.dom.Node;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.XMLReader;
|
import org.xml.sax.XMLReader;
|
||||||
|
|
||||||
public class XmlParser extends ParserBase {
|
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 boolean allowXsiLocation;
|
||||||
private String version;
|
private String version;
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
|
@ -117,24 +118,31 @@ public abstract class FormatUtilities {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParserBase makeParser(FhirFormat format) {
|
public static ParserBase makeParser(FhirFormat format) {
|
||||||
switch (format) {
|
return makeParser(format.name());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ParserBase makeParser(String format) {
|
public static ParserBase makeParser(String format) {
|
||||||
if ("XML".equalsIgnoreCase(format)) return new XmlParser();
|
/*
|
||||||
if ("JSON".equalsIgnoreCase(format)) return new JsonParser();
|
* Note: In this class we're instantiating the parsers using reflection. This is because the
|
||||||
if ("TURTLE".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // return new TurtleParser();
|
* XmlParser and JsonParser are huuuuuge classes and classloading them is quite expensive
|
||||||
if ("JSONLD".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); // return new JsonLdParser();
|
* in cases where they won't actually ever be instantiated (such as when using the
|
||||||
if ("VBAR".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); //
|
* validator in HAPI FHIR)
|
||||||
if ("TEXT".equalsIgnoreCase(format)) throw new Error("unsupported Format "+format.toString()); //
|
*/
|
||||||
throw new Error("unsupported Format "+format);
|
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 {
|
public static FhirFormat determineFormat(byte[] source) throws FHIRException {
|
||||||
|
|
Loading…
Reference in New Issue