Skip to content

Commit

Permalink
Added XML transformer loading optimisations suggested by @philippe-gr…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomakehurst committed Jan 4, 2019
1 parent 2ace35b commit 50c07ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/com/github/tomakehurst/wiremock/common/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.tomakehurst.wiremock.common;

import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import org.custommonkey.xmlunit.XMLUnit;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.EntityResolver;
Expand All @@ -30,16 +31,42 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.security.AccessController;
import java.security.PrivilegedAction;

import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static javax.xml.transform.OutputKeys.INDENT;
import static javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION;

public class Xml {

public static void optimizeFactoriesLoading() {
String transformerFactoryImpl = TransformerFactory.newInstance().getClass().getName();
String xPathFactoryImpl = XPathFactory.newInstance().getClass().getName();

setProperty(TransformerFactory.class.getName(), transformerFactoryImpl);
setProperty(
XPathFactory.DEFAULT_PROPERTY_NAME + ":" + XPathFactory.DEFAULT_OBJECT_MODEL_URI,
xPathFactoryImpl
);

XMLUnit.setTransformerFactory(transformerFactoryImpl);
XMLUnit.setXPathFactory(xPathFactoryImpl);
}

private static String setProperty(final String name, final String value) {
return AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
return System.setProperty(name, value);
}
});
}

public static String prettyPrint(String xml) {
try {
return prettyPrint(read(xml));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.github.tomakehurst.wiremock.admin.LimitAndOffsetPaginator;
import com.github.tomakehurst.wiremock.admin.model.*;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.common.Xml;
import com.github.tomakehurst.wiremock.extension.*;
import com.github.tomakehurst.wiremock.global.GlobalSettings;
import com.github.tomakehurst.wiremock.global.GlobalSettingsHolder;
Expand Down Expand Up @@ -69,6 +70,10 @@ public class WireMockApp implements StubServer, Admin {

private Options options;

static {
Xml.optimizeFactoriesLoading();
}

public WireMockApp(Options options, Container container) {
this.options = options;

Expand Down

0 comments on commit 50c07ef

Please sign in to comment.