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:
Javen O'Neal 2015-11-02 12:57:57 +00:00
parent 54113431b5
commit 9b8250832e
8 changed files with 126 additions and 45 deletions

View File

@ -85,9 +85,13 @@ 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()) {
if (record.isFileLink()) {
link_type = LINK_FILE;
} else if(record.isDocumentLink()) {
link_type = LINK_DOCUMENT;
@ -99,19 +103,23 @@ public class HSSFHyperlink implements Hyperlink {
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());
}
}
/**

View 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();
}

View 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));
}
}
}

View 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
*/

View 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
@ -41,4 +43,10 @@ public class TestSXSSFHyperlink extends BaseTestHyperlink {
SXSSFITestDataProvider.instance.cleanup();
}
@Override
public XSSFHyperlink copyHyperlink(Hyperlink link) {
// FIXME: replace with SXSSFHyperlink if it ever gets created
return new XSSFHyperlink(link);
}
}

View 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());
}
}

View 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());
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();
}*/
}

View 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);
}