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 15:53:41 UTC
svn commit: r1856952 - in
/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox: pdmodel/fdf/
pdmodel/interactive/form/ util/
Author: lehmi
Date: Thu Apr 4 15:53:41 2019
New Revision: 1856952
URL: http://svn.apache.org/viewvc?rev=1856952&view=rev
Log:
PDFBOX-4505: unify the usage of a XMLParser (DRY)
Added:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java (with props)
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java?rev=1856952&r1=1856951&r2=1856952&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFAnnotationStamp.java Thu Apr 4 15:53:41 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;
@@ -44,7 +41,6 @@ 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.
@@ -123,7 +119,8 @@ public class FDFAnnotationStamp extends
{
LOG.debug("Decoded XML: " + new String(decodedAppearanceXML));
- Document stampAppearance = getStampAppearanceDocument(decodedAppearanceXML);
+ Document stampAppearance = org.apache.pdfbox.util.XMLUtil
+ .parse(new ByteArrayInputStream(decodedAppearanceXML));
Element appearanceEl = stampAppearance.getDocumentElement();
@@ -139,31 +136,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 ex)
- {
- LOG.error("Error while converting appearance xml to document: " + ex);
- throw new IOException(ex);
- }
- catch (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.
*
* @param fdfXML The XML document that contains the appearance data.
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java?rev=1856952&r1=1856951&r2=1856952&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFDocument.java Thu Apr 4 15:53:41 2019
@@ -238,8 +238,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(org.apache.pdfbox.util.XMLUtil.parse(input));
}
/**
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java?rev=1856952&r1=1856951&r2=1856952&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/FDFField.java Thu Apr 4 15:53:41 2019
@@ -85,11 +85,11 @@ public class FDFField implements COSObje
Element child = (Element) node;
if (child.getTagName().equals("value"))
{
- setValue(XMLUtil.getNodeValue(child));
+ setValue(org.apache.pdfbox.util.XMLUtil.getNodeValue(child));
}
else if (child.getTagName().equals("value-richtext"))
{
- setRichText(new COSString(XMLUtil.getNodeValue(child)));
+ setRichText(new COSString(org.apache.pdfbox.util.XMLUtil.getNodeValue(child)));
}
else if (child.getTagName().equals("field"))
{
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java?rev=1856952&r1=1856951&r2=1856952&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/fdf/XMLUtil.java Thu Apr 4 15:53:41 2019
@@ -19,23 +19,17 @@ package org.apache.pdfbox.pdmodel.fdf;
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
+ *
+ * @deprecated Use {@link org.apache.pdfbox.util.XMLUtil} instead
*/
+@Deprecated
final class XMLUtil
{
/**
@@ -55,30 +49,7 @@ final class XMLUtil
*/
public static Document parse(InputStream is) 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);
- DocumentBuilder builder = builderFactory.newDocumentBuilder();
- return builder.parse(is);
- }
- catch (FactoryConfigurationError e)
- {
- throw new IOException(e.getMessage(), e);
- }
- catch (ParserConfigurationException e)
- {
- throw new IOException(e.getMessage(), e);
- }
- catch (SAXException e)
- {
- throw new IOException(e.getMessage(), e);
- }
+ return org.apache.pdfbox.util.XMLUtil.parse(is);
}
/**
@@ -89,17 +60,6 @@ final class XMLUtil
*/
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();
+ return org.apache.pdfbox.util.XMLUtil.getNodeValue(node);
}
}
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java?rev=1856952&r1=1856951&r2=1856952&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDXFAResource.java Thu Apr 4 15:53:41 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;
@@ -149,15 +147,7 @@ public final class PDXFAResource impleme
*/
public Document getDocument() throws ParserConfigurationException, SAXException, 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/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java?rev=1856952&view=auto
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java (added)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java Thu Apr 4 15:53:41 2019
@@ -0,0 +1,123 @@
+/*
+ * 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 e)
+ {
+ throw new IOException(e.getMessage(), e);
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new IOException(e.getMessage(), e);
+ }
+ catch (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/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/util/XMLUtil.java
------------------------------------------------------------------------------
svn:eol-style = native