git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1711951 13f79535-47bb-0310-9956-ffa450edef68tags/REL_3_14_BETA1
protected HSSFHyperlink( HyperlinkRecord record ) | protected HSSFHyperlink( HyperlinkRecord record ) | ||||
{ | { | ||||
this.record = record; | this.record = record; | ||||
link_type = getType(record); | |||||
} | |||||
private int getType(HyperlinkRecord record) { | |||||
int link_type; | |||||
// Figure out the type | // Figure out the type | ||||
if(record.isFileLink()) { | |||||
link_type = LINK_FILE; | |||||
if (record.isFileLink()) { | |||||
link_type = LINK_FILE; | |||||
} else if(record.isDocumentLink()) { | } else if(record.isDocumentLink()) { | ||||
link_type = LINK_DOCUMENT; | |||||
link_type = LINK_DOCUMENT; | |||||
} else { | } 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()); | |||||
} | |||||
} | } | ||||
/** | /** |
/** | /** | ||||
* 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 | ||||
* | * | ||||
* @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(); | |||||
} | } |
// 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()) { | ||||
setHyperlink(null); | setHyperlink(null); | ||||
} | } | ||||
else { | else { | ||||
setHyperlink(srcHyperlink.clone()); | |||||
setHyperlink(new XSSFHyperlink(srcHyperlink)); | |||||
} | } | ||||
} | } | ||||
} | } |
} | } | ||||
} | } | ||||
@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 | * @return the underlying CTHyperlink object | ||||
*/ | */ |
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 | ||||
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); | |||||
} | |||||
} | } |
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; | ||||
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; | ||||
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()); | |||||
} | |||||
} | } |
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. | ||||
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(); | |||||
}*/ | |||||
} | } |
assertEquals("'Target Sheet'!A1", link.getAddress()); | assertEquals("'Target Sheet'!A1", link.getAddress()); | ||||
} | } | ||||
// copy a hyperlink via the copy constructor | |||||
@Test | @Test | ||||
public void testClone() { | |||||
System.out.println("testClone"); | |||||
public void testCopyHyperlink() { | |||||
final Workbook wb = _testDataProvider.createWorkbook(); | final Workbook wb = _testDataProvider.createWorkbook(); | ||||
final CreationHelper createHelper = wb.getCreationHelper(); | final CreationHelper createHelper = wb.getCreationHelper(); | ||||
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/"); | ||||
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); | |||||
} | } |