]> source.dussan.org Git - poi.git/commitdiff
bug 58572: replace Cloneable with copy constructors for spreadsheet Hyperlink class
authorJaven O'Neal <onealj@apache.org>
Mon, 2 Nov 2015 12:57:57 +0000 (12:57 +0000)
committerJaven O'Neal <onealj@apache.org>
Mon, 2 Nov 2015 12:57:57 +0000 (12:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711951 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java
src/java/org/apache/poi/ss/usermodel/Hyperlink.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFHyperlink.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java

index f7010b5f18ebb4c213defd7525dd83c467376f5a..e8221264c7bbe4e72cd9a836351aac01e5e23e74 100644 (file)
@@ -85,33 +85,41 @@ public class HSSFHyperlink implements Hyperlink {
     protected HSSFHyperlink( HyperlinkRecord record )
     {
         this.record = record;
-        
+        link_type = getType(record);
+    }
+    
+    private int getType(HyperlinkRecord record) {
+        int link_type;
         // Figure out the type
-        if(record.isFileLink()) {
-           link_type = LINK_FILE;
+        if (record.isFileLink()) {
+            link_type = LINK_FILE;
         } else if(record.isDocumentLink()) {
-           link_type = LINK_DOCUMENT;
+            link_type = LINK_DOCUMENT;
         } else {
-           if(record.getAddress() != null &&
-                 record.getAddress().startsWith("mailto:")) {
-              link_type = LINK_EMAIL;
-           } else {
-              link_type = LINK_URL;
-           }
+            if(record.getAddress() != null &&
+                    record.getAddress().startsWith("mailto:")) {
+                link_type = LINK_EMAIL;
+            } else {
+                link_type = LINK_URL;
+            }
         }
+        return link_type;
     }
     
-    @Override
-    public HSSFHyperlink clone() {
-        return new HSSFHyperlink(record.clone());
-        /*final HSSFHyperlink link = new HSSFHyperlink(link_type);
-        link.setLabel(getLabel());
-        link.setAddress(getAddress());
-        link.setFirstColumn(getFirstColumn());
-        link.setFirstRow(getFirstRow());
-        link.setLastColumn(getLastColumn());
-        link.setLastRow(getLastRow());
-        return link;*/
+    protected HSSFHyperlink(Hyperlink other) {
+        if (other instanceof HSSFHyperlink) {
+            HSSFHyperlink hlink = (HSSFHyperlink) other;
+            record = hlink.record.clone();
+            link_type = getType(record);
+        }
+        else {
+            link_type = other.getType();
+            record = new HyperlinkRecord();
+            setFirstRow(other.getFirstRow());
+            setFirstColumn(other.getFirstColumn());
+            setLastRow(other.getLastRow());
+            setLastColumn(other.getLastColumn());
+        }
     }
 
     /**
index 35ceb7863e2b55225fe394ca9d9be450fed1648c..c067cc002c221cea9e716297b6bf1489297bc359 100644 (file)
@@ -19,7 +19,7 @@ package org.apache.poi.ss.usermodel;
 /**
  * Represents an Excel hyperlink.
  */
-public interface Hyperlink extends org.apache.poi.common.usermodel.Hyperlink, Cloneable {
+public interface Hyperlink extends org.apache.poi.common.usermodel.Hyperlink {
     /**
      * Return the row of the first cell that contains the hyperlink
      *
@@ -75,11 +75,4 @@ public interface Hyperlink extends org.apache.poi.common.usermodel.Hyperlink, Cl
      * @param col the 0-based column of the last cell that contains the hyperlink
      */
     public void setLastColumn(int col);
-    
-    /**
-     * Create a clone of this hyperlink
-     *
-     * @return clone of this Hyperlink
-     */
-    public Hyperlink clone();
 }
index 68ec6bc6c42d3598ca590cde162b308f7091d6ec..f718e37f093c2a720fb4d060426dc5383d962d8e 100644 (file)
@@ -188,7 +188,7 @@ public final class XSSFCell implements Cell {
             // if srcCell doesn't have a hyperlink and destCell has a hyperlink, don't clear destCell's hyperlink
             final Hyperlink srcHyperlink = srcCell.getHyperlink();
             if (srcHyperlink != null) {
-                setHyperlink(srcHyperlink.clone());
+                setHyperlink(new XSSFHyperlink(srcHyperlink));
             }
         }
         else if (policy.isCopyHyperlink()) {
@@ -199,7 +199,7 @@ public final class XSSFCell implements Cell {
                 setHyperlink(null);
             }
             else {
-                setHyperlink(srcHyperlink.clone());
+                setHyperlink(new XSSFHyperlink(srcHyperlink));
             }
         }
     }
index fdf7759ec689d0723c217c91ba0fee5a92aa13f6..ccc0f943951cbc76416ed9184f923c90e4fa41f1 100644 (file)
@@ -93,13 +93,29 @@ public class XSSFHyperlink implements Hyperlink {
         }
     }
     
-    @Override
-    public Hyperlink clone() {
-        final XSSFHyperlink clone = new XSSFHyperlink((CTHyperlink) _ctHyperlink.copy(), _externalRel);
-        clone.setLocation(_location);
-        return clone;
+    /**
+     * Create a new XSSFHyperlink. This method is for Internal use only.
+     * XSSFHyperlinks can be created by XSSFCreationHelper.
+     *
+     * @param type - the type of hyperlink to create, see {@link Hyperlink}
+     */
+    @Internal //FIXME: change to protected if/when SXSSFHyperlink class is created
+    public XSSFHyperlink(Hyperlink other) {
+        if (other instanceof XSSFHyperlink) {
+            XSSFHyperlink xlink = (XSSFHyperlink) other;
+            _type = xlink.getType();
+            _location = xlink._location;
+            _externalRel = xlink._externalRel;
+            _ctHyperlink = (CTHyperlink) xlink._ctHyperlink.copy();
+        }
+        else {
+            _type = other.getType();
+            _location = other.getAddress();
+            _externalRel = null;
+            _ctHyperlink = CTHyperlink.Factory.newInstance();
+            setCellReference(new CellReference(other.getFirstRow(), other.getFirstColumn()));
+        }
     }
-
     /**
      * @return the underlying CTHyperlink object
      */
index 51aa9959ba4efc71daf171478d4273b4b3d4a31d..e1e237b689b697ccd12b929753834072fb09e922 100644 (file)
@@ -22,7 +22,9 @@ package org.apache.poi.xssf.streaming;
 import org.junit.After;
 
 import org.apache.poi.ss.usermodel.BaseTestHyperlink;
+import org.apache.poi.ss.usermodel.Hyperlink;
 import org.apache.poi.xssf.SXSSFITestDataProvider;
+import org.apache.poi.xssf.usermodel.XSSFHyperlink;
 
 /**
  * Test setting hyperlinks in SXSSF
@@ -40,5 +42,11 @@ public class TestSXSSFHyperlink extends BaseTestHyperlink {
     public void tearDown(){
         SXSSFITestDataProvider.instance.cleanup();
     }
+    
+    @Override
+    public XSSFHyperlink copyHyperlink(Hyperlink link) {
+        // FIXME: replace with SXSSFHyperlink if it ever gets created
+        return new XSSFHyperlink(link);
+    }
 
 }
\ No newline at end of file
index 1a9877ff25ab552bfe011569374e961d95e19d82..d5e3df294ef8f616549efdfe00c55f7df96c8fc9 100644 (file)
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
+import org.apache.poi.hssf.usermodel.HSSFHyperlink;
 import org.apache.poi.openxml4j.opc.PackageRelationship;
 import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
 import org.apache.poi.ss.usermodel.BaseTestHyperlink;
@@ -30,6 +31,7 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.ss.usermodel.Hyperlink;
 import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.junit.Test;
@@ -268,4 +270,26 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
         link = wb.getSheetAt(0).getRow(0).getCell(14).getHyperlink();
         assertEquals("mailto:nobody@nowhere.uk%C2%A0", link.getAddress());
     }
+    
+    @Override
+    public XSSFHyperlink copyHyperlink(Hyperlink link) {
+        return new XSSFHyperlink(link);
+    }
+    
+    @Test
+    public void testCopyHSSFHyperlink() {
+        HSSFHyperlink hlink = new HSSFHyperlink(Hyperlink.LINK_URL);
+        hlink.setAddress("http://poi.apache.org/");
+        hlink.setFirstColumn(3);
+        hlink.setFirstRow(2);
+        hlink.setLastColumn(5);
+        hlink.setLastRow(6);
+        hlink.setLabel("label");
+        XSSFHyperlink xlink = new XSSFHyperlink(hlink);
+        
+        assertEquals("http://poi.apache.org/", xlink.getAddress());
+        assertEquals(new CellReference(2, 3), new CellReference(xlink.getCellRef()));
+        // Are HSSFHyperlink.label and XSSFHyperlink.tooltip the same? If so, perhaps one of these needs renamed for a consistent Hyperlink interface
+        // assertEquals("label", xlink.getTooltip());
+    }
 }
index bc1fafe8a759853e8a89dbe092e352a0587d64f7..c7f01840f91258a7ccc389b4b9b8863a9042696a 100644 (file)
@@ -27,7 +27,13 @@ import java.io.IOException;
 import org.apache.poi.hssf.HSSFITestDataProvider;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.ss.usermodel.BaseTestHyperlink;
-
+import org.apache.poi.ss.usermodel.Hyperlink;
+/*
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
+import org.apache.poi.xssf.usermodel.XSSFHyperlink;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+*/
 
 /**
  * Tests HSSFHyperlink.
@@ -259,4 +265,28 @@ public final class TestHSSFHyperlink extends BaseTestHyperlink {
         assertEquals(5, link2_shifted.getFirstRow());\r
         assertEquals(5, link2_shifted.getLastRow());
     }
+    
+    @Override
+    public HSSFHyperlink copyHyperlink(Hyperlink link) {
+        return new HSSFHyperlink(link);
+    }
+    
+    /*
+    @Test
+    public void testCopyXSSFHyperlink() throws IOException {
+        XSSFWorkbook wb = new XSSFWorkbook();
+        XSSFCreationHelper helper = wb.getCreationHelper();
+        XSSFHyperlink xlink = helper.createHyperlink(Hyperlink.LINK_URL);
+        xlink.setAddress("http://poi.apache.org/");
+        xlink.setCellReference("C3");
+        xlink.setTooltip("tooltip");
+        HSSFHyperlink hlink = new HSSFHyperlink(xlink);
+        
+        assertEquals("http://poi.apache.org/", hlink.getAddress());
+        assertEquals("C3", new CellReference(hlink.getFirstRow(), hlink.getFirstColumn()).formatAsString());
+        // Are HSSFHyperlink.label and XSSFHyperlink.tooltip the same? If so, perhaps one of these needs renamed for a consistent Hyperlink interface
+        // assertEquals("tooltip", hlink.getLabel());
+        
+        wb.close();
+    }*/
 }
index 4be6de3a71b21b80a36f0205f3f1e189eded6605..d96cc3bbae7bd4653896150944f30cdfc73a7e28 100644 (file)
@@ -97,9 +97,9 @@ public abstract class BaseTestHyperlink {
         assertEquals("'Target Sheet'!A1", link.getAddress());
     }
     
+    // copy a hyperlink via the copy constructor
     @Test
-    public void testClone() {
-        System.out.println("testClone");
+    public void testCopyHyperlink() {
         final Workbook wb = _testDataProvider.createWorkbook();
         final CreationHelper createHelper = wb.getCreationHelper();
 
@@ -116,7 +116,7 @@ public abstract class BaseTestHyperlink {
         link1.setAddress("http://poi.apache.org/");
         cell1.setHyperlink(link1);
         
-        link2 = link1.clone();
+        link2 = copyHyperlink(link1);
         
         // Change address (type is not changeable)
         link2.setAddress("http://apache.org/");
@@ -137,4 +137,6 @@ public abstract class BaseTestHyperlink {
         assertEquals(link1, actualHyperlinks.get(0));
         assertEquals(link2, actualHyperlinks.get(1));
     }
+    
+    public abstract Hyperlink copyHyperlink(Hyperlink link);
 }