poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r607043 - in /poi/trunk: ./ src/scratchpad/ooxml-src/org/apache/poi/hssf/ src/scratchpad/ooxml-src/org/apache/poi/hxf/ src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/ src/scratchpad/ooxml-testcases/org/apache/poi/hssf/
Date Thu, 27 Dec 2007 11:52:14 GMT
Author: nick
Date: Thu Dec 27 03:52:12 2007
New Revision: 607043

URL: http://svn.apache.org/viewvc?rev=607043&view=rev
Log:
Support getting the low level sheet objects back for an xlsx document

Modified:
    poi/trunk/build.xml
    poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java
    poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java
    poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java
    poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java

Modified: poi/trunk/build.xml
URL: http://svn.apache.org/viewvc/poi/trunk/build.xml?rev=607043&r1=607042&r2=607043&view=diff
==============================================================================
--- poi/trunk/build.xml (original)
+++ poi/trunk/build.xml Thu Dec 27 03:52:12 2007
@@ -271,6 +271,7 @@
         <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}"/>
@@ -698,6 +699,43 @@
 
     <target name="-test-contrib-write-testfile" unless="contrib.test.failed">
         <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">

Modified: poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java?rev=607043&r1=607042&r2=607043&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java (original)
+++ poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hssf/HSSFXML.java Thu Dec 27 03:52:12
2007
@@ -22,8 +22,13 @@
 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 @@
 			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();
 	}
 }

Modified: poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java?rev=607043&r1=607042&r2=607043&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java (original)
+++ poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java Thu Dec 27 03:52:12
2007
@@ -29,6 +29,9 @@
 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 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;
 	}
 
 	/**
@@ -86,5 +109,13 @@
 	 */
 	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);
 	}
 }

Modified: poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java?rev=607043&r1=607042&r2=607043&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java (original)
+++ poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/dev/HXFLister.java Thu Dec 27 03:52:12
2007
@@ -80,6 +80,9 @@
 			
 			if(! part.isRelationshipPart()) {
 				disp.println("\t" + part.getRelationships().size() + " relations");
+				for(PackageRelationship rel : part.getRelationships()) {
+					displayRelation(rel, "\t  ");
+				}
 			}
 		}
 	}
@@ -92,12 +95,15 @@
 		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 {

Modified: poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java?rev=607043&r1=607042&r2=607043&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java (original)
+++ poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java Thu Dec
27 03:52:12 2007
@@ -5,6 +5,7 @@
 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 @@
 		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]));
 	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message