Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 3044 - use correct TransformerFactory impl class #3045

Merged
merged 2 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions fhir-model/src/main/java/com/ibm/fhir/model/util/XMLSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
public final class XMLSupport {
public static final String FHIR_NS_URI = "http://hl7.org/fhir";
public static final String XHTML_NS_URI = "http://www.w3.org/1999/xhtml";


// System properties used to control XML factory behavior
private static final String PROP_XML_INPUT_FACTORY = "javax.xml.stream.XMLInputFactory";
private static final String PROP_XML_INPUT_FACTORY_CONFIG = "com.ibm.fhir.xml.input.factory";
private static final String PROP_XML_OUTPUT_FACTORY = "javax.xml.stream.XMLOutputFactory";
private static final String PROP_TRANSFORMER_FACTORY = "javax.xml.stream.XMLTransformerFactory";
private static final String PROP_XML_OUTPUT_FACTORY_CONFIG = "com.ibm.fhir.xml.output.factory";

private static final String XML_INPUT_FACTORY_ID = "com.ibm.fhir.xml.input.factory";
private static final String XML_INPUT_FACTORY_IMPL = "com.sun.xml.internal.stream.XMLInputFactoryImpl";
private static final String XML_OUTPUT_FACTORY_ID = "com.ibm.fhir.xml.output.factory";
private static final String XML_OUTPUT_FACTORY_IMPL = "com.sun.xml.internal.stream.XMLOutputFactoryImpl";
private static final String TRANSFORMER_FACTORY_IMPL = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";

Expand Down Expand Up @@ -190,17 +194,25 @@ public void close() throws XMLStreamException {

private static XMLInputFactory createXMLInputFactory() {
try {
boolean isSet = System.getProperty(PROP_XML_INPUT_FACTORY) != null;
if (!isSet) {
System.setProperty(PROP_XML_INPUT_FACTORY, XML_INPUT_FACTORY_IMPL);
}
XMLInputFactory factory = XMLInputFactory.newFactory();
if (!isSet) {
System.clearProperty(PROP_XML_INPUT_FACTORY);
final XMLInputFactory factory;
if (System.getProperty(PROP_XML_INPUT_FACTORY_CONFIG) != null) {
// Use [java_home]/conf/stax.properties to specify the impl class
// using the value of XML_INPUT_FACTORY_ID as a key
factory = XMLInputFactory.newFactory(XML_INPUT_FACTORY_ID, null);
} else {
boolean isSet = System.getProperty(PROP_XML_INPUT_FACTORY) != null;
if (!isSet) {
System.setProperty(PROP_XML_INPUT_FACTORY, XML_INPUT_FACTORY_IMPL);
}
factory = XMLInputFactory.newFactory();
if (!isSet) {
System.clearProperty(PROP_XML_INPUT_FACTORY);
}
}

factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);

return factory;
} catch (Exception e) {
throw new Error(e);
Expand All @@ -209,15 +221,21 @@ private static XMLInputFactory createXMLInputFactory() {

private static XMLOutputFactory createXMLOutputFactory() {
try {
boolean isSet = System.getProperty(PROP_XML_OUTPUT_FACTORY) != null;
if (!isSet) {
System.setProperty(PROP_XML_OUTPUT_FACTORY, XML_OUTPUT_FACTORY_IMPL);
}
XMLOutputFactory factory = XMLOutputFactory.newFactory();
if (!isSet) {
System.clearProperty(PROP_XML_OUTPUT_FACTORY);
final XMLOutputFactory factory;
if (System.getProperty(PROP_XML_OUTPUT_FACTORY_CONFIG) != null) {
// Use [java_home]/conf/stax.properties to specify the impl class
// using the value of XML_OUTPUT_FACTORY_ID as a key
factory = XMLOutputFactory.newFactory(XML_OUTPUT_FACTORY_ID, null);
} else {
boolean isSet = System.getProperty(PROP_XML_OUTPUT_FACTORY) != null;
if (!isSet) {
System.setProperty(PROP_XML_OUTPUT_FACTORY, XML_OUTPUT_FACTORY_IMPL);
}
factory = XMLOutputFactory.newFactory();
if (!isSet) {
System.clearProperty(PROP_XML_OUTPUT_FACTORY);
}
}

return factory;
} catch (Exception e) {
throw new Error(e);
Expand All @@ -226,15 +244,8 @@ private static XMLOutputFactory createXMLOutputFactory() {

private static TransformerFactory createTransformerFactory() {
try {
boolean isSet = System.getProperty(PROP_TRANSFORMER_FACTORY) != null;
if (!isSet) {
System.setProperty(PROP_TRANSFORMER_FACTORY, TRANSFORMER_FACTORY_IMPL);
lmsurpre marked this conversation as resolved.
Show resolved Hide resolved
}
TransformerFactory factory = TransformerFactory.newInstance();
if (!isSet) {
System.clearProperty(PROP_TRANSFORMER_FACTORY);
}

// Always get the correct TransformerFactory
TransformerFactory factory = TransformerFactory.newInstance(TRANSFORMER_FACTORY_IMPL, null);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
return factory;
} catch (Exception e) {
Expand Down
Loading