diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b7720e9e8..1cc222777 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Initialise the link type of HSSFHyperLink, so that getType() on it works 48425 - improved performance of DateUtil.isCellDateFormatted() 47215 - fixed InterfaceEndRecord to tolerate unexpected record contents 48415 - improved javadoc on HSSPicture.resize() diff --git a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java index 94be338f0..ed7fee1fd 100644 --- a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java +++ b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java @@ -674,6 +674,27 @@ public final class HyperlinkRecord extends StandardRecord { return buffer.toString(); } + /** + * Based on the link options, is this a url? + */ + public boolean isUrlLink() { + return (_linkOpts & HLINK_URL) > 0 + && (_linkOpts & HLINK_ABS) > 0; + } + /** + * Based on the link options, is this a file? + */ + public boolean isFileLink() { + return (_linkOpts & HLINK_URL) > 0 + && (_linkOpts & HLINK_ABS) == 0; + } + /** + * Based on the link options, is this a document? + */ + public boolean isDocumentLink() { + return (_linkOpts & HLINK_PLACE) > 0; + } + /** * Initialize a new url link */ diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java b/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java index 669f52f06..711c6d145 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java @@ -87,6 +87,20 @@ public class HSSFHyperlink implements Hyperlink { protected HSSFHyperlink( HyperlinkRecord record ) { this.record = 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; + } + } } /** diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java index 8a5d60136..0188e8b27 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java @@ -49,6 +49,7 @@ public final class TestHSSFHyperlink extends BaseTestHyperlink { assertEquals("POI", link.getLabel()); assertEquals("POI", cell.getRichStringCellValue().getString()); assertEquals("http://poi.apache.org/", link.getAddress()); + assertEquals(HSSFHyperlink.LINK_URL, link.getType()); cell = sheet.getRow(8).getCell(0); link = cell.getHyperlink(); @@ -56,6 +57,7 @@ public final class TestHSSFHyperlink extends BaseTestHyperlink { assertEquals("HSSF", link.getLabel()); assertEquals("HSSF", cell.getRichStringCellValue().getString()); assertEquals("http://poi.apache.org/hssf/", link.getAddress()); + assertEquals(HSSFHyperlink.LINK_URL, link.getType()); sheet = wb.getSheet("Emails"); cell = sheet.getRow(4).getCell(0); @@ -64,6 +66,7 @@ public final class TestHSSFHyperlink extends BaseTestHyperlink { assertEquals("dev", link.getLabel()); assertEquals("dev", cell.getRichStringCellValue().getString()); assertEquals("mailto:dev@poi.apache.org", link.getAddress()); + assertEquals(HSSFHyperlink.LINK_EMAIL, link.getType()); sheet = wb.getSheet("Internal"); cell = sheet.getRow(4).getCell(0); @@ -73,6 +76,7 @@ public final class TestHSSFHyperlink extends BaseTestHyperlink { assertEquals("Link To First Sheet", cell.getRichStringCellValue().getString()); assertEquals("WebLinks!A1", link.getTextMark()); assertEquals("WebLinks!A1", link.getAddress()); + assertEquals(HSSFHyperlink.LINK_DOCUMENT, link.getType()); } public void testModify() {