You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2020/09/29 17:26:03 UTC
svn commit: r1882133 -
/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java
Author: tilman
Date: Tue Sep 29 17:26:03 2020
New Revision: 1882133
URL: http://svn.apache.org/viewvc?rev=1882133&view=rev
Log:
PDFBOX-4971: refactor; replace unsupported attribute with supported feature
Modified:
pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java
Modified: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java?rev=1882133&r1=1882132&r2=1882133&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java (original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java Tue Sep 29 17:26:03 2020
@@ -26,7 +26,7 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.InputStreamReader;
+import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
@@ -45,9 +45,6 @@ import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.xml.XMLConstants;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
@@ -74,9 +71,8 @@ import org.apache.pdfbox.debugger.stream
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdfparser.PDFStreamParser;
import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.util.XMLUtil;
import org.w3c.dom.Document;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
/**
* @author Khyrul Bashar
@@ -113,7 +109,6 @@ public class StreamPane implements Actio
private final Stream stream;
private ToolTipController tTController;
private PDResources resources;
- private final boolean isContentStream;
/**
* Constructor.
@@ -127,8 +122,6 @@ public class StreamPane implements Actio
public StreamPane(COSStream cosStream, boolean isContentStream, boolean isThumb,
COSDictionary resourcesDic) throws IOException
{
- this.isContentStream = isContentStream;
-
this.stream = new Stream(cosStream, isThumb);
if (resourcesDic != null)
{
@@ -308,7 +301,7 @@ public class StreamPane implements Actio
{
if (stream.isXmlMetadata())
{
- return getXMLDocument(inputStream, encoding);
+ return getXMLDocument(inputStream);
}
StyledDocument document = getContentStreamDocument(inputStream);
if (document != null)
@@ -376,43 +369,28 @@ public class StreamPane implements Actio
return docu;
}
- private StyledDocument getXMLDocument(InputStream inputStream, String encoding)
+ private StyledDocument getXMLDocument(InputStream inputStream)
{
StyledDocument docu = new DefaultStyledDocument();
if (inputStream != null)
{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
try
{
- InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding);
- 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();
- Document doc = builder.parse(new InputSource(inputStreamReader));
+ Document doc = XMLUtil.parse(inputStream);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
- transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(1));
- StreamResult result = new StreamResult(baos);
+ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1");
+ StringWriter sw = new StringWriter();
+ StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
- docu.insertString(0, new String(baos.toByteArray(), StandardCharsets.UTF_8), null);
+ docu.insertString(0, sw.toString(), null);
}
- catch (ParserConfigurationException | SAXException | IOException |
- TransformerFactoryConfigurationError | IllegalArgumentException |
+ catch (IOException | TransformerFactoryConfigurationError | IllegalArgumentException |
TransformerException | BadLocationException ex)
{
LOG.error(ex.getMessage(), ex);