Another attempt to avoid parser classload

This commit is contained in:
jamesagnew 2022-01-21 14:59:50 -05:00
parent 8818acfbf8
commit 315604759c
4 changed files with 67 additions and 40 deletions

View File

@ -131,17 +131,21 @@ public abstract class FormatUtilities {
*
* See https://github.com/hapifhir/hapi-fhir/issues/3268
*/
if ("XML".equalsIgnoreCase(format))
return new org.hl7.fhir.r5.formats.XmlParser();
if ("JSON".equalsIgnoreCase(format))
return new org.hl7.fhir.r5.formats.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);
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 (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException e) {
throw new Error("Could not instantiate", e);
}
}
public static FhirFormat determineFormat(byte[] source) throws FHIRException {

View File

@ -1,33 +1,33 @@
package org.hl7.fhir.r5.formats;
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (c) 2011+, HL7, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
@ -75,6 +75,7 @@ import org.hl7.fhir.r5.model.IdType;
import org.hl7.fhir.r5.model.PrimitiveType;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.StringType;
import org.hl7.fhir.r5.test.utils.ClassesLoadedFlags;
import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.json.JsonTrackingParser;
@ -86,6 +87,8 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import org.slf4j.LoggerFactory;
/**
* General parser for JSON content. You instantiate an JsonParser of these, but you
* actually use parse or parseGeneral defined on this class
@ -93,7 +96,12 @@ import com.google.gson.JsonSyntaxException;
* The two classes are separated to keep generated and manually maintained code apart.
*/
public abstract class JsonParserBase extends ParserBase implements IParser {
static {
LoggerFactory.getLogger("org.hl7.fhir.r5.formats.XmlParserBase").debug("XML Parser is being loaded");
ClassesLoadedFlags.ourJsonParserBaseLoaded = true;
}
@Override
public ParserType getType() {
return ParserType.JSON;

View File

@ -76,6 +76,7 @@ import org.hl7.fhir.r5.model.DomainResource;
import org.hl7.fhir.r5.model.Element;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.model.StringType;
import org.hl7.fhir.r5.test.utils.ClassesLoadedFlags;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
@ -83,6 +84,7 @@ import org.hl7.fhir.utilities.xhtml.XhtmlNode;
import org.hl7.fhir.utilities.xhtml.XhtmlParser;
import org.hl7.fhir.utilities.xml.IXMLWriter;
import org.hl7.fhir.utilities.xml.XMLWriter;
import org.slf4j.LoggerFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
@ -95,6 +97,11 @@ import org.xmlpull.v1.XmlPullParserFactory;
*/
public abstract class XmlParserBase extends ParserBase implements IParser {
static {
LoggerFactory.getLogger("org.hl7.fhir.r5.formats.XmlParserBase").debug("XML Parser is being loaded");
ClassesLoadedFlags.ourXmlParserBaseLoaded = true;
}
@Override
public ParserType getType() {
return ParserType.XML;

View File

@ -0,0 +1,8 @@
package org.hl7.fhir.r5.test.utils;
public class ClassesLoadedFlags {
public static boolean ourXmlParserBaseLoaded;
public static boolean ourJsonParserBaseLoaded;
}