Skip to content

Commit

Permalink
improved DOMUtil initialization, now with addTransformerFactorySystem…
Browse files Browse the repository at this point in the history
…Property configuration option
  • Loading branch information
1azyman committed Oct 5, 2019
1 parent 7e18163 commit 99122a4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
Expand Up @@ -101,7 +101,7 @@ private XSSchemaSet parseSchema(Element schema) throws SchemaException {
// Make sure that the schema parser sees all the namespace declarations
DOMUtil.fixNamespaceDeclarations(schema);
try {
TransformerFactory transfac = TransformerFactory.newInstance();
TransformerFactory transfac = DOMUtil.setupTransformerFactory();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
Expand Down
34 changes: 13 additions & 21 deletions infra/util/src/main/java/com/evolveum/midpoint/util/DOMUtil.java
Expand Up @@ -171,27 +171,7 @@ public class DOMUtil {

transformerThreadLocal = ThreadLocal.withInitial(() -> {
try {
//setTransformerFactoryIfPresent("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); // too many whitespaces in Java11
//setTransformerFactoryIfPresent("org.apache.xalan.xsltc.trax.TransformerFactoryImpl"); // too few whitespaces

String className = "org.apache.xalan.processor.TransformerFactoryImpl";
setTransformerFactoryIfPresent(className); // a bit slower

ClassLoader cl = null;
try {
Class clazz = Class.forName(className);
if (clazz != null) {
cl = clazz.getClassLoader();
}
} catch (Exception ex) {
}

TransformerFactory transformerFactory;
if (cl != null) {
transformerFactory = TransformerFactory.newInstance(className, cl);
} else {
transformerFactory = TransformerFactory.newInstance();
}
TransformerFactory transformerFactory = setupTransformerFactory();

//System.out.println("TF = " + transformerFactory.getClass().getName());
Transformer trans = transformerFactory.newTransformer();
Expand All @@ -205,7 +185,19 @@ public class DOMUtil {
});
}

public static TransformerFactory setupTransformerFactory() {
//setTransformerFactoryIfPresent("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); // too many whitespaces in Java11
//setTransformerFactoryIfPresent("org.apache.xalan.xsltc.trax.TransformerFactoryImpl"); // too few whitespaces
setTransformerFactoryIfPresent("org.apache.xalan.processor.TransformerFactoryImpl"); // a bit slower

return TransformerFactory.newInstance();
}

private static void setTransformerFactoryIfPresent(String className) {
if (!DOMUtilSettings.isAddTransformerFactorySystemProperty()) {
return;
}

String propertyName = TransformerFactory.class.getName();
try {
Class.forName(className);
Expand Down
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2010-2019 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.util;

/**
* Created by Viliam Repan (lazyman).
*/
public class DOMUtilSettings {

private static boolean addTransformerFactorySystemProperty = true;

public static boolean isAddTransformerFactorySystemProperty() {
return addTransformerFactorySystemProperty;
}

/**
* Method used by MidPoint Studio to disable setting system property during {@link DOMUtil} initialization.
* Not used within MidPoint as the default "true" value doesn't change the initialization behaviour.
*
* @param addTransformerFactorySystemProperty
*/
public static void setAddTransformerFactorySystemProperty(boolean addTransformerFactorySystemProperty) {
DOMUtilSettings.addTransformerFactorySystemProperty = addTransformerFactorySystemProperty;
}
}

0 comments on commit 99122a4

Please sign in to comment.