]> source.dussan.org Git - poi.git/commitdiff
51187 - fixed OPCPackage to correctly handle self references
authorYegor Kozlov <yegor@apache.org>
Thu, 11 Aug 2011 08:13:00 +0000 (08:13 +0000)
committerYegor Kozlov <yegor@apache.org>
Thu, 11 Aug 2011 08:13:00 +0000 (08:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1156529 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java

index 1633fdc4853693b0bcf22811e1866cfe2c73750e..ac2112f65561028ab4f3c8ffcfa15e6c7ee57cd8 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="fix">51187 - fixed OPCPackage to correctly handle self references</action>
            <action dev="poi-developers" type="fix">51635 - Improved performance of XSSFSheet#write</action>
            <action dev="poi-developers" type="fix">47731 - Word Extractor considers text copied from some website as an embedded object</action>
            <action dev="poi-developers" type="add">Add Word-to-Text converter and use it as replacement for WordExtractor</action>
index 7d33bdd022ee4052bb08d0c41e900912660d4d36..be13cf0f05eaeca63dad87f1ff82728635b62b48 100644 (file)
@@ -342,7 +342,16 @@ public final class PackagingURIHelper {
                // Special case for where the two are the same
                if (segmentsTheSame == segmentsSource.length
                                && segmentsTheSame == segmentsTarget.length) {
-                       retVal.append("");
+            if(sourceURI.equals(targetURI)){
+                // if source and target are the same they should be resolved to the last segment,
+                // Example: if a slide references itself, e.g. the source URI is
+                // "/ppt/slides/slide1.xml" and the targetURI is "slide1.xml" then
+                // this it should be relativized as "slide1.xml", i.e. the last segment.
+                retVal.append(segmentsSource[segmentsSource.length - 1]);
+            } else {
+                retVal.append("");
+            }
+
                } else {
                        // Matched for so long, but no more
 
index 9e2297d53049290156f1a30fbc06a941c2fce1a0..24d19a931f3b1c384c5407141aa564af64f1691c 100644 (file)
@@ -53,7 +53,10 @@ public class TestPackagingURIHelper extends TestCase {
 
                // Document to itself is the same place (empty URI)
                URI retURI2 = PackagingURIHelper.relativizeURI(uri1, uri1);
-               assertEquals("", retURI2.getPath());
+               // YK: the line below used to assert empty string which is wrong
+        // if source and target are the same they should be relaitivized as the last segment,
+        // see Bugzilla 51187
+        assertEquals("document.xml", retURI2.getPath());
 
                // relativization against root
                URI root = new URI("/");
index 7711b1d0c8b4b92a87ba610fe0ae54db408a9f26..738175f54d5d2c83fbb590c55f466a69a32a02b5 100644 (file)
@@ -313,4 +313,35 @@ public class TestRelationships extends TestCase {
         assertEquals("'\u0410\u043F\u0430\u0447\u0435 \u041F\u041E\u0418'!A5", rel6.getFragment());
     }
 
+   public void testSelfRelations_bug51187() throws Exception {
+       ByteArrayOutputStream baos = new ByteArrayOutputStream();
+       OPCPackage pkg = OPCPackage.create(baos);
+
+       PackagePart partA =
+               pkg.createPart(PackagingURIHelper.createPartName("/partA"), "text/plain");
+       assertNotNull(partA);
+
+       // reference itself
+       PackageRelationship rel1 = partA.addRelationship(partA.getPartName(), TargetMode.INTERNAL, "partA");
+
+       
+       // Save, and re-load
+       pkg.close();
+       ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+       pkg = OPCPackage.open(bais);
+
+       partA = pkg.getPart(PackagingURIHelper.createPartName("/partA"));
+
+
+       // Check the relations
+       assertEquals(1, partA.getRelationships().size());
+
+       PackageRelationship rel2 = partA.getRelationships().getRelationship(0);
+
+       assertEquals(rel1.getRelationshipType(), rel2.getRelationshipType());
+       assertEquals(rel1.getId(), rel2.getId());
+       assertEquals(rel1.getSourceURI(), rel2.getSourceURI());
+       assertEquals(rel1.getTargetURI(), rel2.getTargetURI());
+       assertEquals(rel1.getTargetMode(), rel2.getTargetMode());
+    }
 }
index d502902a7b057fd9edcd297d7310c81a140864e4..332164f8fa9a507d60e7261089d346d987866ae3 100644 (file)
@@ -52,7 +52,7 @@ public class TestXSLFBugs extends TestCase {
        assertEquals("/ppt/slideLayouts/slideLayout12.xml", slidePart.getRelationship("rId1").getTargetURI().toString());
        assertEquals("/ppt/notesSlides/notesSlide1.xml", slidePart.getRelationship("rId2").getTargetURI().toString());
        // TODO Fix this
-//       assertEquals("/ppt/slides/slide1.xml", slidePart.getRelationship("rId3").getTargetURI().toString());
+       assertEquals("/ppt/slides/slide1.xml", slidePart.getRelationship("rId3").getTargetURI().toString());
        assertEquals("/ppt/media/image1.png", slidePart.getRelationship("rId4").getTargetURI().toString());
     }
 }