From eb89ca36858a3aa86bf4a7db2a9c19081dbc69d9 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Thu, 11 Aug 2011 08:13:00 +0000 Subject: [PATCH] 51187 - fixed OPCPackage to correctly handle self references git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1156529 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/openxml4j/opc/PackagingURIHelper.java | 11 ++++++- .../openxml4j/opc/TestPackagingURIHelper.java | 5 ++- .../poi/openxml4j/opc/TestRelationships.java | 31 +++++++++++++++++++ .../org/apache/poi/xslf/TestXSLFBugs.java | 2 +- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 1633fdc485..ac2112f655 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51187 - fixed OPCPackage to correctly handle self references 51635 - Improved performance of XSSFSheet#write 47731 - Word Extractor considers text copied from some website as an embedded object Add Word-to-Text converter and use it as replacement for WordExtractor diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java index 7d33bdd022..be13cf0f05 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java @@ -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 diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java index 9e2297d530..24d19a931f 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackagingURIHelper.java @@ -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("/"); diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java index 7711b1d0c8..738175f54d 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java @@ -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()); + } } diff --git a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java index d502902a7b..332164f8fa 100644 --- a/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java @@ -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()); } }