poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1671095 - in /poi/trunk/src: integrationtest/org/apache/poi/stress/AbstractFileHandler.java ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java
Date Fri, 03 Apr 2015 15:54:59 GMT
Author: centic
Date: Fri Apr  3 15:54:59 2015
New Revision: 1671095

URL: http://svn.apache.org/r1671095
Log:
XMLPrettyPrint: Don't try to pretty-print non-XML files and print out which file from the
ooxml-file fails to parse

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

Modified: poi/trunk/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java?rev=1671095&r1=1671094&r2=1671095&view=diff
==============================================================================
--- poi/trunk/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java (original)
+++ poi/trunk/src/integrationtest/org/apache/poi/stress/AbstractFileHandler.java Fri Apr 
3 15:54:59 2015
@@ -27,9 +27,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.zip.ZipException;
 
 import org.apache.poi.POIOLE2TextExtractor;
 import org.apache.poi.POITextExtractor;
+import org.apache.poi.dev.OOXMLPrettyPrint;
 import org.apache.poi.extractor.ExtractorFactory;
 import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -68,6 +70,14 @@ public abstract class AbstractFileHandle
         } finally {
             ExtractorFactory.setThreadPrefersEventExtractors(before);
         }
+        
+        /* Did fail for some documents with special XML contents...
+        try {
+            OOXMLPrettyPrint.main(new String[] { file.getAbsolutePath(), 
+            		"/tmp/pretty-" + file.getName() });
+        } catch (ZipException e) {
+        	// ignore, not a Zip/OOXML file
+        }*/
     }
 
     private void handleExtractingInternal(File file) throws Exception {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java?rev=1671095&r1=1671094&r2=1671095&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/dev/OOXMLPrettyPrint.java Fri Apr  3 15:54:59
2015
@@ -18,7 +18,6 @@ package org.apache.poi.dev;
 
 import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -40,9 +39,9 @@ import javax.xml.transform.TransformerFa
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 
+import org.apache.poi.util.IOUtils;
 import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
 
 /**
  * Reads a zipped OOXML file and produces a copy with the included 
@@ -80,8 +79,7 @@ public class OOXMLPrettyPrint {
 	}
 
     private static void handleFile(File file, File outFile) throws ZipException,
-            IOException, FileNotFoundException, SAXException,
-            TransformerException, ParserConfigurationException {
+            IOException, TransformerException, ParserConfigurationException {
         System.out.println("Reading zip-file " + file + " and writing pretty-printed XML
to " + outFile);
 
         ZipFile zipFile = new ZipFile(file);
@@ -99,15 +97,23 @@ public class OOXMLPrettyPrint {
 		}
     }
 
-	private void handle(ZipFile file, ZipOutputStream out) throws SAXException, IOException,
TransformerException {
+	private void handle(ZipFile file, ZipOutputStream out) throws IOException, TransformerException
{
         Enumeration<? extends ZipEntry> entries = file.entries();
         while(entries.hasMoreElements()) {
             ZipEntry entry = entries.nextElement();
 
-            out.putNextEntry(new ZipEntry(entry.getName()));
+            String name = entry.getName();
+            out.putNextEntry(new ZipEntry(name));
             try {
-                Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry)));
-                pretty(document, out, 2);
+                if(name.endsWith(".xml") || name.endsWith(".rels")) {
+                    Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry)));
+                    pretty(document, out, 2);
+                } else {
+                    System.out.println("Not pretty-printing non-XML file " + name);
+                    IOUtils.copy(file.getInputStream(entry), out);
+                }
+            } catch (Exception e) {
+                throw new IOException("While handling entry " + name, e);
             } finally {
                 out.closeEntry();
             }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message