XMLPrettyPrint: Don't try to pretty-print non-XML files and print out which file from the ooxml-file fails to parse

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1671095 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-04-03 15:54:59 +00:00
parent 165fc1a099
commit b173dc11d0
2 changed files with 24 additions and 8 deletions

View File

@ -27,9 +27,11 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.zip.ZipException;
import org.apache.poi.POIOLE2TextExtractor; import org.apache.poi.POIOLE2TextExtractor;
import org.apache.poi.POITextExtractor; import org.apache.poi.POITextExtractor;
import org.apache.poi.dev.OOXMLPrettyPrint;
import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor; import org.apache.poi.hpsf.extractor.HPSFPropertiesExtractor;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -68,6 +70,14 @@ public abstract class AbstractFileHandler implements FileHandler {
} finally { } finally {
ExtractorFactory.setThreadPrefersEventExtractors(before); 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 { private void handleExtractingInternal(File file) throws Exception {

View File

@ -18,7 +18,6 @@ package org.apache.poi.dev;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -40,9 +39,9 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import org.apache.poi.util.IOUtils;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/** /**
* Reads a zipped OOXML file and produces a copy with the included * 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, private static void handleFile(File file, File outFile) throws ZipException,
IOException, FileNotFoundException, SAXException, IOException, TransformerException, ParserConfigurationException {
TransformerException, ParserConfigurationException {
System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile); System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile);
ZipFile zipFile = new ZipFile(file); 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(); Enumeration<? extends ZipEntry> entries = file.entries();
while(entries.hasMoreElements()) { while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement(); ZipEntry entry = entries.nextElement();
out.putNextEntry(new ZipEntry(entry.getName())); String name = entry.getName();
out.putNextEntry(new ZipEntry(name));
try { try {
if(name.endsWith(".xml") || name.endsWith(".rels")) {
Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry))); Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry)));
pretty(document, out, 2); 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 { } finally {
out.closeEntry(); out.closeEntry();
} }