From: Nick Burch Date: Thu, 27 Dec 2007 09:12:57 +0000 (+0000) Subject: Shuffle a few ooxml bits about, improve the ant tasks for it, and add in a ooxml... X-Git-Tag: REL_3_0_3_BETA1~240 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=bd40f5a0188e871ff21e4e61ef7c0a052fd72fe3;p=poi.git Shuffle a few ooxml bits about, improve the ant tasks for it, and add in a ooxml container lister git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@607024 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/build.xml b/build.xml index 88bc47c20b..0db3160735 100644 --- a/build.xml +++ b/build.xml @@ -145,6 +145,7 @@ under the License. + @@ -262,9 +263,11 @@ under the License. + + @@ -348,7 +351,18 @@ under the License. - + + + + + + + + + + @@ -452,7 +466,8 @@ under the License. - + diff --git a/src/scratchpad/ooxml-src/org/apache/poi/HXFDocument.java b/src/scratchpad/ooxml-src/org/apache/poi/HXFDocument.java deleted file mode 100644 index 427e377fa0..0000000000 --- a/src/scratchpad/ooxml-src/org/apache/poi/HXFDocument.java +++ /dev/null @@ -1,89 +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; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; - -import org.dom4j.Document; -import org.dom4j.DocumentException; -import org.dom4j.io.SAXReader; -import org.openxml4j.exceptions.InvalidFormatException; -import org.openxml4j.exceptions.OpenXML4JException; -import org.openxml4j.opc.Package; -import org.openxml4j.opc.PackageAccess; -import org.openxml4j.opc.PackagePart; - -/** - * Parent class of the low level interface to - * all POI XML (OOXML) implementations. - * Normal users should probably deal with things that - * extends {@link POIXMLDocument}, unless they really - * do need to get low level access to the files. - * - * WARNING - APIs expected to change rapidly - */ -public abstract class HXFDocument { - /** - * File package/container. - */ - protected Package container; - /** - * The Package Part for our base document - */ - protected PackagePart basePart; - /** - * The base document of this instance, eg Workbook for - * xslsx - */ - protected Document baseDocument; - - protected HXFDocument(Package container, String baseContentType) throws OpenXML4JException { - this.container = container; - - // Find the base document - ArrayList baseParts = - container.getPartsByContentType(baseContentType); - if(baseParts.size() != 1) { - throw new OpenXML4JException("Expecting one entry with content type of " + baseContentType + ", but found " + baseParts.size()); - } - basePart = baseParts.get(0); - - // And load it up - try { - SAXReader reader = new SAXReader(); - baseDocument = reader.read(basePart.getInputStream()); - } catch (DocumentException e) { - throw new OpenXML4JException(e.getMessage()); - } catch (IOException ioe) { - throw new OpenXML4JException(ioe.getMessage()); - } - } - - public static Package openPackage(File f) throws InvalidFormatException { - return Package.open(f.toString(), PackageAccess.READ_WRITE); - } - - /** - * Get the package container. - * @return The package associated to this document. - */ - public Package getPackage() { - return container; - } -} diff --git a/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java b/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java index 20707aac82..f2a71fd467 100644 --- a/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java +++ b/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java @@ -18,7 +18,7 @@ package org.apache.poi.hssf; import java.io.IOException; -import org.apache.poi.HXFDocument; +import org.apache.poi.hxf.HXFDocument; import org.apache.xmlbeans.XmlException; import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.opc.Package; diff --git a/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java b/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java new file mode 100644 index 0000000000..f8f74e0943 --- /dev/null +++ b/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java @@ -0,0 +1,90 @@ +/* ==================================================================== + 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.hxf; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.poi.POIXMLDocument; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.openxml4j.exceptions.InvalidFormatException; +import org.openxml4j.exceptions.OpenXML4JException; +import org.openxml4j.opc.Package; +import org.openxml4j.opc.PackageAccess; +import org.openxml4j.opc.PackagePart; + +/** + * Parent class of the low level interface to + * all POI XML (OOXML) implementations. + * Normal users should probably deal with things that + * extends {@link POIXMLDocument}, unless they really + * do need to get low level access to the files. + * + * WARNING - APIs expected to change rapidly + */ +public abstract class HXFDocument { + /** + * File package/container. + */ + protected Package container; + /** + * The Package Part for our base document + */ + protected PackagePart basePart; + /** + * The base document of this instance, eg Workbook for + * xslsx + */ + protected Document baseDocument; + + protected HXFDocument(Package container, String baseContentType) throws OpenXML4JException { + this.container = container; + + // Find the base document + ArrayList baseParts = + container.getPartsByContentType(baseContentType); + if(baseParts.size() != 1) { + throw new OpenXML4JException("Expecting one entry with content type of " + baseContentType + ", but found " + baseParts.size()); + } + basePart = baseParts.get(0); + + // And load it up + try { + SAXReader reader = new SAXReader(); + baseDocument = reader.read(basePart.getInputStream()); + } catch (DocumentException e) { + throw new OpenXML4JException(e.getMessage()); + } catch (IOException ioe) { + throw new OpenXML4JException(ioe.getMessage()); + } + } + + public static Package openPackage(File f) throws InvalidFormatException { + return Package.open(f.toString(), PackageAccess.READ_WRITE); + } + + /** + * Get the package container. + * @return The package associated to this document. + */ + public Package getPackage() { + return container; + } +} diff --git a/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java b/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java new file mode 100644 index 0000000000..d56d2682e8 --- /dev/null +++ b/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java @@ -0,0 +1,126 @@ +/* ==================================================================== + 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.hxf.dev; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; + +import org.openxml4j.opc.Package; +import org.openxml4j.opc.PackageAccess; +import org.openxml4j.opc.PackagePart; +import org.openxml4j.opc.PackageRelationship; +import org.openxml4j.opc.PackageRelationshipCollection; + +/** + * Prints out the contents of a HXF (ooxml) container. + * Useful for seeing what parts are defined, and how + * they're all related to each other. + */ +public class HXFLister { + private Package container; + private PrintStream disp; + + public HXFLister(Package container) { + this(container, System.out); + } + public HXFLister(Package container, PrintStream disp) { + this.container = container; + this.disp = disp; + } + + /** + * Figures out how big a given PackagePart is. + */ + public static long getSize(PackagePart part) throws IOException { + InputStream in = part.getInputStream(); + 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; + } + + /** + * Displays information on all the different + * parts of the OOXML file container. + */ + public void displayParts() throws Exception { + ArrayList 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"); + } + } + } + /** + * Displays information on all the different + * relationships between different parts + * of the OOXML file container. + */ + public void displayRelations() throws Exception { + PackageRelationshipCollection rels = + container.getRelationships(); + for (PackageRelationship rel : rels) { + disp.println("Relationship:"); + disp.println("\tFrom: "+ rel.getSourceURI()); + disp.println("\tTo: " + rel.getTargetURI()); + disp.println("\tMode: " + rel.getTargetMode()); + disp.println("\tType: " + rel.getRelationshipType()); + } + } + + public static void main(String[] args) throws Exception { + if(args.length == 0) { + System.err.println("Use:"); + System.err.println("\tjava HXFLister "); + System.exit(1); + } + + File f = new File(args[0]); + if(! f.exists()) { + System.err.println("Error, file not found!"); + System.err.println("\t" + f.toString()); + System.exit(2); + } + + HXFLister lister = new HXFLister( + Package.open(f.toString(), PackageAccess.READ) + ); + + lister.disp.println(f.toString() + "\n"); + lister.displayParts(); + lister.disp.println(); + lister.displayRelations(); + } +} diff --git a/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java b/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java index 1013d4f104..69d5a668a2 100644 --- a/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java +++ b/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java @@ -2,7 +2,7 @@ package org.apache.poi.hssf; import java.io.File; -import org.apache.poi.HXFDocument; +import org.apache.poi.hxf.HXFDocument; import org.openxml4j.opc.Package; import org.openxml4j.opc.PackagePart;