Author: nick
Date: Mon Feb  4 15:06:46 2013
New Revision: 1442148

URL: http://svn.apache.org/viewvc?rev=1442148&view=rev
Log:
Improve the number of steps when generating an ID of a new relationship, and 
add more tests, bug #53904

Modified:
    
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
    
poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java?rev=1442148&r1=1442147&r2=1442148&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
 (original)
+++ 
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
 Mon Feb  4 15:06:46 2013
@@ -72,6 +72,12 @@ public final class PackageRelationshipCo
         * Reference to the package.
         */
        private OPCPackage container;
+       
+       /**
+        * The ID number of the next rID# to generate, or -1
+        *  if that is still to be determined.
+        */
+       private int nextRelationshipId = -1;
 
        /**
         * Constructor.
@@ -206,14 +212,17 @@ public final class PackageRelationshipCo
         */
        public PackageRelationship addRelationship(URI targetUri,
                        TargetMode targetMode, String relationshipType, String 
id) {
-
-               if (id == null) {
-                       // Generate a unique ID is id parameter is null.
-                       int i = 0;
-                       do {
-                               id = "rId" + ++i;
-                       } while (relationshipsByID.get(id) != null);
-               }
+      if (id == null) {
+         // Generate a unique ID is id parameter is null.
+         if (nextRelationshipId == -1) {
+            nextRelationshipId = size() + 1;
+         }
+
+         // Work up until we find a unique number (there could be gaps etc)
+         do {
+            id = "rId" + nextRelationshipId++;
+         } while (relationshipsByID.get(id) != null);
+      }
 
                PackageRelationship rel = new PackageRelationship(container,
                                sourcePart, targetUri, targetMode, 
relationshipType, id);

Modified: 
poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java?rev=1442148&r1=1442147&r2=1442148&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
 (original)
+++ 
poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
 Mon Feb  4 15:06:46 2013
@@ -254,6 +254,25 @@ public class TestRelationships extends T
        // Check core too
        assertEquals("/docProps/core.xml",
                        
pkg.getRelationshipsByType("http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";).getRelationship(0).getTargetURI().toString());
+       
+       
+       // Add some more
+      partB.addExternalRelationship("http://poi.apache.org/new";, 
"http://example/poi/new";);
+      partB.addExternalRelationship("http://poi.apache.org/alt";, 
"http://example/poi/alt";);
+      
+      // Check the relations
+      assertEquals(2, partA.getRelationships().size());
+      assertEquals(3, partB.getRelationships().size());
+      
+      assertEquals("/partB", 
partA.getRelationship("rId1").getTargetURI().toString());
+      assertEquals("http://poi.apache.org/";, 
+            partA.getRelationship("rId2").getTargetURI().toString());
+      assertEquals("http://poi.apache.org/ss/";, 
+            partB.getRelationship("rId1").getTargetURI().toString());
+      assertEquals("http://poi.apache.org/new";, 
+            partB.getRelationship("rId2").getTargetURI().toString());
+      assertEquals("http://poi.apache.org/alt";, 
+            partB.getRelationship("rId3").getTargetURI().toString());
     }
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to