ASF Bugzilla – Attachment 33246 Details for
Bug 58572
Make XSSFSheet.getHyperlinkList() and XSSFSheet.getHyperlink(row, col) available in Sheet interface
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
changes to make hyperlinks accessible via HSSFSheet and SXSSFSheet
bug58572.patch (text/plain), 14.26 KB, created by
Javen O'Neal
on 2015-11-02 09:41:57 UTC
(
hide
)
Description:
changes to make hyperlinks accessible via HSSFSheet and SXSSFSheet
Filename:
MIME Type:
Creator:
Javen O'Neal
Created:
2015-11-02 09:41:57 UTC
Size:
14.26 KB
patch
obsolete
>Index: src/java/org/apache/poi/hssf/usermodel/HSSFCell.java >=================================================================== >--- src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (revision 1711921) >+++ src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (working copy) >@@ -1045,17 +1045,9 @@ > /** > * @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 @@ > * > * @param hyperlink hyperlink associated with this cell > */ >+ @Override > public void setHyperlink(Hyperlink hyperlink){ > if (hyperlink == null) { > removeHyperlink(); >Index: src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java >=================================================================== >--- src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java (revision 1711921) >+++ src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java (working copy) >@@ -47,12 +47,12 @@ > /** > * 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 @@ > } > } > } >+ >+ @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 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(); >+ } > } >Index: src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java >=================================================================== >--- src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (revision 1711921) >+++ src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (working copy) >@@ -35,8 +35,10 @@ > 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 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); >Index: src/java/org/apache/poi/ss/usermodel/Hyperlink.java >=================================================================== >--- src/java/org/apache/poi/ss/usermodel/Hyperlink.java (revision 1711921) >+++ src/java/org/apache/poi/ss/usermodel/Hyperlink.java (working copy) >@@ -19,7 +19,7 @@ > /** > * 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 @@ > * @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: src/java/org/apache/poi/ss/usermodel/Sheet.java >=================================================================== >--- src/java/org/apache/poi/ss/usermodel/Sheet.java (revision 1711921) >+++ src/java/org/apache/poi/ss/usermodel/Sheet.java (working copy) >@@ -1067,4 +1067,20 @@ > * 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(); > } >Index: src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java (revision 1711921) >+++ src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java (working copy) >@@ -579,6 +579,7 @@ > /** > * @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 @@ > * > * @param link hyperlink associated with this cell > */ >+ @Override > public void setHyperlink(Hyperlink link) > { > if (link == null) { >Index: src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (revision 1711921) >+++ src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java (working copy) >@@ -44,6 +44,7 @@ > 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 @@ > { > 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. >Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java (revision 1711921) >+++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java (working copy) >@@ -32,9 +32,9 @@ > * 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 @@ > 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 @@ > > } > } >+ >+ @Override >+ public Hyperlink clone() { >+ final XSSFHyperlink clone = new XSSFHyperlink((CTHyperlink) _ctHyperlink.copy(), _externalRel); >+ clone.setLocation(_location); >+ return clone; >+ } > > /** > * @return the underlying CTHyperlink object >Index: src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java >=================================================================== >--- src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (revision 1711921) >+++ src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (working copy) >@@ -703,6 +703,7 @@ > * @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 @@ > * > * @return Hyperlinks for the sheet > */ >+ @Override > public List<XSSFHyperlink> getHyperlinkList() { > return Collections.unmodifiableList(hyperlinks); > } >Index: src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java >=================================================================== >--- src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java (revision 1711921) >+++ src/testcases/org/apache/poi/ss/usermodel/BaseTestHyperlink.java (working copy) >@@ -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 @@ > 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)); >+ } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 58572
: 33246