remove obsolete dev classes
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1833217 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
74ff20d363
commit
3245d7c809
@ -1,152 +0,0 @@
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==================================================================== */
|
||||
package org.apache.poi.ooxml.dev;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.openxml4j.opc.PackageAccess;
|
||||
import org.apache.poi.openxml4j.opc.PackagePart;
|
||||
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
||||
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
|
||||
|
||||
/**
|
||||
* Prints out the contents of a OOXML container.
|
||||
* Useful for seeing what parts are defined, and how
|
||||
* they're all related to each other.
|
||||
*/
|
||||
public class OOXMLLister implements Closeable {
|
||||
private final OPCPackage container;
|
||||
private final PrintStream disp;
|
||||
|
||||
public OOXMLLister(OPCPackage container) {
|
||||
this(container, System.out);
|
||||
}
|
||||
public OOXMLLister(OPCPackage container, PrintStream disp) {
|
||||
this.container = container;
|
||||
this.disp = disp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Figures out how big a given PackagePart is.
|
||||
*
|
||||
* @param part the PackagePart
|
||||
* @return the size of the PackagePart
|
||||
*
|
||||
* @throws IOException if the part can't be read
|
||||
*/
|
||||
public static long getSize(PackagePart part) throws IOException {
|
||||
InputStream in = part.getInputStream();
|
||||
try {
|
||||
byte[] b = new byte[8192];
|
||||
long size = 0;
|
||||
int read = 0;
|
||||
|
||||
while(read > -1) {
|
||||
read = in.read(b);
|
||||
if(read > 0) {
|
||||
size += read;
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
} finally {
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays information on all the different
|
||||
* parts of the OOXML file container.
|
||||
* @throws InvalidFormatException if the package relations are invalid
|
||||
* @throws IOException if the package can't be read
|
||||
*/
|
||||
public void displayParts() throws InvalidFormatException, IOException {
|
||||
ArrayList<PackagePart> parts = container.getParts();
|
||||
for (PackagePart part : parts) {
|
||||
disp.println(part.getPartName());
|
||||
disp.println("\t" + part.getContentType());
|
||||
|
||||
if(! part.getPartName().toString().equals("/docProps/core.xml")) {
|
||||
disp.println("\t" + getSize(part) + " bytes");
|
||||
}
|
||||
|
||||
if(! part.isRelationshipPart()) {
|
||||
disp.println("\t" + part.getRelationships().size() + " relations");
|
||||
for(PackageRelationship rel : part.getRelationships()) {
|
||||
displayRelation(rel, "\t ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Displays information on all the different
|
||||
* relationships between different parts
|
||||
* of the OOXML file container.
|
||||
*/
|
||||
public void displayRelations() {
|
||||
PackageRelationshipCollection rels =
|
||||
container.getRelationships();
|
||||
for (PackageRelationship rel : rels) {
|
||||
displayRelation(rel, "");
|
||||
}
|
||||
}
|
||||
|
||||
private void displayRelation(PackageRelationship rel, String indent) {
|
||||
disp.println(indent+"Relationship:");
|
||||
disp.println(indent+"\tFrom: "+ rel.getSourceURI());
|
||||
disp.println(indent+"\tTo: " + rel.getTargetURI());
|
||||
disp.println(indent+"\tID: " + rel.getId());
|
||||
disp.println(indent+"\tMode: " + rel.getTargetMode());
|
||||
disp.println(indent+"\tType: " + rel.getRelationshipType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
container.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, InvalidFormatException {
|
||||
if(args.length == 0) {
|
||||
System.err.println("Use:");
|
||||
System.err.println("\tjava OOXMLLister <filename>");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
File f = new File(args[0]);
|
||||
if(! f.exists()) {
|
||||
System.err.println("Error, file not found!");
|
||||
System.err.println("\t" + f);
|
||||
System.exit(2);
|
||||
}
|
||||
|
||||
OOXMLLister lister = new OOXMLLister(
|
||||
OPCPackage.open(f.toString(), PackageAccess.READ)
|
||||
);
|
||||
|
||||
try {
|
||||
lister.disp.println(f + "\n");
|
||||
lister.displayParts();
|
||||
lister.disp.println();
|
||||
lister.displayRelations();
|
||||
} finally {
|
||||
lister.close();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,136 +0,0 @@
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==================================================================== */
|
||||
package org.apache.poi.ooxml.dev;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
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.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile;
|
||||
import org.apache.poi.openxml4j.opc.internal.ZipHelper;
|
||||
import org.apache.poi.openxml4j.util.ZipSecureFile;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* Reads a zipped OOXML file and produces a copy with the included
|
||||
* pretty-printed XML files.
|
||||
*
|
||||
* This is useful for comparing OOXML files produced by different tools as the often
|
||||
* use different formatting of the XML.
|
||||
*/
|
||||
public class OOXMLPrettyPrint {
|
||||
private final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
private final DocumentBuilder documentBuilder;
|
||||
|
||||
public OOXMLPrettyPrint() throws ParserConfigurationException {
|
||||
// allow files with much lower inflation rate here as there is no risk of Zip Bomb attacks in this developer tool
|
||||
ZipSecureFile.setMinInflateRatio(0.00001);
|
||||
|
||||
documentBuilder = documentBuilderFactory.newDocumentBuilder();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if(args.length <= 1 || args.length % 2 != 0) {
|
||||
System.err.println("Use:");
|
||||
System.err.println("\tjava OOXMLPrettyPrint [<filename> <outfilename>] ...");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
for(int i = 0;i < args.length;i+=2) {
|
||||
File f = new File(args[i]);
|
||||
if(! f.exists()) {
|
||||
System.err.println("Error, file not found!");
|
||||
System.err.println("\t" + f);
|
||||
System.exit(2);
|
||||
}
|
||||
|
||||
handleFile(f, new File(args[i+1]));
|
||||
}
|
||||
System.out.println("Done.");
|
||||
}
|
||||
|
||||
private static void handleFile(File file, File outFile) throws
|
||||
IOException, TransformerException, ParserConfigurationException {
|
||||
System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile);
|
||||
|
||||
try (ZipSecureFile zipFile = ZipHelper.openZipFile(file)) {
|
||||
try (ZipArchiveOutputStream out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)))) {
|
||||
new OOXMLPrettyPrint().handle(zipFile, out);
|
||||
}
|
||||
} finally {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private void handle(ZipFile file, ZipArchiveOutputStream out) throws IOException, TransformerException {
|
||||
Enumeration<? extends ZipArchiveEntry> entries = file.getEntries();
|
||||
while(entries.hasMoreElements()) {
|
||||
ZipArchiveEntry entry = entries.nextElement();
|
||||
|
||||
String name = entry.getName();
|
||||
out.putArchiveEntry(new ZipArchiveEntry(name));
|
||||
try {
|
||||
if(name.endsWith(".xml") || name.endsWith(".rels")) {
|
||||
Document document = documentBuilder.parse(new InputSource(file.getInputStream(entry)));
|
||||
document.setXmlStandalone(true);
|
||||
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.closeArchiveEntry();
|
||||
}
|
||||
System.out.print(".");
|
||||
}
|
||||
}
|
||||
|
||||
private static void pretty(Document document, OutputStream outputStream, int indent) throws TransformerException {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
|
||||
if (indent > 0) {
|
||||
// set properties to indent the resulting XML nicely
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
|
||||
}
|
||||
Result result = new StreamResult(outputStream);
|
||||
Source source = new DOMSource(document);
|
||||
transformer.transform(source, result);
|
||||
}
|
||||
}
|
@ -1,110 +0,0 @@
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==================================================================== */
|
||||
|
||||
package org.apache.poi.xssf.dev;
|
||||
|
||||
import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Enumeration;
|
||||
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
import org.apache.poi.openxml4j.opc.internal.ZipHelper;
|
||||
import org.apache.poi.openxml4j.util.ZipSecureFile;
|
||||
import org.apache.poi.ooxml.util.DocumentHelper;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.apache.xmlbeans.XmlObject;
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* Utility class which dumps the contents of a *.xlsx file into file system.
|
||||
*
|
||||
* @author Yegor Kozlov
|
||||
*/
|
||||
public final class XSSFDump {
|
||||
|
||||
private XSSFDump() {}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
for (String arg : args) {
|
||||
System.out.println("Dumping " + arg);
|
||||
try (ZipSecureFile zip = ZipHelper.openZipFile(arg)) {
|
||||
dump(zip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void createDirIfMissing(File directory) throws RuntimeException {
|
||||
if (!directory.exists()) {
|
||||
boolean dirWasCreated = directory.mkdir();
|
||||
if (!dirWasCreated) {
|
||||
throw new RuntimeException("Unable to create directory: " + directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void recursivelyCreateDirIfMissing(File directory) throws RuntimeException {
|
||||
if (!directory.exists()) {
|
||||
boolean dirsWereCreated = directory.mkdirs();
|
||||
if (!dirsWereCreated) {
|
||||
throw new RuntimeException("Unable to recursively create directory: " + directory);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void dump(ZipSecureFile zip) throws Exception {
|
||||
String zipname = zip.getName();
|
||||
int sep = zipname.lastIndexOf('.');
|
||||
File root = new File(zipname.substring(0, sep));
|
||||
createDirIfMissing(root);
|
||||
System.out.println("Dumping to directory " + root);
|
||||
|
||||
Enumeration<? extends ZipArchiveEntry> en = zip.getEntries();
|
||||
while (en.hasMoreElements()) {
|
||||
ZipArchiveEntry entry = en.nextElement();
|
||||
String name = entry.getName();
|
||||
int idx = name.lastIndexOf('/');
|
||||
if (idx != -1) {
|
||||
File bs = new File(root, name.substring(0, idx));
|
||||
recursivelyCreateDirIfMissing(bs);
|
||||
}
|
||||
|
||||
File f = new File(root, entry.getName());
|
||||
try (final OutputStream out = new FileOutputStream(f)) {
|
||||
if (entry.getName().endsWith(".xml") || entry.getName().endsWith(".vml") || entry.getName().endsWith(".rels")) {
|
||||
try {
|
||||
Document doc = DocumentHelper.readDocument(zip.getInputStream(entry));
|
||||
XmlObject xml = XmlObject.Factory.parse(doc, DEFAULT_XML_OPTIONS);
|
||||
XmlOptions options = new XmlOptions();
|
||||
options.setSavePrettyPrint();
|
||||
xml.save(out, options);
|
||||
} catch (XmlException e) {
|
||||
System.err.println("Failed to parse " + entry.getName() + ", dumping raw content");
|
||||
IOUtils.copy(zip.getInputStream(entry), out);
|
||||
}
|
||||
} else {
|
||||
IOUtils.copy(zip.getInputStream(entry), out);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==================================================================== */
|
||||
|
||||
package org.apache.poi.xssf.dev;
|
||||
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
/**
|
||||
* Utility which loads a SpreadsheetML file and saves it back.
|
||||
* This is a handy tool to investigate read-write round trip safety.
|
||||
*
|
||||
* @author Yegor Kozlov
|
||||
*/
|
||||
public final class XSSFSave {
|
||||
public static void main(String[] args) throws Exception {
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
OPCPackage pkg = OPCPackage.open(args[i]);
|
||||
XSSFWorkbook wb = new XSSFWorkbook(pkg);
|
||||
|
||||
int sep = args[i].lastIndexOf('.');
|
||||
String outfile = args[i].substring(0, sep) + "-save.xls" + (wb.isMacroEnabled() ? "m" : "x");
|
||||
FileOutputStream out = new FileOutputStream(outfile);
|
||||
wb.write(out);
|
||||
out.close();
|
||||
|
||||
pkg.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
2012 - Alfresco Software, Ltd.
|
||||
Alfresco Software has modified source of this file
|
||||
The details of changes as svn diff can be found in svn at location root/projects/3rd-party/src
|
||||
==================================================================== */
|
||||
package org.apache.poi.dev;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import org.apache.poi.ooxml.dev.OOXMLLister;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.openxml4j.opc.PackageAccess;
|
||||
import org.apache.poi.util.NullOutputStream;
|
||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestOOXMLLister {
|
||||
private static PrintStream SYSTEM_OUT;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws UnsupportedEncodingException {
|
||||
SYSTEM_OUT = System.out;
|
||||
System.setOut(new PrintStream(new OutputStream() {
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
|
||||
}
|
||||
}, false, "UTF-8"));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
System.setOut(SYSTEM_OUT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMain() throws IOException, InvalidFormatException {
|
||||
File file = XSSFTestDataSamples.getSampleFile("Formatting.xlsx");
|
||||
OOXMLLister.main(new String[] {file.getAbsolutePath()});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithPrintStream() throws IOException, InvalidFormatException {
|
||||
File file = XSSFTestDataSamples.getSampleFile("Formatting.xlsx");
|
||||
PrintStream nullStream = new PrintStream(new NullOutputStream(), true, "UTF-8");
|
||||
OPCPackage opc = OPCPackage.open(file.getAbsolutePath(), PackageAccess.READ);
|
||||
OOXMLLister lister = new OOXMLLister(opc, nullStream);
|
||||
lister.displayParts();
|
||||
lister.displayRelations();
|
||||
lister.close();
|
||||
opc.close();
|
||||
nullStream.close();
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
2012 - Alfresco Software, Ltd.
|
||||
Alfresco Software has modified source of this file
|
||||
The details of changes as svn diff can be found in svn at location root/projects/3rd-party/src
|
||||
==================================================================== */
|
||||
package org.apache.poi.dev;
|
||||
|
||||
import org.apache.poi.ooxml.dev.OOXMLPrettyPrint;
|
||||
import org.apache.poi.util.TempFile;
|
||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TestOOXMLPrettyPrint {
|
||||
|
||||
private static PrintStream SYSTEM_OUT;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws UnsupportedEncodingException {
|
||||
SYSTEM_OUT = System.out;
|
||||
System.setOut(new PrintStream(new OutputStream() {
|
||||
@Override
|
||||
public void write(int b) throws IOException {
|
||||
|
||||
}
|
||||
}, false, "UTF-8"));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
System.setOut(SYSTEM_OUT);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMain() throws Exception {
|
||||
File file = XSSFTestDataSamples.getSampleFile("Formatting.xlsx");
|
||||
File outFile = TempFile.createTempFile("Formatting", "-pretty.xlsx");
|
||||
|
||||
assertTrue(outFile.delete());
|
||||
assertFalse(outFile.exists());
|
||||
|
||||
OOXMLPrettyPrint.main(new String[] {
|
||||
file.getAbsolutePath(), outFile.getAbsolutePath()
|
||||
});
|
||||
|
||||
assertTrue(outFile.exists());
|
||||
assertTrue(outFile.delete());
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
2012 - Alfresco Software, Ltd.
|
||||
Alfresco Software has modified source of this file
|
||||
The details of changes as svn diff can be found in svn at location root/projects/3rd-party/src
|
||||
==================================================================== */
|
||||
package org.apache.poi.dev;
|
||||
|
||||
import org.apache.poi.util.TempFile;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class TestRecordGenerator {
|
||||
@Ignore("Could not get this to run, probably the dev-application does not work any more at all")
|
||||
@Test
|
||||
public void testNotEnoughArgs() throws Exception {
|
||||
RecordGenerator.main(new String[] {});
|
||||
}
|
||||
|
||||
@Ignore("Could not get this to run, probably the dev-application does not work any more at all")
|
||||
@Test
|
||||
public void testMainRecords() throws Exception {
|
||||
File dir = TempFile.createTempDirectory("TestRecordGenerator");
|
||||
|
||||
RecordGenerator.main(new String[] {
|
||||
"src/records/definitions/",
|
||||
"src/records/styles/",
|
||||
dir.getAbsolutePath(),
|
||||
dir.getAbsolutePath(),
|
||||
});
|
||||
}
|
||||
|
||||
@Ignore("Could not get this to run, probably the dev-application does not work any more at all")
|
||||
@Test
|
||||
public void testMainTypes() throws Exception {
|
||||
File dir = TempFile.createTempDirectory("TestRecordGenerator");
|
||||
|
||||
RecordGenerator.main(new String[] {
|
||||
"src/types/definitions/",
|
||||
"src/types/styles/",
|
||||
dir.getAbsolutePath(),
|
||||
dir.getAbsolutePath(),
|
||||
});
|
||||
}
|
||||
}
|
@ -1,160 +0,0 @@
|
||||
|
||||
/* ====================================================================
|
||||
Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
contributor license agreements. See the NOTICE file distributed with
|
||||
this work for additional information regarding copyright ownership.
|
||||
The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
(the "License"); you may not use this file except in compliance with
|
||||
the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
==================================================================== */
|
||||
|
||||
package org.apache.poi.dev;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.apache.poi.util.XMLHelper;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Description of the Class
|
||||
*
|
||||
*@author andy
|
||||
*@since May 10, 2002
|
||||
*/
|
||||
public class RecordGenerator {
|
||||
/**
|
||||
* The main program for the RecordGenerator class
|
||||
*
|
||||
*@param args The command line arguments
|
||||
*@exception Exception Description of the Exception
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
throws Exception {
|
||||
// Force load so that we don't start generating records and realise this hasn't compiled yet.
|
||||
Class.forName("org.apache.poi.generator.FieldIterator");
|
||||
|
||||
if (args.length != 4) {
|
||||
System.out.println("Usage:");
|
||||
System.out.println(" java org.apache.poi.hssf.util.RecordGenerator RECORD_DEFINTIONS RECORD_STYLES DEST_SRC_PATH TEST_SRC_PATH");
|
||||
} else {
|
||||
generateRecords(args[0], args[1], args[2], args[3]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void generateRecords(String defintionsDir, String recordStyleDir, String destSrcPathDir, String testSrcPathDir)
|
||||
throws Exception {
|
||||
File definitionsFiles[] = new File(defintionsDir).listFiles();
|
||||
if (definitionsFiles == null) {
|
||||
System.err.println(defintionsDir+" is not a directory.");
|
||||
return;
|
||||
}
|
||||
|
||||
for (File file : definitionsFiles) {
|
||||
if (file.isFile() &&
|
||||
(file.getName().endsWith("_record.xml") ||
|
||||
file.getName().endsWith("_type.xml")
|
||||
)
|
||||
) {
|
||||
// Get record name and package
|
||||
DocumentBuilderFactory factory = XMLHelper.getDocumentBuilderFactory();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.parse(file);
|
||||
Element record = document.getDocumentElement();
|
||||
String extendstg = record.getElementsByTagName("extends").item(0).getFirstChild().getNodeValue();
|
||||
String suffix = record.getElementsByTagName("suffix").item(0).getFirstChild().getNodeValue();
|
||||
String recordName = record.getAttributes().getNamedItem("name").getNodeValue();
|
||||
String packageName = record.getAttributes().getNamedItem("package").getNodeValue();
|
||||
packageName = packageName.replace('.', '/');
|
||||
|
||||
// Generate record
|
||||
String destinationPath = destSrcPathDir + "/" + packageName;
|
||||
File destinationPathFile = new File(destinationPath);
|
||||
if(!destinationPathFile.mkdirs()) {
|
||||
throw new IOException("Could not create directory " + destinationPathFile);
|
||||
} else {
|
||||
System.out.println("Created destination directory: " + destinationPath);
|
||||
}
|
||||
String destinationFilepath = destinationPath + "/" + recordName + suffix + ".java";
|
||||
transform(file, new File(destinationFilepath),
|
||||
new File(recordStyleDir + "/" + extendstg.toLowerCase(Locale.ROOT) + ".xsl"));
|
||||
System.out.println("Generated " + suffix + ": " + destinationFilepath);
|
||||
|
||||
// Generate test (if not already generated)
|
||||
destinationPath = testSrcPathDir + "/" + packageName;
|
||||
destinationPathFile = new File(destinationPath);
|
||||
if(!destinationPathFile.mkdirs()) {
|
||||
throw new IOException("Could not create directory " + destinationPathFile);
|
||||
} else {
|
||||
System.out.println("Created destination directory: " + destinationPath);
|
||||
}
|
||||
destinationFilepath = destinationPath + "/Test" + recordName + suffix + ".java";
|
||||
if (!new File(destinationFilepath).exists()) {
|
||||
String temp = (recordStyleDir + "/" + extendstg.toLowerCase(Locale.ROOT) + "_test.xsl");
|
||||
transform(file, new File(destinationFilepath), new File(temp));
|
||||
System.out.println("Generated test: " + destinationFilepath);
|
||||
} else {
|
||||
System.out.println("Skipped test generation: " + destinationFilepath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Executes an XSL transformation. This process transforms an XML input
|
||||
* file into a text output file controlled by an XSLT specification.</p>
|
||||
*
|
||||
* @param in the XML input file
|
||||
* @param out the text output file
|
||||
* @param xslt the XSLT specification, i.e. an XSL style sheet
|
||||
* @throws FileNotFoundException
|
||||
* @throws TransformerException
|
||||
*/
|
||||
private static void transform(final File in, final File out, final File xslt)
|
||||
throws FileNotFoundException, TransformerException
|
||||
{
|
||||
final StreamSource ss = new StreamSource(xslt);
|
||||
final TransformerFactory tf = TransformerFactory.newInstance();
|
||||
final Transformer t;
|
||||
try
|
||||
{
|
||||
t = tf.newTransformer(ss);
|
||||
}
|
||||
catch (TransformerException ex)
|
||||
{
|
||||
System.err.println("Error compiling XSL style sheet " + xslt);
|
||||
throw ex;
|
||||
}
|
||||
final Properties p = new Properties();
|
||||
p.setProperty(OutputKeys.METHOD, "text");
|
||||
t.setOutputProperties(p);
|
||||
final Result result = new StreamResult(out);
|
||||
t.transform(new StreamSource(in), result);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user