poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From one...@apache.org
Subject svn commit: r1711951 - in /poi/trunk/src: java/org/apache/poi/hssf/usermodel/ java/org/apache/poi/ss/usermodel/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/testcases/org/apache/poi/xssf/streaming/ ooxml/testcases/org/apache/poi/xssf/usermodel/ test...
Date Mon, 02 Nov 2015 12:57:57 GMT
Author: onealj
Date: Mon Nov  2 12:57:57 2015
New Revision: 1711951

URL: http://svn.apache.org/viewvc?rev=1711951&view=rev
Log:
bug 58572: replace Cloneable with copy constructors for spreadsheet Hyperlink class

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

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java?rev=1711951&r1=1711950&r2=1711951&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java Mon Nov  2 12:57:57
2015
@@ -85,33 +85,41 @@ public class HSSFHyperlink implements Hy
     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());
+        }
     }
 
     /**

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/Hyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/Hyperlink.java?rev=1711951&r1=1711950&r2=1711951&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/Hyperlink.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/Hyperlink.java Mon Nov  2 12:57:57 2015
@@ -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.a
      * @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();
 }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java?rev=1711951&r1=1711950&r2=1711951&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java Mon Nov  2 12:57:57
2015
@@ -188,7 +188,7 @@ public final class XSSFCell implements C
             // 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 C
                 setHyperlink(null);
             }
             else {
-                setHyperlink(srcHyperlink.clone());
+                setHyperlink(new XSSFHyperlink(srcHyperlink));
             }
         }
     }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java?rev=1711951&r1=1711950&r2=1711951&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java Mon Nov  2 12:57:57
2015
@@ -93,13 +93,29 @@ public class XSSFHyperlink implements Hy
         }
     }
     
-    @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
      */

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFHyperlink.java?rev=1711951&r1=1711950&r2=1711951&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFHyperlink.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestSXSSFHyperlink.java Mon
Nov  2 12:57:57 2015
@@ -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
     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

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=1711951&r1=1711950&r2=1711951&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
Nov  2 12:57:57 2015
@@ -23,6 +23,7 @@ import static org.junit.Assert.assertEqu
 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 ext
         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());
+    }
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java?rev=1711951&r1=1711950&r2=1711951&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java Mon Nov 
2 12:57:57 2015
@@ -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 ext
         assertEquals(5, link2_shifted.getFirstRow());
         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();
+    }*/
 }

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java?rev=1711951&r1=1711950&r2=1711951&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java Mon Nov  2
12:57:57 2015
@@ -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);
 }



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


Mime
View raw message