You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2020/04/26 13:37:26 UTC

svn commit: r1877025 - /pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java

Author: lehmi
Date: Sun Apr 26 13:37:26 2020
New Revision: 1877025

URL: http://svn.apache.org/viewvc?rev=1877025&view=rev
Log:
PDFBOX-4817: introduce an additional constructor to pass an alternative TransformerFactory and DocumentBuilderFactory

Modified:
    pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java

Modified: pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java?rev=1877025&r1=1877024&r2=1877025&view=diff
==============================================================================
--- pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java (original)
+++ pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/xml/XmpSerializer.java Sun Apr 26 13:37:26 2020
@@ -54,18 +54,34 @@ import org.w3c.dom.ProcessingInstruction
 public class XmpSerializer
 {
 
-    private DocumentBuilder documentBuilder = null;
+    private final TransformerFactory transformerFactory;
+    private final DocumentBuilder documentBuilder;
 
     private boolean parseTypeResourceForLi = true;
 
+    /**
+     * Default constructor.
+     */
+    @SuppressWarnings({ "squid:S2755" }) // self-created XML
     public XmpSerializer()
     {
+        this(TransformerFactory.newInstance(), DocumentBuilderFactory.newInstance());
+    }
+
+    /**
+     * Constructor to be used if other factories than the default ones are needed.
+     * 
+     * @param transformerFactory     transformer factory to be used
+     * @param documentBuilderFactory document builder factory to be used
+     */
+    public XmpSerializer(TransformerFactory transformerFactory,
+            DocumentBuilderFactory documentBuilderFactory)
+    {
+        this.transformerFactory = transformerFactory;
         // xml init
-        @SuppressWarnings({"squid:S2755"}) // self-created XML
-        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
         try
         {
-            documentBuilder = builderFactory.newDocumentBuilder();
+            documentBuilder = documentBuilderFactory.newDocumentBuilder();
         }
         catch (ParserConfigurationException e)
         {
@@ -277,20 +293,15 @@ public class XmpSerializer
     /**
      * Save the XML document to an output stream.
      * 
-     * @param doc
-     *            The XML document to save.
-     * @param outStream
-     *            The stream to save the document to.
-     * @param encoding
-     *            The encoding to save the file as.
+     * @param doc       The XML document to save.
+     * @param outStream The stream to save the document to.
+     * @param encoding  The encoding to save the file as.
      * 
-     * @throws TransformerException
-     *             If there is an error while saving the XML.
+     * @throws TransformerException If there is an error while saving the XML.
      */
     private void save(Node doc, OutputStream outStream, String encoding) throws TransformerException
     {
-        @SuppressWarnings({"squid:S4435"}) // self-created XML
-        Transformer transformer = TransformerFactory.newInstance().newTransformer();
+        Transformer transformer = transformerFactory.newTransformer();
         // human readable
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
         // indent elements