diff options
author | Nick Burch <nick@apache.org> | 2007-12-27 11:52:12 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2007-12-27 11:52:12 +0000 |
commit | 7d409e8139c26f46a1020e55f88b7b13623f5814 (patch) | |
tree | 27796c7db64bae81777211c9c8da8ffe9395db22 | |
parent | bd40f5a0188e871ff21e4e61ef7c0a052fd72fe3 (diff) | |
download | poi-7d409e8139c26f46a1020e55f88b7b13623f5814.tar.gz poi-7d409e8139c26f46a1020e55f88b7b13623f5814.zip |
Support getting the low level sheet objects back for an xlsx document
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@607043 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | build.xml | 38 | ||||
-rw-r--r-- | src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java | 28 | ||||
-rw-r--r-- | src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java | 35 | ||||
-rw-r--r-- | src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java | 16 | ||||
-rw-r--r-- | src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java | 28 |
5 files changed, 138 insertions, 7 deletions
@@ -271,6 +271,7 @@ under the License. <mkdir dir="${main.reports.test}"/> <mkdir dir="${scratchpad.reports.test}"/> <mkdir dir="${contrib.reports.test}"/> + <mkdir dir="${ooxml.reports.test}"/> <mkdir dir="${junit.report.dir}"/> <mkdir dir="${jdepend.report.dir}"/> <mkdir dir="${jdepend.report.out.dir}"/> @@ -700,6 +701,43 @@ under the License. <echo file="${contrib.testokfile}" append="false" message="testok"/> </target> + <target name="-test-ooxml-check"> + <uptodate property="ooxml.test.notRequired" targetfile="${ooxml.testokfile}"> + <srcfiles dir="${ooxml.src}"/> + <srcfiles dir="${ooxml.src.test}"/> + </uptodate> + </target> + + <target name="test-ooxml" depends="compile-main,compile-ooxml,-test-ooxml-check" unless="ooxml.test.notRequired"> + <junit printsummary="yes" fork="no" haltonfailure="${halt.on.test.failure}" failureproperty="ooxml.test.failed"> + <classpath> + <path refid="ooxml.classpath"/> + <pathelement location="${main.output.dir}"/> + <pathelement location="${ooxml.output.dir}"/> + <pathelement location="${ooxml.output.test.dir}"/> + <pathelement location="${junit.jar1.dir}"/> + </classpath> + <sysproperty key="HSSF.testdata.path" file="${main.src.test}/org/apache/poi/hssf/data"/> + <sysproperty key="HWPF.testdata.path" file="${scratchpad.src.test}/org/apache/poi/hwpf/data"/> + <sysproperty key="HSLF.testdata.path" file="${scratchpad.src.test}/org/apache/poi/hslf/data"/> + <sysproperty key="java.awt.headless" value="true"/> + <formatter type="plain"/> + <formatter type="xml"/> + <batchtest todir="${ooxml.reports.test}"> + <fileset dir="${ooxml.src.test}"> + <include name="**/Test*.java"/> + <exclude name="**/AllTests.java"/> + </fileset> + </batchtest> + </junit> + <delete file="${ooxml.testokfile}"/> + <antcall target="-test-ooxml-write-testfile"/> + </target> + + <target name="-test-ooxml-write-testfile" unless="ooxml.test.failed"> + <echo file="${ooxml.testokfile}" append="false" message="testok"/> + </target> + <target name="-check-docs"> <uptodate property="main.docs.notRequired" targetfile="${build.site}/index.html"> <srcfiles dir="${build.site.src}"/> 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 f2a71fd467..b85ec33732 100644 --- a/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java +++ b/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java @@ -22,8 +22,13 @@ import org.apache.poi.hxf.HXFDocument; import org.apache.xmlbeans.XmlException; import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.opc.Package; +import org.openxml4j.opc.PackagePart; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheets; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument; /** * Experimental class to do low level processing @@ -45,7 +50,30 @@ public class HSSFXML extends HXFDocument { WorkbookDocument.Factory.parse(basePart.getInputStream()); } + /** + * Returns the low level workbook base object + */ public CTWorkbook getWorkbook() { return workbookDoc.getWorkbook(); } + /** + * Returns the references from the workbook to its + * sheets. + * You'll need these to figure out the sheet ordering, + * and to get at the actual sheets themselves + */ + public CTSheets getSheetReferences() { + return getWorkbook().getSheets(); + } + /** + * Returns the low level (work)sheet object from + * the supplied sheet reference + */ + public CTWorksheet getSheet(CTSheet sheet) throws IOException, XmlException { + PackagePart sheetPart = + getRelatedPackagePart(sheet.getId()); + WorksheetDocument sheetDoc = + WorksheetDocument.Factory.parse(sheetPart.getInputStream()); + return sheetDoc.getWorksheet(); + } } diff --git a/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java b/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java index f8f74e0943..a7a17f9421 100644 --- a/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java +++ b/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java @@ -29,6 +29,9 @@ import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.opc.Package; import org.openxml4j.opc.PackageAccess; import org.openxml4j.opc.PackagePart; +import org.openxml4j.opc.PackagePartName; +import org.openxml4j.opc.PackageRelationship; +import org.openxml4j.opc.PackagingURIHelper; /** * Parent class of the low level interface to @@ -76,8 +79,28 @@ public abstract class HXFDocument { } } - public static Package openPackage(File f) throws InvalidFormatException { - return Package.open(f.toString(), PackageAccess.READ_WRITE); + /** + * Retrieves the PackagePart for the given relation + * id. This will normally come from a r:id attribute + * on part of the base document. + * @param partId The r:id pointing to the other PackagePart + */ + protected PackagePart getRelatedPackagePart(String partId) { + PackageRelationship rel = + basePart.getRelationship(partId); + + PackagePartName relName; + try { + relName = PackagingURIHelper.createPartName(rel.getTargetURI()); + } catch(InvalidFormatException e) { + throw new InternalError(e.getMessage()); + } + + PackagePart part = container.getPart(relName); + if(part == null) { + throw new IllegalArgumentException("No part found for rel " + rel); + } + return part; } /** @@ -87,4 +110,12 @@ public abstract class HXFDocument { public Package getPackage() { return container; } + + /** + * Returns an opened OOXML Package for the supplied File + * @param f File to open + */ + public static Package openPackage(File f) throws InvalidFormatException { + return Package.open(f.toString(), PackageAccess.READ_WRITE); + } } 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 index d56d2682e8..3b0a540d37 100644 --- a/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java +++ b/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java @@ -80,6 +80,9 @@ public class HXFLister { if(! part.isRelationshipPart()) { disp.println("\t" + part.getRelationships().size() + " relations"); + for(PackageRelationship rel : part.getRelationships()) { + displayRelation(rel, "\t "); + } } } } @@ -92,13 +95,16 @@ public class HXFLister { 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()); + 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+"\tMode: " + rel.getTargetMode()); + disp.println(indent+"\tType: " + rel.getRelationshipType()); + } public static void main(String[] args) throws Exception { if(args.length == 0) { 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 69d5a668a2..4634060399 100644 --- a/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java +++ b/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java @@ -5,6 +5,7 @@ import java.io.File; import org.apache.poi.hxf.HXFDocument; import org.openxml4j.opc.Package; import org.openxml4j.opc.PackagePart; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import junit.framework.TestCase; @@ -64,5 +65,32 @@ public class TestHSSFXML extends TestCase { xml = new HSSFXML( HXFDocument.openPackage(sampleFile) ); + + // Check it has a workbook + assertNotNull(xml.getWorkbook()); + } + + public void testSheetBasics() throws Exception { + HSSFXML xml = new HSSFXML( + HXFDocument.openPackage(sampleFile) + ); + + // Should have three sheets + assertEquals(3, xml.getSheetReferences().sizeOfSheetArray()); + assertEquals(3, xml.getSheetReferences().getSheetArray().length); + + // Check they're as expected + CTSheet[] sheets = xml.getSheetReferences().getSheetArray(); + assertEquals("Sheet1", sheets[0].getName()); + assertEquals("Sheet2", sheets[1].getName()); + assertEquals("Sheet3", sheets[2].getName()); + assertEquals("rId1", sheets[0].getId()); + assertEquals("rId2", sheets[1].getId()); + assertEquals("rId3", sheets[2].getId()); + + // Now get those objects + assertNotNull(xml.getSheet(sheets[0])); + assertNotNull(xml.getSheet(sheets[1])); + assertNotNull(xml.getSheet(sheets[2])); } }
\ No newline at end of file |