You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2012/10/29 09:42:55 UTC

svn commit: r1403195 - in /poi/trunk: src/documentation/content/xdocs/ src/ooxml/java/org/apache/poi/openxml4j/opc/ src/ooxml/testcases/org/apache/poi/openxml4j/opc/ src/ooxml/testcases/org/apache/poi/xssf/usermodel/ test-data/openxml4j/ test-data/spre...

Author: yegor
Date: Mon Oct 29 08:42:54 2012
New Revision: 1403195

URL: http://svn.apache.org/viewvc?rev=1403195&view=rev
Log:
Bug #53282 - Avoid exception when parsing OPC relationships with non-breaking spaces

Added:
    poi/trunk/test-data/openxml4j/53282.xlsx   (with props)
    poi/trunk/test-data/spreadsheet/53282.xlsx   (with props)
Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestRelationships.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1403195&r1=1403194&r2=1403195&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Mon Oct 29 08:42:54 2012
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.9-beta1" date="2012-??-??">
+          <action dev="poi-developers" type="fix">53282 - Avoid exception when parsing OPC relationships with non-breaking spaces</action>
           <action dev="poi-developers" type="fix">54016 - Avoid exception when parsing workbooks with DConRefRecord in row aggregate</action>
           <action dev="poi-developers" type="fix">54008 - Fixed Ant build to support build directories with blanks</action>
           <action dev="poi-developers" type="fix">53374 - Avoid exceptions when parsing hyperlinks of type "javascript://"</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java?rev=1403195&r1=1403194&r2=1403195&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java Mon Oct 29 08:42:54 2012
@@ -709,6 +709,25 @@ public final class PackagingURIHelper {
             value = path + "#" + encode(fragment);
         }
 
+        // trailing white spaces must be url-encoded, see Bugzilla 53282
+        if(value.length() > 0 ){
+            StringBuilder b = new StringBuilder();
+            int idx = value.length() - 1;
+            for(; idx >= 0; idx--){
+                char c = value.charAt(idx);
+                if(Character.isWhitespace(c) || c == '\u00A0') {
+                    b.append(c);
+                } else {
+                    break;
+                }
+            }
+            if(b.length() > 0){
+                value = value.substring(0, idx+1) + encode(b.reverse().toString());
+            }
+        }
+
+        // MS Office can insert URIs with missing authority, e.g. "http://" or "javascript://"
+        // append a forward slash to avoid parse exception
         if(missingAuthPattern.matcher(value).matches()){
             value += "/";
         }
@@ -756,7 +775,7 @@ public final class PackagingURIHelper {
     };
 
     private static boolean isUnsafe(int ch) {
-        return ch > 0x80 || " ".indexOf(ch) >= 0;
+        return ch > 0x80 || Character.isWhitespace(ch) || ch == '\u00A0';
     }
 
 }

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=1403195&r1=1403194&r2=1403195&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 Oct 29 08:42:54 2012
@@ -19,6 +19,7 @@ package org.apache.poi.openxml4j.opc;
 
 import java.io.*;
 import java.net.URI;
+import java.util.regex.Pattern;
 
 import junit.framework.TestCase;
 
@@ -344,4 +345,32 @@ public class TestRelationships extends T
        assertEquals(rel1.getTargetURI(), rel2.getTargetURI());
        assertEquals(rel1.getTargetMode(), rel2.getTargetMode());
     }
+
+    public void testTrailingSpacesInURI_53282() throws Exception {
+        InputStream is = OpenXML4JTestDataSamples.openSampleStream("53282.xlsx");
+        OPCPackage pkg = OPCPackage.open(is);
+        is.close();
+
+        PackageRelationshipCollection sheetRels = pkg.getPartsByName(Pattern.compile("/xl/worksheets/sheet1.xml")).get(0).getRelationships();
+        assertEquals(3, sheetRels.size());
+        PackageRelationship rId1 = sheetRels.getRelationshipByID("rId1");
+        assertEquals(TargetMode.EXTERNAL, rId1.getTargetMode());
+        URI targetUri = rId1.getTargetURI();
+        assertEquals("mailto:nobody@nowhere.uk%C2%A0", targetUri.toASCIIString());
+        assertEquals("nobody@nowhere.uk\u00A0", targetUri.getSchemeSpecificPart());
+
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        pkg.save(out);
+        out.close();
+
+        pkg =  OPCPackage.open(new ByteArrayInputStream(out.toByteArray()));
+        sheetRels = pkg.getPartsByName(Pattern.compile("/xl/worksheets/sheet1.xml")).get(0).getRelationships();
+        assertEquals(3, sheetRels.size());
+        rId1 = sheetRels.getRelationshipByID("rId1");
+        assertEquals(TargetMode.EXTERNAL, rId1.getTargetMode());
+        targetUri = rId1.getTargetURI();
+        assertEquals("mailto:nobody@nowhere.uk%C2%A0", targetUri.toASCIIString());
+        assertEquals("nobody@nowhere.uk\u00A0", targetUri.getSchemeSpecificPart());
+
+    }
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java?rev=1403195&r1=1403194&r2=1403195&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java Mon Oct 29 08:42:54 2012
@@ -252,4 +252,14 @@ public final class TestXSSFHyperlink ext
         link = wb.getSheetAt(0).getRow(0).getCell(0).getHyperlink();
         assertEquals("javascript:///", link.getAddress());
     }
+
+    public void test53282() {
+        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53282.xlsx");
+        XSSFHyperlink link = wb.getSheetAt(0).getRow(0).getCell(14).getHyperlink();
+        assertEquals("mailto:nobody@nowhere.uk%C2%A0", link.getAddress());
+
+        wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+        link = wb.getSheetAt(0).getRow(0).getCell(14).getHyperlink();
+        assertEquals("mailto:nobody@nowhere.uk%C2%A0", link.getAddress());
+    }
 }

Added: poi/trunk/test-data/openxml4j/53282.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/openxml4j/53282.xlsx?rev=1403195&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/openxml4j/53282.xlsx
------------------------------------------------------------------------------
    svn:mime-type = application/vnd.ms-excel

Added: poi/trunk/test-data/spreadsheet/53282.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/53282.xlsx?rev=1403195&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/53282.xlsx
------------------------------------------------------------------------------
    svn:mime-type = application/vnd.ms-excel



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org