--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.dev;\r
+\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
+import java.util.Enumeration;\r
+import java.util.zip.ZipEntry;\r
+import java.util.zip.ZipException;\r
+import java.util.zip.ZipFile;\r
+import java.util.zip.ZipOutputStream;\r
+\r
+import javax.xml.parsers.DocumentBuilder;\r
+import javax.xml.parsers.DocumentBuilderFactory;\r
+import javax.xml.parsers.ParserConfigurationException;\r
+import javax.xml.transform.OutputKeys;\r
+import javax.xml.transform.Result;\r
+import javax.xml.transform.Source;\r
+import javax.xml.transform.Transformer;\r
+import javax.xml.transform.TransformerException;\r
+import javax.xml.transform.TransformerFactory;\r
+import javax.xml.transform.dom.DOMSource;\r
+import javax.xml.transform.stream.StreamResult;\r
+\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
+ * pretty-printed XML files.\r
+ * \r
+ * This is useful for comparing OOXML files produced by different tools as the often \r
+ * use different formatting of the XML.\r
+ */\r
+public class OOXMLPrettyPrint {\r
+ private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();\r
+ private final DocumentBuilder documentBuilder;\r
+\r
+ public OOXMLPrettyPrint() throws ParserConfigurationException {\r
+ documentBuilder = documentBuilderFactory.newDocumentBuilder();\r
+ }\r
+\r
+ public static void main(String[] args) throws Exception {\r
+ if(args.length <= 1 || args.length % 2 != 0) {\r
+ System.err.println("Use:");\r
+ System.err.println("\tjava OOXMLPrettyPrint [<filename> <outfilename>] ...");\r
+ System.exit(1);\r
+ }\r
+ \r
+ for(int i = 0;i < args.length;i+=2) {\r
+ File f = new File(args[i]);\r
+ if(! f.exists()) {\r
+ System.err.println("Error, file not found!");\r
+ System.err.println("\t" + f.toString());\r
+ System.exit(2);\r
+ }\r
+\r
+ handleFile(f, new File(args[i+1]));\r
+ }\r
+ System.out.println("Done.");\r
+ }\r
+\r
+ private static void handleFile(File file, File outFile) throws ZipException,\r
+ IOException, FileNotFoundException, SAXException,\r
+ 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
+ try {\r
+ ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));\r
+ try {\r
+ new OOXMLPrettyPrint().handle(zipFile, out);\r
+ } finally {\r
+ out.close();\r
+ }\r
+ } finally {\r
+ zipFile.close();\r
+\r
+ System.out.println();\r
+ }\r
+ }\r
+\r
+ private void handle(ZipFile file, ZipOutputStream out) throws SAXException, 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
+ try {\r
+ Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry)));\r
+ pretty(document, out, 2);\r
+ } finally {\r
+ out.closeEntry();\r
+ }\r
+ System.out.print(".");\r
+ }\r
+ }\r
+\r
+ private static void pretty(Document document, OutputStream outputStream, int indent) throws TransformerException {\r
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();\r
+ Transformer transformer = transformerFactory.newTransformer();\r
+ transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");\r
+ if (indent > 0) {\r
+ // set properties to indent the resulting XML nicely\r
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");\r
+ transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));\r
+ }\r
+ Result result = new StreamResult(outputStream);\r
+ Source source = new DOMSource(document);\r
+ transformer.transform(source, result);\r
+ } \r
+}\r