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
This commit is contained in:
Nick Burch 2007-12-27 11:52:12 +00:00
parent bd40f5a018
commit 7d409e8139
5 changed files with 138 additions and 7 deletions

View File

@ -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}"/>

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -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]));
}
}