poi-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=1711923&view=rev
Log:
bug 58572: move getHyperlinkList() and getHyperlink(row, col) from XSSFSheet to Sheet

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
    poi/trunk/src/java/org/apache/poi/ss/usermodel/Hyperlink.java
    poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=1711923&r1=1711922&r2=1711923&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Mon Nov  2 09:40:49 2015
@@ -1045,17 +1045,9 @@ public class HSSFCell implements Cell {
     /**
      * @return hyperlink associated with this cell or <code>null</code> if not
found
      */
+    @Override
     public HSSFHyperlink getHyperlink(){
-        for (Iterator<RecordBase> it = _sheet.getSheet().getRecords().iterator(); it.hasNext();
) {
-            RecordBase rec = it.next();
-            if (rec instanceof HyperlinkRecord){
-                HyperlinkRecord link = (HyperlinkRecord)rec;
-                if(link.getFirstColumn() == _record.getColumn() && link.getFirstRow()
== _record.getRow()){
-                    return new HSSFHyperlink(link);
-                }
-            }
-        }
-        return null;
+        return _sheet.getHyperlink(_record.getRow(), _record.getColumn());
     }
 
     /**
@@ -1064,6 +1056,7 @@ public class HSSFCell implements Cell {
      *
      * @param hyperlink hyperlink associated with this cell
      */
+    @Override
     public void setHyperlink(Hyperlink hyperlink){
         if (hyperlink == null) {
             removeHyperlink();

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=1711923&r1=1711922&r2=1711923&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 09:40:49
2015
@@ -47,12 +47,12 @@ public class HSSFHyperlink implements Hy
     /**
      * Low-level record object that stores the actual hyperlink data
      */
-    protected HyperlinkRecord record = null;
+    final protected HyperlinkRecord record;
 
     /**
      * If we create a new hyperlink remember its type
      */
-    protected int link_type;
+    final protected int link_type;
 
     /**
      * Construct a new hyperlink
@@ -100,6 +100,19 @@ public class HSSFHyperlink implements Hy
            }
         }
     }
+    
+    @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;*/
+    }
 
     /**
      * Return the row of the first cell that contains the hyperlink
@@ -240,4 +253,20 @@ public class HSSFHyperlink implements Hy
     public int getType(){
         return link_type;
     }
+    
+    /**
+     * @return whether the objects have the same HyperlinkRecord
+     */
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) return true;
+        if (!(other instanceof HSSFHyperlink)) return false;
+        HSSFHyperlink otherLink = (HSSFHyperlink) other;
+        return record == otherLink.record;
+    }
+    
+    @Override
+    public int hashCode() {
+        return record.hashCode();
+    }
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java?rev=1711923&r1=1711922&r2=1711923&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java Mon Nov  2 09:40:49 2015
@@ -35,8 +35,10 @@ import org.apache.poi.hssf.record.Dimens
 import org.apache.poi.hssf.record.DrawingRecord;
 import org.apache.poi.hssf.record.EscherAggregate;
 import org.apache.poi.hssf.record.ExtendedFormatRecord;
+import org.apache.poi.hssf.record.HyperlinkRecord;
 import org.apache.poi.hssf.record.NameRecord;
 import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.RecordBase;
 import org.apache.poi.hssf.record.RowRecord;
 import org.apache.poi.hssf.record.SCLRecord;
 import org.apache.poi.hssf.record.WSBoolRecord;
@@ -2046,6 +2048,45 @@ public final class HSSFSheet implements
     public HSSFComment getCellComment(int row, int column) {
         return findCellComment(row, column);
     }
+    
+    /**
+     * Get a Hyperlink in this sheet anchored at row, column
+     *
+     * @param row
+     * @param column
+     * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns
null
+     */
+    @Override
+    public HSSFHyperlink getHyperlink(int row, int column) {
+        for (Iterator<RecordBase> it = _sheet.getRecords().iterator(); it.hasNext();
) {
+            RecordBase rec = it.next();
+            if (rec instanceof HyperlinkRecord){
+                HyperlinkRecord link = (HyperlinkRecord)rec;
+                if (link.getFirstColumn() == column && link.getFirstRow() == row)
{
+                    return new HSSFHyperlink(link);
+                }
+            }
+        }
+        return null;
+    }
+    
+    /**
+     * Get a list of Hyperlinks in this sheet
+     *
+     * @return Hyperlinks for the sheet
+     */
+    @Override
+    public List<HSSFHyperlink> getHyperlinkList() {
+        final List<HSSFHyperlink> hyperlinkList = new ArrayList<HSSFHyperlink>();
+        for (Iterator<RecordBase> it = _sheet.getRecords().iterator(); it.hasNext();
) {
+            RecordBase rec = it.next();
+            if (rec instanceof HyperlinkRecord){
+                HyperlinkRecord link = (HyperlinkRecord)rec;
+                hyperlinkList.add(new HSSFHyperlink(link));
+            }
+        }
+        return hyperlinkList;
+    }
 
     public HSSFSheetConditionalFormatting getSheetConditionalFormatting() {
         return new HSSFSheetConditionalFormatting(this);

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=1711923&r1=1711922&r2=1711923&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 09:40:49 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 {
+public interface Hyperlink extends org.apache.poi.common.usermodel.Hyperlink, Cloneable {
     /**
      * Return the row of the first cell that contains the hyperlink
      *
@@ -75,4 +75,11 @@ 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/java/org/apache/poi/ss/usermodel/Sheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java?rev=1711923&r1=1711922&r2=1711923&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java Mon Nov  2 09:40:49 2015
@@ -1067,4 +1067,20 @@ public interface Sheet extends Iterable<
      *  you take it out of them.
      */
     int getColumnOutlineLevel(int columnIndex);
+    
+    /**
+     * Get a Hyperlink in this sheet anchored at row, column
+     *
+     * @param row
+     * @param column
+     * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns
null
+     */
+    public Hyperlink getHyperlink(int row, int column);
+    
+    /**
+     * Get a list of Hyperlinks in this sheet
+     *
+     * @return Hyperlinks for the sheet
+     */
+    public List<? extends Hyperlink> getHyperlinkList();
 }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java?rev=1711923&r1=1711922&r2=1711923&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java Mon Nov  2 09:40:49
2015
@@ -579,6 +579,7 @@ public class SXSSFCell implements Cell {
     /**
      * @return hyperlink associated with this cell or <code>null</code> if not
found
      */
+    @Override
     public Hyperlink getHyperlink()
     {
         return (Hyperlink)getPropertyValue(Property.HYPERLINK);
@@ -590,6 +591,7 @@ public class SXSSFCell implements Cell {
      *
      * @param link hyperlink associated with this cell
      */
+    @Override
     public void setHyperlink(Hyperlink link)
     {
         if (link == null) {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java?rev=1711923&r1=1711922&r2=1711923&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java Mon Nov  2 09:40:49
2015
@@ -44,6 +44,7 @@ import org.apache.poi.ss.usermodel.Workb
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.SheetUtil;
 import org.apache.poi.xssf.usermodel.XSSFDataValidation;
+import org.apache.poi.xssf.usermodel.XSSFHyperlink;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
@@ -1326,6 +1327,28 @@ public class SXSSFSheet implements Sheet
     {
         return _sh.getCellComment(row, column);
     }
+    
+    /**
+     * Get a Hyperlink in this sheet anchored at row, column
+     *
+     * @param row
+     * @param column
+     * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns
null
+     */
+    @Override
+    public XSSFHyperlink getHyperlink(int row, int column) {
+        return _sh.getHyperlink(row, column);
+    }
+    
+    /**
+     * Get a list of Hyperlinks in this sheet
+     *
+     * @return Hyperlinks for the sheet
+     */
+    @Override
+    public List<XSSFHyperlink> getHyperlinkList() {
+        return _sh.getHyperlinkList();
+    }
 
     /**
      * Creates the top-level drawing patriarch.

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=1711923&r1=1711922&r2=1711923&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 09:40:49
2015
@@ -32,9 +32,9 @@ import org.openxmlformats.schemas.spread
  * are largely stored as relations of the sheet
  */
 public class XSSFHyperlink implements Hyperlink {
-    private int _type;
-    private PackageRelationship _externalRel;
-    private CTHyperlink _ctHyperlink; //contains a reference to the cell where the hyperlink
is anchored, getRef()
+    final private int _type;
+    final private PackageRelationship _externalRel;
+    final private CTHyperlink _ctHyperlink; //contains a reference to the cell where the
hyperlink is anchored, getRef()
     private String _location; //what the hyperlink refers to
 
     /**
@@ -45,10 +45,11 @@ public class XSSFHyperlink implements Hy
     protected XSSFHyperlink(int type) {
         _type = type;
         _ctHyperlink = CTHyperlink.Factory.newInstance();
+        _externalRel = null;
     }
 
     /**
-     * Create a XSSFHyperlink amd initialize it from the supplied CTHyperlink bean and package
relationship
+     * Create a XSSFHyperlink and initialize it from the supplied CTHyperlink bean and package
relationship
      *
      * @param ctHyperlink the xml bean containing xml properties
      * @param hyperlinkRel the relationship in the underlying OPC package which stores the
actual link's address
@@ -91,6 +92,13 @@ public class XSSFHyperlink implements Hy
 
         }
     }
+    
+    @Override
+    public Hyperlink clone() {
+        final XSSFHyperlink clone = new XSSFHyperlink((CTHyperlink) _ctHyperlink.copy(),
_externalRel);
+        clone.setLocation(_location);
+        return clone;
+    }
 
     /**
      * @return the underlying CTHyperlink object

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1711923&r1=1711922&r2=1711923&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Mon Nov  2 09:40:49
2015
@@ -703,6 +703,7 @@ public class XSSFSheet extends POIXMLDoc
      * @param column
      * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns
null
      */
+    @Override
     public XSSFHyperlink getHyperlink(int row, int column) {
         String ref = new CellReference(row, column).formatAsString();
         for(XSSFHyperlink hyperlink : hyperlinks) {
@@ -718,6 +719,7 @@ public class XSSFSheet extends POIXMLDoc
      *
      * @return Hyperlinks for the sheet
      */
+    @Override
     public List<XSSFHyperlink> getHyperlinkList() {
         return Collections.unmodifiableList(hyperlinks);
     }

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=1711923&r1=1711922&r2=1711923&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
09:40:49 2015
@@ -18,6 +18,11 @@
 package org.apache.poi.ss.usermodel;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotSame;
+
+import java.util.List;
+
 import org.junit.Test;
 
 import org.apache.poi.ss.ITestDataProvider;
@@ -91,4 +96,45 @@ public abstract class BaseTestHyperlink
         link = sheet.getRow(3).getCell(0).getHyperlink();
         assertEquals("'Target Sheet'!A1", link.getAddress());
     }
+    
+    @Test
+    public void testClone() {
+        System.out.println("testClone");
+        final Workbook wb = _testDataProvider.createWorkbook();
+        final CreationHelper createHelper = wb.getCreationHelper();
+
+        final Sheet sheet = wb.createSheet("Hyperlinks");
+        final Row row = sheet.createRow(0);
+        final Cell cell1, cell2;
+        final Hyperlink link1, link2;
+
+        //URL
+        cell1 = row.createCell(0);
+        cell2 = row.createCell(1);
+        cell1.setCellValue("URL Link");
+        link1 = createHelper.createHyperlink(Hyperlink.LINK_URL);
+        link1.setAddress("http://poi.apache.org/");
+        cell1.setHyperlink(link1);
+        
+        link2 = link1.clone();
+        
+        // Change address (type is not changeable)
+        link2.setAddress("http://apache.org/");
+        cell2.setHyperlink(link2);
+        
+        // Make sure hyperlinks were deep-copied, and modifying one does not modify the other.

+        assertNotSame(link1, link2);
+        assertNotEquals(link1, link2);
+        assertEquals("http://poi.apache.org/", link1.getAddress());
+        assertEquals("http://apache.org/", link2.getAddress());
+        assertEquals(link1, cell1.getHyperlink());
+        assertEquals(link2, cell2.getHyperlink());
+        
+        // Make sure both hyperlinks were added to the sheet
+        @SuppressWarnings("unchecked")
+        final List<Hyperlink> actualHyperlinks = (List<Hyperlink>) sheet.getHyperlinkList();
+        assertEquals(2, actualHyperlinks.size());
+        assertEquals(link1, actualHyperlinks.get(0));
+        assertEquals(link2, actualHyperlinks.get(1));
+    }
 }



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


Mime
View raw message