New PackagePart method getRelatedPart(PackageRelationship) to simplify navigation of relations between OPC Parts

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1172853 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-09-19 21:40:39 +00:00
parent 7d6c26cf86
commit 0446976e0f
7 changed files with 72 additions and 49 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta5" date="2011-??-??">
<action dev="poi-developers" type="add">New PackagePart method getRelatedPart(PackageRelationship) to simplify navigation of relations between OPC Parts</action>
<action dev="poi-developers" type="fix">51832 - handle XLS files where the WRITEPROTECT record preceeds the FILEPASS one, rather than following as normal</action>
<action dev="poi-developers" type="fix">51809 - correct GTE handling in COUNTIF</action>
<action dev="poi-developers" type="add">Add HWPF API to update range text and delete bookmarks</action>

View File

@ -90,7 +90,7 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{
PackagePart[] parts = new PackagePart[partsC.size()];
int count = 0;
for (PackageRelationship rel : partsC) {
parts[count] = getTargetPart(rel);
parts[count] = getPackagePart().getRelatedPart(rel);
count++;
}
return parts;

View File

@ -61,23 +61,6 @@ public class POIXMLDocumentPart {
private POIXMLDocumentPart parent;
private Map<String,POIXMLDocumentPart> relations = new LinkedHashMap<String,POIXMLDocumentPart>();
/**
* Get the PackagePart that is the target of a relationship.
*
* @param rel The relationship
* @param pkg The package to fetch from
* @return The target part
* @throws InvalidFormatException
*/
protected static PackagePart getTargetPart(OPCPackage pkg, PackageRelationship rel)
throws InvalidFormatException {
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
PackagePart part = pkg.getPart(relName);
if (part == null) {
throw new IllegalArgumentException("No part found for relationship " + rel);
}
return part;
}
/**
* Counter that provides the amount of incoming relations from other parts
* to this part.
@ -159,7 +142,7 @@ public class POIXMLDocumentPart {
);
}
packageRel = cores.getRelationship(0);
packagePart = POIXMLDocument.getTargetPart(pkg, packageRel);
packagePart = packagePart.getRelatedPart(packageRel);
}
/**
@ -425,6 +408,18 @@ public class POIXMLDocumentPart {
}
}
}
/**
* Get the PackagePart that is the target of a relationship from this Part.
*
* @param rel The relationship
* @return The target part
* @throws InvalidFormatException
*/
protected PackagePart getTargetPart(PackageRelationship rel) throws InvalidFormatException {
return getPackagePart().getRelatedPart(rel);
}
/**
* Fired when a new package part is created
@ -440,17 +435,6 @@ public class POIXMLDocumentPart {
}
/**
* Get the PackagePart that is the target of a relationship.
*
* @param rel The relationship
* @return The target part
* @throws InvalidFormatException
*/
protected PackagePart getTargetPart(PackageRelationship rel) throws InvalidFormatException {
return getTargetPart(getPackagePart().getPackage(), rel);
}
/**
* Fired when a package part is about to be removed from the package
*/

View File

@ -456,6 +456,38 @@ public abstract class PackagePart implements RelationshipSource {
return false;
}
/**
* Get the PackagePart that is the target of a relationship.
*
* @param rel A relationship from this part to another one
* @return The target part of the relationship
*/
public PackagePart getRelatedPart(PackageRelationship rel) throws InvalidFormatException {
// Ensure this is one of ours
if(! isRelationshipExists(rel)) {
throw new IllegalArgumentException("Relationship " + rel + " doesn't start with this part " + _partName);
}
// Get the target URI, excluding any relative fragments
URI target = rel.getTargetURI();
if(target.getFragment() != null) {
String t = target.toString();
try {
target = new URI( t.substring(0, t.indexOf('#')) );
} catch(URISyntaxException e) {
throw new InvalidFormatException("Invalid target URI: " + target);
}
}
// Turn that into a name, and fetch
PackagePartName relName = PackagingURIHelper.createPartName(target);
PackagePart part = _container.getPart(relName);
if (part == null) {
throw new IllegalArgumentException("No part found for relationship " + rel);
}
return part;
}
/**
* Get the input stream of this part to read its content.
*

View File

@ -78,14 +78,15 @@ public class XSLFSlideShow extends POIXMLDocument {
embedds = new LinkedList<PackagePart>();
for (CTSlideIdListEntry ctSlide : getSlideReferences().getSldIdList()) {
PackagePart slidePart =
getTargetPart(getCorePart().getRelationship(ctSlide.getId2()));
PackagePart corePart = getCorePart();
PackagePart slidePart = corePart.getRelatedPart(
corePart.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
embedds.add(slidePart.getRelatedPart(rel)); // TODO: Add this reference to each slide as well
for(PackageRelationship rel : slidePart.getRelationshipsByType(PACK_OBJECT_REL_TYPE))
embedds.add(getTargetPart(rel));
embedds.add(slidePart.getRelatedPart(rel));
}
}
public XSLFSlideShow(String file) throws OpenXML4JException, IOException, XmlException {
@ -129,8 +130,9 @@ public class XSLFSlideShow extends POIXMLDocument {
public PackagePart getSlideMasterPart(CTSlideMasterIdListEntry master) throws IOException, XmlException {
try {
return getTargetPart(
getCorePart().getRelationship(master.getId2())
PackagePart corePart = getCorePart();
return corePart.getRelatedPart(
corePart.getRelationship(master.getId2())
);
} catch(InvalidFormatException e) {
throw new XmlException(e);
@ -150,9 +152,10 @@ public class XSLFSlideShow extends POIXMLDocument {
public PackagePart getSlidePart(CTSlideIdListEntry slide) throws IOException, XmlException {
try {
return getTargetPart(
getCorePart().getRelationship(slide.getId2())
);
PackagePart corePart = getCorePart();
return corePart.getRelatedPart(
corePart.getRelationship(slide.getId2())
);
} catch(InvalidFormatException e) {
throw new XmlException(e);
}
@ -192,7 +195,7 @@ public class XSLFSlideShow extends POIXMLDocument {
}
try {
return getTargetPart(notes.getRelationship(0));
return slidePart.getRelatedPart(notes.getRelationship(0));
} catch(InvalidFormatException e) {
throw new IllegalStateException(e);
}
@ -236,7 +239,7 @@ public class XSLFSlideShow extends POIXMLDocument {
}
try {
PackagePart cPart = getTargetPart(
PackagePart cPart = slidePart.getRelatedPart(
commentRels.getRelationship(0)
);
CmLstDocument commDoc =

View File

@ -1333,12 +1333,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
for(XSSFSheet sheet : sheets){
// Get the embeddings for the workbook
for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation()))
embedds.add(getTargetPart(rel));
for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.OLEEMBEDDINGS.getRelation())) {
embedds.add( sheet.getPackagePart().getRelatedPart(rel) );
}
for(PackageRelationship rel : sheet.getPackagePart().getRelationshipsByType(XSSFRelation.PACKEMBEDDINGS.getRelation())) {
embedds.add( sheet.getPackagePart().getRelatedPart(rel) );
}
}
return embedds;
}

View File

@ -387,7 +387,8 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
*/
public PackagePart getPartById(String id) {
try {
return getTargetPart(getCorePart().getRelationship(id));
PackagePart corePart = getCorePart();
return corePart.getRelatedPart(corePart.getRelationship(id));
} catch (InvalidFormatException e) {
throw new IllegalArgumentException(e);
}
@ -428,12 +429,13 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
List<PackagePart> embedds = new LinkedList<PackagePart>();
// Get the embeddings for the workbook
PackagePart part = getPackagePart();
for (PackageRelationship rel : getPackagePart().getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
embedds.add(getTargetPart(rel));
embedds.add(part.getRelatedPart(rel));
}
for (PackageRelationship rel : getPackagePart().getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
embedds.add(getTargetPart(rel));
embedds.add(part.getRelatedPart(rel));
}
return embedds;