]> source.dussan.org Git - poi.git/commitdiff
New PackagePart method getRelatedPart(PackageRelationship) to simplify navigation...
authorNick Burch <nick@apache.org>
Mon, 19 Sep 2011 21:40:39 +0000 (21:40 +0000)
committerNick Burch <nick@apache.org>
Mon, 19 Sep 2011 21:40:39 +0000 (21:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1172853 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/POIXMLDocument.java
src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePart.java
src/ooxml/java/org/apache/poi/xslf/XSLFSlideShow.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java

index bfcdf5aa4562c14175f583bc46844c283c1a0841..507b7919901c808585fc825a7b24a459982539c3 100644 (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>
index 075c9c84fe7658e3a6635c465b1430551feee2b9..fa41a5744f306e1c48a6f64d927b9a82b189f215 100644 (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;
index 311573618aa43408110e17c8b4493cce5c24a263..880af27650678175c4c3f65f730a7067ae6a52f3 100644 (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
      */
index 36bcc37a3c5ef6c28e1334d8a543d43107cf5ffd..6371c13ce5309e4812449fa8df556d9da4619019 100644 (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.
         *
index d2986dc3fcde4f93ab00bf3442e61896d7e74799..09dccdff1e1d7399f8a91ee69ff7be6d4d79adf0 100644 (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 = 
index 9b0c7219af445f48e3cf03cdc3762eabcbb8af61..80112817870faa34f9b9a3d9d7354faa4168245b 100644 (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;
     }
index c213d5790cbded31bcc87162a802610cb3b43040..231103ed55fe811941a818770464620afccefcdc 100644 (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;