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
This commit is contained in:
Yegor Kozlov 2011-08-11 08:13:00 +00:00
parent 53769d29ad
commit dc100d1004
5 changed files with 47 additions and 3 deletions

View 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>

View 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

View 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("/");

View 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());
}
}

View 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());
}
}