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/28 17:36:45 UTC
svn commit: r1882094 -
/pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java
Author: tilman
Date: Mon Sep 28 17:36:45 2020
New Revision: 1882094
URL: http://svn.apache.org/viewvc?rev=1882094&view=rev
Log:
PDFBOX-4971: format XML
Modified:
pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java
Modified: pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java?rev=1882094&r1=1882093&r2=1882094&view=diff
==============================================================================
--- pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java (original)
+++ pdfbox/branches/2.0/debugger/src/main/java/org/apache/pdfbox/debugger/streampane/StreamPane.java Mon Sep 28 17:36:45 2020
@@ -26,6 +26,8 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Vector;
@@ -43,6 +45,16 @@ import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
+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.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.contentstream.operator.Operator;
@@ -63,6 +75,9 @@ import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdfparser.PDFStreamParser;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.util.Charsets;
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
/**
* @author Khyrul Bashar
@@ -149,7 +164,7 @@ public class StreamPane implements Actio
rawView = new StreamPaneView();
hexView = new HexView();
- if (isContentStream)
+ if (isContentStream || stream.isXmlMetadata())
{
niceView = new StreamPaneView();
}
@@ -174,7 +189,7 @@ public class StreamPane implements Actio
{
tabbedPane.add("Image view", rawView.getStreamPanel());
}
- else if (isContentStream)
+ else if (isContentStream || stream.isXmlMetadata())
{
tabbedPane.add("Nice view", niceView.getStreamPanel());
tabbedPane.add("Raw view", rawView.getStreamPanel());
@@ -224,7 +239,7 @@ public class StreamPane implements Actio
return;
}
tabbedPane.removeAll();
- if (Stream.UNFILTERED.equals(currentFilter) && isContentStream)
+ if (Stream.UNFILTERED.equals(currentFilter) && (isContentStream || stream.isXmlMetadata()))
{
tabbedPane.add("Nice view", niceView.getStreamPanel());
tabbedPane.add("Raw view", rawView.getStreamPanel());
@@ -313,6 +328,10 @@ public class StreamPane implements Actio
InputStream inputStream = stream.getStream(filterKey);
if (nice && Stream.UNFILTERED.equals(filterKey))
{
+ if (stream.isXmlMetadata())
+ {
+ return getXMLDocument(inputStream, encoding);
+ }
StyledDocument document = getContentStreamDocument(inputStream);
if (document != null)
{
@@ -383,6 +402,69 @@ public class StreamPane implements Actio
return docu;
}
+ private StyledDocument getXMLDocument(InputStream inputStream, String encoding)
+ {
+ 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));
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(1));
+ StreamResult result = new StreamResult(baos);
+ DOMSource source = new DOMSource(doc);
+ transformer.transform(source, result);
+ docu.insertString(0, new String(baos.toByteArray(), Charsets.UTF_8), null);
+ }
+ catch (ParserConfigurationException ex)
+ {
+ LOG.error(ex.getMessage(), ex);
+ }
+ catch (UnsupportedEncodingException ex)
+ {
+ LOG.error(ex.getMessage(), ex);
+ }
+ catch (SAXException ex)
+ {
+ LOG.error(ex.getMessage(), ex);
+ }
+ catch (TransformerConfigurationException ex)
+ {
+ LOG.error(ex.getMessage(), ex);
+ }
+ catch (TransformerException ex)
+ {
+ LOG.error(ex.getMessage(), ex);
+ }
+ catch (BadLocationException ex)
+ {
+ LOG.error(ex.getMessage(), ex);
+ }
+ catch (IOException ex)
+ {
+ LOG.error(ex.getMessage(), ex);
+ }
+ }
+ return docu;
+ }
+
private StyledDocument getContentStreamDocument(InputStream inputStream)
{
StyledDocument docu = new DefaultStyledDocument();
@@ -569,4 +651,4 @@ public class StreamPane implements Actio
}
}
}
-}
+}
\ No newline at end of file