1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-11-22 21:15:09 +00:00

Reuse long-lived XML DocumentBuilderFactory & TransformerFactory instances.

This commit is contained in:
matthew16550 2021-10-07 02:59:13 +11:00
parent c874842158
commit cd6f988eac
7 changed files with 52 additions and 48 deletions

View File

@ -42,12 +42,10 @@ import java.util.Date;
import java.util.List; import java.util.List;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -59,6 +57,7 @@ import net.sourceforge.plantuml.stats.api.Stats;
import net.sourceforge.plantuml.stats.api.StatsColumn; import net.sourceforge.plantuml.stats.api.StatsColumn;
import net.sourceforge.plantuml.stats.api.StatsLine; import net.sourceforge.plantuml.stats.api.StatsLine;
import net.sourceforge.plantuml.stats.api.StatsTable; import net.sourceforge.plantuml.stats.api.StatsTable;
import net.sourceforge.plantuml.xml.XmlFactories;
public class XmlConverter { public class XmlConverter {
@ -71,8 +70,7 @@ public class XmlConverter {
} }
private Document getDocument() throws ParserConfigurationException { private Document getDocument() throws ParserConfigurationException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = XmlFactories.newDocumentBuilder();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.newDocument(); final Document document = builder.newDocument();
document.setXmlStandalone(true); document.setXmlStandalone(true);
@ -124,8 +122,7 @@ public class XmlConverter {
} }
private Transformer getTransformer() throws TransformerException { private Transformer getTransformer() throws TransformerException {
final TransformerFactory xformFactory = TransformerFactory.newInstance(); final Transformer transformer = XmlFactories.newTransformer();
final Transformer transformer = xformFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
return transformer; return transformer;
} }

View File

@ -53,12 +53,10 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -81,6 +79,7 @@ import net.sourceforge.plantuml.ugraphic.USegment;
import net.sourceforge.plantuml.ugraphic.USegmentType; import net.sourceforge.plantuml.ugraphic.USegmentType;
import net.sourceforge.plantuml.ugraphic.color.ColorMapper; import net.sourceforge.plantuml.ugraphic.color.ColorMapper;
import net.sourceforge.plantuml.ugraphic.color.HColorGradient; import net.sourceforge.plantuml.ugraphic.color.HColorGradient;
import net.sourceforge.plantuml.xml.XmlFactories;
public class SvgGraphics { public class SvgGraphics {
@ -240,9 +239,7 @@ public class SvgGraphics {
} }
private Document getDocument() throws ParserConfigurationException { private Document getDocument() throws ParserConfigurationException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = XmlFactories.newDocumentBuilder();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.newDocument(); final Document document = builder.newDocument();
document.setXmlStandalone(true); document.setXmlStandalone(true);
return document; return document;
@ -569,19 +566,7 @@ public class SvgGraphics {
} }
private Transformer getTransformer() throws TransformerException { private Transformer getTransformer() throws TransformerException {
// Get a TransformerFactory object. final Transformer transformer = XmlFactories.newTransformer();
TransformerFactory xformFactory = null;
// try {
// final Class<?> factoryClass = Class
// .forName("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
// xformFactory = (TransformerFactory) factoryClass.newInstance();
// } catch (Exception e) {
xformFactory = TransformerFactory.newInstance();
// }
Log.info("TransformerFactory=" + xformFactory.getClass());
// Get an XSL Transformer object.
final Transformer transformer = xformFactory.newTransformer();
Log.info("Transformer=" + transformer.getClass()); Log.info("Transformer=" + transformer.getClass());
// // Sets the standalone property in the first line of // // Sets the standalone property in the first line of

View File

@ -42,14 +42,12 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result; import javax.xml.transform.Result;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -64,6 +62,7 @@ import net.sourceforge.plantuml.cucadiagram.Member;
import net.sourceforge.plantuml.cucadiagram.Stereotype; import net.sourceforge.plantuml.cucadiagram.Stereotype;
import net.sourceforge.plantuml.skin.VisibilityModifier; import net.sourceforge.plantuml.skin.VisibilityModifier;
import net.sourceforge.plantuml.utils.UniqueSequence; import net.sourceforge.plantuml.utils.UniqueSequence;
import net.sourceforge.plantuml.xml.XmlFactories;
abstract class XmiClassDiagramAbstract implements IXmiClassDiagram { abstract class XmiClassDiagramAbstract implements IXmiClassDiagram {
@ -78,9 +77,8 @@ abstract class XmiClassDiagramAbstract implements IXmiClassDiagram {
public XmiClassDiagramAbstract(ClassDiagram classDiagram) throws ParserConfigurationException { public XmiClassDiagramAbstract(ClassDiagram classDiagram) throws ParserConfigurationException {
this.classDiagram = classDiagram; this.classDiagram = classDiagram;
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder(); final DocumentBuilder builder = XmlFactories.newDocumentBuilder();
this.document = builder.newDocument(); this.document = builder.newDocument();
document.setXmlVersion("1.0"); document.setXmlVersion("1.0");
document.setXmlStandalone(true); document.setXmlStandalone(true);
@ -131,8 +129,7 @@ abstract class XmiClassDiagramAbstract implements IXmiClassDiagram {
final Result resultat = new StreamResult(os); final Result resultat = new StreamResult(os);
final TransformerFactory fabrique = TransformerFactory.newInstance(); final Transformer transformer = XmlFactories.newTransformer();
final Transformer transformer = fabrique.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); // transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name()); transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name());

View File

@ -40,14 +40,12 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.OutputStream; import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result; import javax.xml.transform.Result;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -63,6 +61,7 @@ import net.sourceforge.plantuml.cucadiagram.LinkDecor;
import net.sourceforge.plantuml.descdiagram.DescriptionDiagram; import net.sourceforge.plantuml.descdiagram.DescriptionDiagram;
import net.sourceforge.plantuml.utils.UniqueSequence; import net.sourceforge.plantuml.utils.UniqueSequence;
import net.sourceforge.plantuml.version.Version; import net.sourceforge.plantuml.version.Version;
import net.sourceforge.plantuml.xml.XmlFactories;
public class XmiDescriptionDiagram implements IXmiClassDiagram { public class XmiDescriptionDiagram implements IXmiClassDiagram {
@ -72,9 +71,8 @@ public class XmiDescriptionDiagram implements IXmiClassDiagram {
public XmiDescriptionDiagram(DescriptionDiagram diagram) throws ParserConfigurationException { public XmiDescriptionDiagram(DescriptionDiagram diagram) throws ParserConfigurationException {
this.diagram = diagram; this.diagram = diagram;
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder(); final DocumentBuilder builder = XmlFactories.newDocumentBuilder();
this.document = builder.newDocument(); this.document = builder.newDocument();
document.setXmlVersion("1.0"); document.setXmlVersion("1.0");
document.setXmlStandalone(true); document.setXmlStandalone(true);
@ -232,8 +230,7 @@ public class XmiDescriptionDiagram implements IXmiClassDiagram {
final Result resultat = new StreamResult(os); final Result resultat = new StreamResult(os);
final TransformerFactory fabrique = TransformerFactory.newInstance(); final Transformer transformer = XmlFactories.newTransformer();
final Transformer transformer = fabrique.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); // transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name()); transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name());

View File

@ -40,14 +40,12 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.OutputStream; import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result; import javax.xml.transform.Result;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -63,6 +61,7 @@ import net.sourceforge.plantuml.cucadiagram.LinkDecor;
import net.sourceforge.plantuml.statediagram.StateDiagram; import net.sourceforge.plantuml.statediagram.StateDiagram;
import net.sourceforge.plantuml.utils.UniqueSequence; import net.sourceforge.plantuml.utils.UniqueSequence;
import net.sourceforge.plantuml.version.Version; import net.sourceforge.plantuml.version.Version;
import net.sourceforge.plantuml.xml.XmlFactories;
public class XmiStateDiagram implements IXmiClassDiagram { public class XmiStateDiagram implements IXmiClassDiagram {
@ -72,9 +71,8 @@ public class XmiStateDiagram implements IXmiClassDiagram {
public XmiStateDiagram(StateDiagram diagram) throws ParserConfigurationException { public XmiStateDiagram(StateDiagram diagram) throws ParserConfigurationException {
this.diagram = diagram; this.diagram = diagram;
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder(); final DocumentBuilder builder = XmlFactories.newDocumentBuilder();
this.document = builder.newDocument(); this.document = builder.newDocument();
document.setXmlVersion("1.0"); document.setXmlVersion("1.0");
document.setXmlStandalone(true); document.setXmlStandalone(true);
@ -246,8 +244,7 @@ public class XmiStateDiagram implements IXmiClassDiagram {
final Result resultat = new StreamResult(os); final Result resultat = new StreamResult(os);
final TransformerFactory fabrique = TransformerFactory.newInstance(); final Transformer transformer = XmlFactories.newTransformer();
final Transformer transformer = fabrique.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); // transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name()); transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name());

View File

@ -0,0 +1,34 @@
package net.sourceforge.plantuml.xml;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
public class XmlFactories {
private XmlFactories() {
}
// This class uses the "initialization-on-demand holder" idiom to provide thread-safe
// lazy initialization of expensive factories.
// (see https://stackoverflow.com/a/8297830/1848731)
private static class DocumentBuilderFactoryHolder {
static final DocumentBuilderFactory INSTANCE = DocumentBuilderFactory.newInstance();
}
private static class TransformerFactoryHolder {
static final TransformerFactory INSTANCE = TransformerFactory.newInstance();
}
public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
return DocumentBuilderFactoryHolder.INSTANCE.newDocumentBuilder();
}
public static Transformer newTransformer() throws TransformerConfigurationException {
return TransformerFactoryHolder.INSTANCE.newTransformer();
}
}

View File

@ -40,14 +40,12 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.OutputStream; import java.io.OutputStream;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result; import javax.xml.transform.Result;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -59,6 +57,7 @@ import net.sourceforge.plantuml.cucadiagram.IEntity;
import net.sourceforge.plantuml.cucadiagram.LeafType; import net.sourceforge.plantuml.cucadiagram.LeafType;
import net.sourceforge.plantuml.cucadiagram.Link; import net.sourceforge.plantuml.cucadiagram.Link;
import net.sourceforge.plantuml.statediagram.StateDiagram; import net.sourceforge.plantuml.statediagram.StateDiagram;
import net.sourceforge.plantuml.xml.XmlFactories;
public class ScxmlStateDiagramStandard { public class ScxmlStateDiagramStandard {
@ -67,9 +66,8 @@ public class ScxmlStateDiagramStandard {
public ScxmlStateDiagramStandard(StateDiagram diagram) throws ParserConfigurationException { public ScxmlStateDiagramStandard(StateDiagram diagram) throws ParserConfigurationException {
this.diagram = diagram; this.diagram = diagram;
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder(); final DocumentBuilder builder = XmlFactories.newDocumentBuilder();
this.document = builder.newDocument(); this.document = builder.newDocument();
document.setXmlVersion("1.0"); document.setXmlVersion("1.0");
document.setXmlStandalone(true); document.setXmlStandalone(true);
@ -132,8 +130,7 @@ public class ScxmlStateDiagramStandard {
final Result resultat = new StreamResult(os); final Result resultat = new StreamResult(os);
final TransformerFactory fabrique = TransformerFactory.newInstance(); final Transformer transformer = XmlFactories.newTransformer();
final Transformer transformer = fabrique.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name()); transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8.name());
transformer.transform(source, resultat); transformer.transform(source, resultat);