]> source.dussan.org Git - poi.git/commitdiff
XMLPrettyPrint: Don't try to pretty-print non-XML files and print out which file...
authorDominik Stadler <centic@apache.org>
Fri, 3 Apr 2015 15:54:59 +0000 (15:54 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 3 Apr 2015 15:54:59 +0000 (15:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1671095 13f79535-47bb-0310-9956-ffa450edef68

src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java
src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java

index 8819083771d6df96d0804fcfb44250e5e64a3fbf..55e2c368f2927131eb3b6d5d07ec707aec8511eb 100644 (file)
@@ -27,9 +27,11 @@ import java.io.IOException;
 import java.io.InputStream;\r
 import java.util.HashSet;\r
 import java.util.Set;\r
+import java.util.zip.ZipException;\r
 \r
 import org.apache.poi.POIOLE2TextExtractor;\r
 import org.apache.poi.POITextExtractor;\r
+import org.apache.poi.dev.OOXMLPrettyPrint;\r
 import org.apache.poi.extractor.ExtractorFactory;\r
 import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;\r
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;\r
@@ -68,6 +70,14 @@ public abstract class AbstractFileHandler implements FileHandler {
         } finally {\r
             ExtractorFactory.setThreadPrefersEventExtractors(before);\r
         }\r
+        \r
+        /* Did fail for some documents with special XML contents...\r
+        try {\r
+            OOXMLPrettyPrint.main(new String[] { file.getAbsolutePath(), \r
+                       "/tmp/pretty-" + file.getName() });\r
+        } catch (ZipException e) {\r
+               // ignore, not a Zip/OOXML file\r
+        }*/\r
     }\r
 \r
     private void handleExtractingInternal(File file) throws Exception {\r
index aab8112875c107b3532f379ea9239ea7c8e37e16..48341d0c9a732404d6c2d3915f18753af29bfbc6 100644 (file)
@@ -18,7 +18,6 @@ package org.apache.poi.dev;
 \r
 import java.io.BufferedOutputStream;\r
 import java.io.File;\r
-import java.io.FileNotFoundException;\r
 import java.io.FileOutputStream;\r
 import java.io.IOException;\r
 import java.io.OutputStream;\r
@@ -40,9 +39,9 @@ import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;\r
 import javax.xml.transform.stream.StreamResult;\r
 \r
+import org.apache.poi.util.IOUtils;\r
 import org.w3c.dom.Document;\r
 import org.xml.sax.InputSource;\r
-import org.xml.sax.SAXException;\r
 \r
 /**\r
  * Reads a zipped OOXML file and produces a copy with the included \r
@@ -80,8 +79,7 @@ public class OOXMLPrettyPrint {
        }\r
 \r
     private static void handleFile(File file, File outFile) throws ZipException,\r
-            IOException, FileNotFoundException, SAXException,\r
-            TransformerException, ParserConfigurationException {\r
+            IOException, TransformerException, ParserConfigurationException {\r
         System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile);\r
 \r
         ZipFile zipFile = new ZipFile(file);\r
@@ -99,15 +97,23 @@ public class OOXMLPrettyPrint {
                }\r
     }\r
 \r
-       private void handle(ZipFile file, ZipOutputStream out) throws SAXException, IOException, TransformerException {\r
+       private void handle(ZipFile file, ZipOutputStream out) throws IOException, TransformerException {\r
         Enumeration<? extends ZipEntry> entries = file.entries();\r
         while(entries.hasMoreElements()) {\r
             ZipEntry entry = entries.nextElement();\r
 \r
-            out.putNextEntry(new ZipEntry(entry.getName()));\r
+            String name = entry.getName();\r
+            out.putNextEntry(new ZipEntry(name));\r
             try {\r
-                Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry)));\r
-                pretty(document, out, 2);\r
+                if(name.endsWith(".xml") || name.endsWith(".rels")) {\r
+                    Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry)));\r
+                    pretty(document, out, 2);\r
+                } else {\r
+                    System.out.println("Not pretty-printing non-XML file " + name);\r
+                    IOUtils.copy(file.getInputStream(entry), out);\r
+                }\r
+            } catch (Exception e) {\r
+                throw new IOException("While handling entry " + name, e);\r
             } finally {\r
                 out.closeEntry();\r
             }\r