bug 58572: replace Cloneable with copy constructors for spreadsheet Hyperlink class
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711951 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
54113431b5
commit
9b8250832e
@ -85,33 +85,41 @@ public class HSSFHyperlink implements Hyperlink {
|
|||||||
protected HSSFHyperlink( HyperlinkRecord record )
|
protected HSSFHyperlink( HyperlinkRecord record )
|
||||||
{
|
{
|
||||||
this.record = record;
|
this.record = record;
|
||||||
|
link_type = getType(record);
|
||||||
// Figure out the type
|
|
||||||
if(record.isFileLink()) {
|
|
||||||
link_type = LINK_FILE;
|
|
||||||
} else if(record.isDocumentLink()) {
|
|
||||||
link_type = LINK_DOCUMENT;
|
|
||||||
} else {
|
|
||||||
if(record.getAddress() != null &&
|
|
||||||
record.getAddress().startsWith("mailto:")) {
|
|
||||||
link_type = LINK_EMAIL;
|
|
||||||
} else {
|
|
||||||
link_type = LINK_URL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private int getType(HyperlinkRecord record) {
|
||||||
public HSSFHyperlink clone() {
|
int link_type;
|
||||||
return new HSSFHyperlink(record.clone());
|
// Figure out the type
|
||||||
/*final HSSFHyperlink link = new HSSFHyperlink(link_type);
|
if (record.isFileLink()) {
|
||||||
link.setLabel(getLabel());
|
link_type = LINK_FILE;
|
||||||
link.setAddress(getAddress());
|
} else if(record.isDocumentLink()) {
|
||||||
link.setFirstColumn(getFirstColumn());
|
link_type = LINK_DOCUMENT;
|
||||||
link.setFirstRow(getFirstRow());
|
} else {
|
||||||
link.setLastColumn(getLastColumn());
|
if(record.getAddress() != null &&
|
||||||
link.setLastRow(getLastRow());
|
record.getAddress().startsWith("mailto:")) {
|
||||||
return link;*/
|
link_type = LINK_EMAIL;
|
||||||
|
} else {
|
||||||
|
link_type = LINK_URL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return link_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,7 +19,7 @@ package org.apache.poi.ss.usermodel;
|
|||||||
/**
|
/**
|
||||||
* Represents an Excel hyperlink.
|
* 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
|
* 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
|
* @param col the 0-based column of the last cell that contains the hyperlink
|
||||||
*/
|
*/
|
||||||
public void setLastColumn(int col);
|
public void setLastColumn(int col);
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a clone of this hyperlink
|
|
||||||
*
|
|
||||||
* @return clone of this Hyperlink
|
|
||||||
*/
|
|
||||||
public Hyperlink clone();
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
// if srcCell doesn't have a hyperlink and destCell has a hyperlink, don't clear destCell's hyperlink
|
||||||
final Hyperlink srcHyperlink = srcCell.getHyperlink();
|
final Hyperlink srcHyperlink = srcCell.getHyperlink();
|
||||||
if (srcHyperlink != null) {
|
if (srcHyperlink != null) {
|
||||||
setHyperlink(srcHyperlink.clone());
|
setHyperlink(new XSSFHyperlink(srcHyperlink));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (policy.isCopyHyperlink()) {
|
else if (policy.isCopyHyperlink()) {
|
||||||
@ -199,7 +199,7 @@ public final class XSSFCell implements Cell {
|
|||||||
setHyperlink(null);
|
setHyperlink(null);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
setHyperlink(srcHyperlink.clone());
|
setHyperlink(new XSSFHyperlink(srcHyperlink));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,13 +93,29 @@ public class XSSFHyperlink implements Hyperlink {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public Hyperlink clone() {
|
* Create a new XSSFHyperlink. This method is for Internal use only.
|
||||||
final XSSFHyperlink clone = new XSSFHyperlink((CTHyperlink) _ctHyperlink.copy(), _externalRel);
|
* XSSFHyperlinks can be created by XSSFCreationHelper.
|
||||||
clone.setLocation(_location);
|
*
|
||||||
return clone;
|
* @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
|
* @return the underlying CTHyperlink object
|
||||||
*/
|
*/
|
||||||
|
@ -22,7 +22,9 @@ package org.apache.poi.xssf.streaming;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.BaseTestHyperlink;
|
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.SXSSFITestDataProvider;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test setting hyperlinks in SXSSF
|
* Test setting hyperlinks in SXSSF
|
||||||
@ -40,5 +42,11 @@ public class TestSXSSFHyperlink extends BaseTestHyperlink {
|
|||||||
public void tearDown(){
|
public void tearDown(){
|
||||||
SXSSFITestDataProvider.instance.cleanup();
|
SXSSFITestDataProvider.instance.cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public XSSFHyperlink copyHyperlink(Hyperlink link) {
|
||||||
|
// FIXME: replace with SXSSFHyperlink if it ever gets created
|
||||||
|
return new XSSFHyperlink(link);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -23,6 +23,7 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.fail;
|
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.PackageRelationship;
|
||||||
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
|
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
|
||||||
import org.apache.poi.ss.usermodel.BaseTestHyperlink;
|
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.CreationHelper;
|
||||||
import org.apache.poi.ss.usermodel.Hyperlink;
|
import org.apache.poi.ss.usermodel.Hyperlink;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
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.XSSFITestDataProvider;
|
||||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -268,4 +270,26 @@ public final class TestXSSFHyperlink extends BaseTestHyperlink {
|
|||||||
link = wb.getSheetAt(0).getRow(0).getCell(14).getHyperlink();
|
link = wb.getSheetAt(0).getRow(0).getCell(14).getHyperlink();
|
||||||
assertEquals("mailto:nobody@nowhere.uk%C2%A0", link.getAddress());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,13 @@ import java.io.IOException;
|
|||||||
import org.apache.poi.hssf.HSSFITestDataProvider;
|
import org.apache.poi.hssf.HSSFITestDataProvider;
|
||||||
import org.apache.poi.hssf.HSSFTestDataSamples;
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
||||||
import org.apache.poi.ss.usermodel.BaseTestHyperlink;
|
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.
|
* Tests HSSFHyperlink.
|
||||||
@ -259,4 +265,28 @@ public final class TestHSSFHyperlink extends BaseTestHyperlink {
|
|||||||
assertEquals(5, link2_shifted.getFirstRow());
|
assertEquals(5, link2_shifted.getFirstRow());
|
||||||
assertEquals(5, link2_shifted.getLastRow());
|
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();
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
|
@ -97,9 +97,9 @@ public abstract class BaseTestHyperlink {
|
|||||||
assertEquals("'Target Sheet'!A1", link.getAddress());
|
assertEquals("'Target Sheet'!A1", link.getAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copy a hyperlink via the copy constructor
|
||||||
@Test
|
@Test
|
||||||
public void testClone() {
|
public void testCopyHyperlink() {
|
||||||
System.out.println("testClone");
|
|
||||||
final Workbook wb = _testDataProvider.createWorkbook();
|
final Workbook wb = _testDataProvider.createWorkbook();
|
||||||
final CreationHelper createHelper = wb.getCreationHelper();
|
final CreationHelper createHelper = wb.getCreationHelper();
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ public abstract class BaseTestHyperlink {
|
|||||||
link1.setAddress("http://poi.apache.org/");
|
link1.setAddress("http://poi.apache.org/");
|
||||||
cell1.setHyperlink(link1);
|
cell1.setHyperlink(link1);
|
||||||
|
|
||||||
link2 = link1.clone();
|
link2 = copyHyperlink(link1);
|
||||||
|
|
||||||
// Change address (type is not changeable)
|
// Change address (type is not changeable)
|
||||||
link2.setAddress("http://apache.org/");
|
link2.setAddress("http://apache.org/");
|
||||||
@ -137,4 +137,6 @@ public abstract class BaseTestHyperlink {
|
|||||||
assertEquals(link1, actualHyperlinks.get(0));
|
assertEquals(link1, actualHyperlinks.get(0));
|
||||||
assertEquals(link2, actualHyperlinks.get(1));
|
assertEquals(link2, actualHyperlinks.get(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract Hyperlink copyHyperlink(Hyperlink link);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user