]> source.dussan.org Git - poi.git/commitdiff
validate hyperlink address as discussed on poi-user
authorYegor Kozlov <yegor@apache.org>
Thu, 2 Feb 2012 10:37:49 +0000 (10:37 +0000)
committerYegor Kozlov <yegor@apache.org>
Thu, 2 Feb 2012 10:37:49 +0000 (10:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1239529 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java

index 794df2f1357c2c160de7a07e718f4c042104370a..c2a3693f6d9ab7c1a95dbe1e5e04a4c6b91be60f 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta6" date="2012-??-??">
+           <action dev="poi-developers" type="add">Validate address of hyperlinks in XSSF</action>
            <action dev="poi-developers" type="fix">52540 - Relax the M4.1 constraint on reading OOXML files, as some Office produced ones do have 2 Core Properties, despite the specification explicitly forbidding this</action>
            <action dev="poi-developers" type="add">52462 - Added implementation for SUMIFS()</action>
            <action dev="poi-developers" type="add">POIXMLPropertiesTextExtractor support for extracting custom OOXML properties as text</action>
index 21fd54b844dcfeee9b2b54977e1df944010b8d1d..2fd47d9d4228b1483c77e2db54ebabdc8c1d2e29 100644 (file)
@@ -17,6 +17,7 @@
 package org.apache.poi.xssf.usermodel;
 
 import java.net.URI;
+import java.net.URISyntaxException;
 
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
@@ -181,18 +182,37 @@ public class XSSFHyperlink implements Hyperlink {
     }
 
     /**
-     * Hypelink address. Depending on the hyperlink type it can be URL, e-mail, path to a file
+     * Hyperlink address. Depending on the hyperlink type it can be URL, e-mail, path to a file
      *
      * @param address - the address of this hyperlink
      */
     public void setAddress(String address) {
-        _location = address;
+        validate(address);
+
+       _location = address;
         //we must set location for internal hyperlinks
         if (_type == Hyperlink.LINK_DOCUMENT) {
             setLocation(address);
         }
     }
 
+    private void validate(String address) {
+        switch (_type){
+            // email, path to file and url must be valid URIs
+            case Hyperlink.LINK_EMAIL:
+            case Hyperlink.LINK_FILE:
+            case Hyperlink.LINK_URL:
+                try {
+                    new URI(address);
+                } catch (URISyntaxException x) {
+                    IllegalArgumentException y = new IllegalArgumentException("Address of hyperlink must be a valid URI");
+                    y.initCause(x);
+                    throw y;
+                }
+                break;
+        }
+    }
+
     /**
      * Assigns this hyperlink to the given cell reference
      */
index 6e226e331b0d85cc51156a1a2b259c1ed829cff9..49f79365f33fbe077b550f3182841cb3571fbca1 100644 (file)
@@ -50,6 +50,35 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
                doTestHyperlinkContents(sheet);
        }
 
+    public void testCreate() {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        XSSFCreationHelper createHelper = workbook.getCreationHelper();
+        
+        String[] validURLs = {
+                "http://apache.org",
+                "www.apache.org",
+                "/temp",
+                "c:/temp",
+                "http://apache.org/default.php?s=isTramsformed&submit=Search&la=*&li=*"};
+        for(String s : validURLs){
+            createHelper.createHyperlink(Hyperlink.LINK_URL).setAddress(s);
+        }
+
+        String[] invalidURLs = {
+                "http:\\apache.org",
+                "www.apache .org",
+                "c:\\temp",
+                "\\poi"};
+        for(String s : invalidURLs){
+            try {
+                createHelper.createHyperlink(Hyperlink.LINK_URL).setAddress(s);
+                fail("expected IllegalArgumentException: " + s);
+            } catch (IllegalArgumentException e){
+                
+            }
+        }
+    }
+
        public void testLoadSave() {
                XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx");
                CreationHelper createHelper = workbook.getCreationHelper();