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 2019/04/04 16:03:34 UTC

svn commit: r1856954 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/fdf/ pdmodel/interactive/form/ util/

Author: lehmi
Date: Thu Apr  4 16:03:34 2019
New Revision: 1856954

URL: http://svn.apache.org/viewvc?rev=1856954&view=rev
Log:
PDFBOX-4505: unify the usage of a XMLParser (DRY)

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java   (with props)
Removed:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java?rev=1856954&r1=1856953&r2=1856954&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java Thu Apr  4 16:03:34 2019
@@ -20,9 +20,6 @@ import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathExpressionException;
 import javax.xml.xpath.XPathFactory;
@@ -38,12 +35,11 @@ import org.apache.pdfbox.cos.COSInteger;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.util.Hex;
-
+import org.apache.pdfbox.util.XMLUtil;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
 
 /**
  * This represents a Stamp FDF annotation.
@@ -122,7 +118,8 @@ public class FDFAnnotationStamp extends
         {
             LOG.debug("Decoded XML: " + new String(decodedAppearanceXML));
 
-            Document stampAppearance = getStampAppearanceDocument(decodedAppearanceXML);
+            Document stampAppearance = XMLUtil
+                    .parse(new ByteArrayInputStream(decodedAppearanceXML));
 
             Element appearanceEl = stampAppearance.getDocumentElement();
 
@@ -137,26 +134,6 @@ public class FDFAnnotationStamp extends
         }
     }
 
-    /**
-     * Parse the <param>xmlString</param> to DOM Document tree from XML content
-     */
-    private Document getStampAppearanceDocument(byte[] xml) throws IOException
-    {
-        try
-        {
-            // Obtain DOM Document instance and create DocumentBuilder with default configuration
-            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-
-            // Parse the content to Document object
-            return builder.parse(new ByteArrayInputStream(xml));
-        }
-        catch (ParserConfigurationException | SAXException ex)
-        {
-            LOG.error("Error while converting appearance xml to document: " + ex);
-            throw new IOException(ex);
-        }
-    }
-
     /**
      * This will create an Appearance dictionary from an appearance XML document.
      *

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java?rev=1856954&r1=1856953&r2=1856954&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java Thu Apr  4 16:03:34 2019
@@ -33,6 +33,7 @@ import org.apache.pdfbox.cos.COSDocument
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdfparser.FDFParser;
 import org.apache.pdfbox.pdfwriter.COSWriter;
+import org.apache.pdfbox.util.XMLUtil;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -239,8 +240,7 @@ public class FDFDocument implements Clos
      */
     public static FDFDocument loadXFDF(InputStream input) throws IOException
     {
-        Document doc = XMLUtil.parse(input);
-        return new FDFDocument(doc);
+        return new FDFDocument(XMLUtil.parse(input));
     }
 
     /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java?rev=1856954&r1=1856953&r2=1856954&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java Thu Apr  4 16:03:34 2019
@@ -34,6 +34,7 @@ import org.apache.pdfbox.pdmodel.interac
 import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory;
 import org.apache.pdfbox.pdmodel.interactive.action.PDAdditionalActions;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
+import org.apache.pdfbox.util.XMLUtil;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java?rev=1856954&r1=1856953&r2=1856954&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java Thu Apr  4 16:03:34 2019
@@ -21,8 +21,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.apache.pdfbox.cos.COSArray;
@@ -136,17 +134,9 @@ public final class PDXFAResource impleme
      * @throws IOException if something went wrong when reading the XFA content.
      * 
      */        
-    public Document getDocument() throws ParserConfigurationException, SAXException, IOException 
+    public Document getDocument() throws IOException
     {
-        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-        factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
-        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
-        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
-        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
-        factory.setXIncludeAware(false);
-        factory.setExpandEntityReferences(false);
-        factory.setNamespaceAware(true);
-        DocumentBuilder builder = factory.newDocumentBuilder();
-        return builder.parse(new ByteArrayInputStream(this.getBytes()));
+        return org.apache.pdfbox.util.XMLUtil //
+                .parse(new ByteArrayInputStream(this.getBytes()), true);
     }
 }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java?rev=1856954&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java Thu Apr  4 16:03:34 2019
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.pdfbox.util;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.SAXException;
+
+/**
+ * This class with handle some simple XML operations.
+ *
+ * @author Ben Litchfield
+ */
+public final class XMLUtil
+{
+    /**
+     * Utility class, should not be instantiated.
+     *
+     */
+    private XMLUtil()
+    {
+    }
+
+    /**
+     * This will parse an XML stream and create a DOM document.
+     *
+     * @param is The stream to get the XML from.
+     * @return The DOM document.
+     * @throws IOException It there is an error creating the dom.
+     */
+    public static Document parse(InputStream is) throws IOException
+    {
+        return parse(is, false);
+    }
+
+    /**
+     * This will parse an XML stream and create a DOM document.
+     *
+     * @param is The stream to get the XML from.
+     * @param nsAware activates namespace awareness of the parser
+     * @return The DOM document.
+     * @throws IOException It there is an error creating the dom.
+     */
+    public static Document parse(InputStream is, boolean nsAware) throws IOException
+    {
+        try
+        {
+            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+            builderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+            builderFactory.setFeature("http://xml.org/sax/features/external-general-entities",
+                    false);
+            builderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities",
+                    false);
+            builderFactory.setFeature(
+                    "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+            builderFactory.setXIncludeAware(false);
+            builderFactory.setExpandEntityReferences(false);
+            builderFactory.setNamespaceAware(nsAware);
+            DocumentBuilder builder = builderFactory.newDocumentBuilder();
+            return builder.parse(is);
+        }
+        catch (FactoryConfigurationError | ParserConfigurationException | SAXException e)
+        {
+            throw new IOException(e.getMessage(), e);
+        }
+    }
+
+    /**
+     * This will get the text value of an element.
+     *
+     * @param node The node to get the text value for.
+     * @return The text of the node.
+     */
+    public static String getNodeValue(Element node)
+    {
+        StringBuilder sb = new StringBuilder();
+        NodeList children = node.getChildNodes();
+        int numNodes = children.getLength();
+        for (int i = 0; i < numNodes; i++)
+        {
+            Node next = children.item(i);
+            if (next instanceof Text)
+            {
+                sb.append(next.getNodeValue());
+            }
+        }
+        return sb.toString();
+    }
+
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native