From 25bd56f569139f77c7069283649a0c2ec4d27dab Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 5 Jan 2010 14:08:51 +0000 Subject: [PATCH] Initialise the link type of HSSFHyperLink, so that getType() on it works git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@896049 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/hssf/record/HyperlinkRecord.java | 21 +++++++++++++++++++ .../poi/hssf/usermodel/HSSFHyperlink.java | 14 +++++++++++++ .../poi/hssf/usermodel/TestHSSFHyperlink.java | 4 ++++ 4 files changed, 40 insertions(+) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b7720e9e86..1cc2227775 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 94be338f07..ed7fee1fd2 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 669f52f065..711c6d145e 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 8a5d601369..0188e8b270 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() { -- 2.39.5