Update to ooxml embeding from bug #45018 from Yury

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@660945 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-05-28 13:32:03 +00:00
parent 804cc620e7
commit 627c7cc083
8 changed files with 53 additions and 11 deletions

View File

@ -41,8 +41,12 @@ public abstract class POIXMLDocument {
public static final String EXTENDED_PROPERTIES_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"; public static final String EXTENDED_PROPERTIES_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties";
// OLE embeddings relation name
public static final String OLE_OBJECT_REL_TYPE="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject"; public static final String OLE_OBJECT_REL_TYPE="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject";
// Embedded OPC documents relation name
public static final String PACK_OBJECT_REL_TYPE="http://schemas.openxmlformats.org/officeDocument/2006/relationships/package";
/** The OPC Package */ /** The OPC Package */
private Package pkg; private Package pkg;
@ -57,7 +61,7 @@ public abstract class POIXMLDocument {
/** /**
* The embedded OLE2 files in the OPC package * The embedded OLE2 files in the OPC package
*/ */
private List<PackagePart> embedds; protected List<PackagePart> embedds = new LinkedList<PackagePart>();
protected POIXMLDocument() {} protected POIXMLDocument() {}
@ -71,15 +75,11 @@ public abstract class POIXMLDocument {
// Get core part // Get core part
this.corePart = this.pkg.getPart(coreDocRelationship); this.corePart = this.pkg.getPart(coreDocRelationship);
// Get any embedded OLE2 documents
this.embedds = new LinkedList<PackagePart>();
for(PackageRelationship rel : corePart.getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
embedds.add(getTargetPart(rel));
}
} catch (OpenXML4JException e) { } catch (OpenXML4JException e) {
throw new IOException(e.toString()); throw new IOException(e.toString());
} }
} }
protected POIXMLDocument(String path) throws IOException { protected POIXMLDocument(String path) throws IOException {
this(openPackage(path)); this(openPackage(path));
} }

View File

@ -24,6 +24,7 @@ import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.exceptions.OpenXML4JException;
import org.openxml4j.opc.Package; import org.openxml4j.opc.Package;
import org.openxml4j.opc.PackagePart; import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackageRelationshipCollection; import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesSlide; import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesSlide;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentation; import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentation;
@ -63,6 +64,17 @@ public class XSLFSlideShow extends POIXMLDocument {
presentationDoc = presentationDoc =
PresentationDocument.Factory.parse(getCorePart().getInputStream()); PresentationDocument.Factory.parse(getCorePart().getInputStream());
for (CTSlideIdListEntry ctSlide : getSlideReferences().getSldIdArray()) {
PackagePart slidePart =
getTargetPart(getCorePart().getRelationship(ctSlide.getId2()));
for(PackageRelationship rel : slidePart.getRelationshipsByType(OLE_OBJECT_REL_TYPE))
embedds.add(getTargetPart(rel)); // TODO: Add this reference to each slide as well
for(PackageRelationship rel : slidePart.getRelationshipsByType(PACK_OBJECT_REL_TYPE))
embedds.add(getTargetPart(rel));
}
} }
public XSLFSlideShow(String file) throws OpenXML4JException, IOException, XmlException { public XSLFSlideShow(String file) throws OpenXML4JException, IOException, XmlException {
this(openPackage(file)); this(openPackage(file));

View File

@ -121,6 +121,20 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
null, null,
null null
); );
public static final XSSFRelation OLEEMBEDDINGS = new XSSFRelation(
null,
OLE_OBJECT_REL_TYPE,
null,
null
);
public static final XSSFRelation PACKEMBEDDINGS = new XSSFRelation(
null,
PACK_OBJECT_REL_TYPE,
null,
null
);
public static class XSSFRelation { public static class XSSFRelation {
private String TYPE; private String TYPE;
@ -292,6 +306,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
PackageRelationshipCollection hyperlinkRels = PackageRelationshipCollection hyperlinkRels =
part.getRelationshipsByType(SHEET_HYPERLINKS.REL); part.getRelationshipsByType(SHEET_HYPERLINKS.REL);
sheet.initHyperlinks(hyperlinkRels); sheet.initHyperlinks(hyperlinkRels);
// Get the embeddings for the workbook
for(PackageRelationship rel : part.getRelationshipsByType(OLEEMBEDDINGS.REL))
embedds.add(getTargetPart(rel)); // TODO: Add this reference to each sheet as well
for(PackageRelationship rel : part.getRelationshipsByType(PACKEMBEDDINGS.REL))
embedds.add(getTargetPart(rel));
} }
} catch (XmlException e) { } catch (XmlException e) {
throw new IOException(e.toString()); throw new IOException(e.toString());

View File

@ -114,6 +114,15 @@ public class XWPFDocument extends POIXMLDocument {
tables.add(new XWPFTable(table)); tables.add(new XWPFTable(table));
} }
} }
this.embedds = new LinkedList<PackagePart>();
for(PackageRelationship rel : getCorePart().getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
embedds.add(getTargetPart(rel));
}
for(PackageRelationship rel : getCorePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
embedds.add(getTargetPart(rel));
}
} }
/** /**

View File

@ -49,7 +49,7 @@ public class TestEmbeded extends TestCase
assertTrue(f.exists()); assertTrue(f.exists());
POIXMLDocument doc = new XSSFWorkbook(Package.open(f.toString())); POIXMLDocument doc = new XSSFWorkbook(Package.open(f.toString()));
test(doc, 0); test(doc, 4);
} }
public void testWord() throws Exception { public void testWord() throws Exception {
@ -57,7 +57,7 @@ public class TestEmbeded extends TestCase
assertTrue(f.exists()); assertTrue(f.exists());
POIXMLDocument doc = new XWPFDocument(Package.open(f.toString())); POIXMLDocument doc = new XWPFDocument(Package.open(f.toString()));
test(doc, 4); test(doc, 5);
} }
public void testPowerPoint() throws Exception { public void testPowerPoint() throws Exception {
@ -65,7 +65,7 @@ public class TestEmbeded extends TestCase
assertTrue(f.exists()); assertTrue(f.exists());
POIXMLDocument doc = new XSLFSlideShow(Package.open(f.toString())); POIXMLDocument doc = new XSLFSlideShow(Package.open(f.toString()));
test(doc, 0); test(doc, 4);
} }
private void test(POIXMLDocument doc, int expectedCount) throws Exception { private void test(POIXMLDocument doc, int expectedCount) throws Exception {