From ae3bd545334ee70b7f7c2ea2ef7c454dfdd3ac89 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 27 Dec 2007 11:52:12 +0000 Subject: [PATCH] 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 --- build.xml | 38 +++++++++++++++++++ .../org/apache/poi/hssf/HSSFXML.java | 28 ++++++++++++++ .../org/apache/poi/hxf/HXFDocument.java | 35 ++++++++++++++++- .../org/apache/poi/hxf/dev/HXFLister.java | 16 +++++--- .../org/apache/poi/hssf/TestHSSFXML.java | 28 ++++++++++++++ 5 files changed, 138 insertions(+), 7 deletions(-) diff --git a/build.xml b/build.xml index 0db316073..a11bb9426 100644 --- a/build.xml +++ b/build.xml @@ -271,6 +271,7 @@ under the License. + @@ -700,6 +701,43 @@ under the License. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 f2a71fd46..b85ec3373 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 f8f74e094..a7a17f942 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 d56d2682e..3b0a540d3 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 69d5a668a..463406039 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