From 4ab00acc7cf8a56a22153c84ec203707cd413846 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Thu, 7 Feb 2008 08:56:59 +0000 Subject: [PATCH] support for excel hypelrinks git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@619310 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + .../content/xdocs/hssf/quick-guide.xml | 71 ++ src/documentation/content/xdocs/status.xml | 1 + .../hssf/usermodel/examples/Hyperlinks.java | 91 +++ .../poi/hssf/record/HyperlinkRecord.java | 662 +++++++++++------- .../apache/poi/hssf/usermodel/HSSFCell.java | 21 +- .../poi/hssf/usermodel/HSSFHyperlink.java | 137 +++- .../poi/hssf/record/TestHyperlinkRecord.java | 407 ++++++++--- .../poi/hssf/usermodel/TestHSSFCell.java | 14 +- .../poi/hssf/usermodel/TestHSSFHyperlink.java | 191 +++++ 10 files changed, 1213 insertions(+), 383 deletions(-) create mode 100755 src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java create mode 100755 src/testcases/org/apache/poi/hssf/usermodel/TestHSSFHyperlink.java diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index d2430ca19e..2b017fee7b 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,6 +36,7 @@ + 37923 - Support for Excel hyperlinks Implement hashCode() and equals(obj) on HSSFFont and HSSFCellStyle 44345 - Implement CountA, CountIf, Index, Rows and Columns functions 44336 - Properly escape sheet names as required when figuring out the text of formulas diff --git a/src/documentation/content/xdocs/hssf/quick-guide.xml b/src/documentation/content/xdocs/hssf/quick-guide.xml index 69850afe88..66da604892 100644 --- a/src/documentation/content/xdocs/hssf/quick-guide.xml +++ b/src/documentation/content/xdocs/hssf/quick-guide.xml @@ -69,6 +69,7 @@
  • Named Ranges and Named Cells
  • How to set cell comments
  • How to adjust column width to fit the contents
  • +
  • Hyperlinks
  • Features @@ -1322,6 +1323,76 @@ Examples: (either via -Djava.awt.headless=true startup parameter or via System.setProperty("java.awt.headless", "true")).
    + +
    How to read hyperlinks + + HSSFSheet sheet = workbook.getSheetAt(0); + + HSSFCell cell = sheet.getRow(0).getCell((short)0); + HSSFHyperlink link = cell.getHyperlink(); + if(link != null){ + System.out.println(link.getAddress()); + } + +
    +
    How to create hyperlinks + + HSSFWorkbook wb = new HSSFWorkbook(); + + //cell style for hyperlinks + //by default hypelrinks are blue and underlined + HSSFCellStyle hlink_style = wb.createCellStyle(); + HSSFFont hlink_font = wb.createFont(); + hlink_font.setUnderline(HSSFFont.U_SINGLE); + hlink_font.setColor(HSSFColor.BLUE.index); + hlink_style.setFont(hlink_font); + + HSSFCell cell; + HSSFSheet sheet = wb.createSheet("Hyperlinks"); + + //URL + cell = sheet.createRow(0).createCell((short)0); + cell.setCellValue("URL Link"); + HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL); + link.setAddress("http://poi.apache.org/"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //link to a file in the current directory + cell = sheet.createRow(1).createCell((short)0); + cell.setCellValue("File Link"); + link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE); + link.setAddress("link1.xls"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //e-mail link + cell = sheet.createRow(2).createCell((short)0); + cell.setCellValue("Email Link"); + link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL); + //note, if subject contains white spaces, make sure they are url-encoded + link.setAddress("mailto:poi@apache.org?subject=Hyperlinks"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //link to a place in this workbook + + //create a target sheet and cell + HSSFSheet sheet2 = wb.createSheet("Target Sheet"); + sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell"); + + cell = sheet.createRow(3).createCell((short)0); + cell.setCellValue("Worksheet Link"); + link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT); + link.setAddress("'Target Sheet'!A1"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + FileOutputStream out = new FileOutputStream("hssf-links.xls"); + wb.write(out); + out.close(); + +
    diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 8d43546e19..feab74a973 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 37923 - Support for Excel hyperlinks Implement hashCode() and equals(obj) on HSSFFont and HSSFCellStyle 44345 - Implement CountA, CountIf, Index, Rows and Columns functions 44336 - Properly escape sheet names as required when figuring out the text of formulas diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java new file mode 100755 index 0000000000..24b3f186fc --- /dev/null +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/Hyperlinks.java @@ -0,0 +1,91 @@ + +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.usermodel.examples; + +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.hssf.util.HSSFColor; + +import java.io.IOException; +import java.io.FileOutputStream; + +/** + * Demonstrates how to create hyperlinks. + * + * @author Yegor Kozlov (yegor at apach.org) + */ +public class Hyperlinks { + + public static void main(String[] args) throws IOException { + HSSFWorkbook wb = new HSSFWorkbook(); + + //cell style for hyperlinks + //by default hypelrinks are blue and underlined + HSSFCellStyle hlink_style = wb.createCellStyle(); + HSSFFont hlink_font = wb.createFont(); + hlink_font.setUnderline(HSSFFont.U_SINGLE); + hlink_font.setColor(HSSFColor.BLUE.index); + hlink_style.setFont(hlink_font); + + HSSFCell cell; + HSSFSheet sheet = wb.createSheet("Hyperlinks"); + + //URL + cell = sheet.createRow(0).createCell((short)0); + cell.setCellValue("URL Link"); + HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL); + link.setAddress("http://poi.apache.org/"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //link to a file in the current directory + cell = sheet.createRow(1).createCell((short)0); + cell.setCellValue("File Link"); + link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE); + link.setAddress("link1.xls"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //e-mail link + cell = sheet.createRow(2).createCell((short)0); + cell.setCellValue("Email Link"); + link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL); + //note, if subject contains white spaces, make sure they are url-encoded + link.setAddress("mailto:poi@apache.org?subject=Hyperlinks"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + //link to a place in this workbook + + //create a target sheet and cell + HSSFSheet sheet2 = wb.createSheet("Target Sheet"); + sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell"); + + cell = sheet.createRow(3).createCell((short)0); + cell.setCellValue("Worksheet Link"); + link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT); + link.setAddress("'Target Sheet'!A1"); + cell.setHyperlink(link); + cell.setCellStyle(hlink_style); + + FileOutputStream out = new FileOutputStream("hssf-links.xls"); + wb.write(out); + out.close(); + + } +} diff --git a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java index 0dcd45a724..798d4e1ff5 100644 --- a/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java +++ b/src/java/org/apache/poi/hssf/record/HyperlinkRecord.java @@ -1,29 +1,27 @@ /* ==================================================================== - Copyright 2002-2004 Apache Software Foundation - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - ==================================================================== */ + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ package org.apache.poi.hssf.record; import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; +import java.util.Arrays; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.StringUtil; +import org.apache.poi.util.HexDump; /** * The HyperlinkRecord wraps an HLINK-record @@ -31,146 +29,283 @@ import org.apache.poi.util.StringUtil; * Supports only external links for now (eg http://) * * @author Mark Hissink Muller (in.remaining()/2) ? (in.remaining()/2) : field_7_url_len; - field_12_url = in.readUnicodeLEString(strlen); + } - - /* (non-Javadoc) - * @see org.apache.poi.hssf.record.Record#getSid() - */ + public short getSid() { return HyperlinkRecord.sid; @@ -244,55 +378,75 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface public int serialize(int offset, byte[] data) { - LittleEndian.putShort(data, 0 + offset, sid); - LittleEndian.putShort(data, 2 + offset, - ( short )(getRecordSize()-4)); - LittleEndian.putShort(data, 4 + offset, field_1_unknown); - LittleEndian.putUShort(data, 6 + offset, field_2_row); - LittleEndian.putShort(data, 8 + offset, field_3_column); - LittleEndian.putShort(data, 10 + offset, field_4_xf_index); - - offset += 12; - for(int i=0; i 0){ + System.arraycopy(tail, 0, data, pos, tail.length); pos += tail.length; + } + } else if (Arrays.equals(FILE_MONIKER, moniker)){ + LittleEndian.putShort(data, pos, file_opts); pos += 2; + LittleEndian.putInt(data, pos, address.length()); pos += 4; + byte[] bytes = address.getBytes(); + System.arraycopy(bytes, 0, data, pos, bytes.length); pos += bytes.length; + if(tail.length > 0){ + System.arraycopy(tail, 0, data, pos, tail.length); pos += tail.length; + } + } + } else if((link_opts & HLINK_PLACE) != 0){ + LittleEndian.putInt(data, pos, address.length()); pos += 4; + StringUtil.putUnicodeLE(address, data, pos); pos += address.length()*2; } - - LittleEndian.putInt(data, offset, field_11_url_opts); - offset += 4; - StringUtil.putUnicodeLE(field_12_url, data, offset); - return getRecordSize(); } public int getRecordSize() { - // We have: - // 4 shorts - // junk - // 3 ints - // label - // junk - // int - // url - return 4 + 4*2 + field_5_unknown.length + - 3*4 + field_9_label.length()*2 + - field_10_unknown.length + 4 + - field_12_url.length()*2; + int size = 4; + size += 2 + 2 + 2 + 2; //rwFirst, rwLast, colFirst, colLast + size += guid.length; + size += 4; //label_opts + size += 4; //link_opts + if ((link_opts & HLINK_LABEL) != 0){ + size += 4; //link length + size += label.length()*2; + } + if ((link_opts & HLINK_URL) != 0){ + size += moniker.length; //moniker length + if(Arrays.equals(URL_MONIKER, moniker)){ + size += 4; //address length + size += address.length()*2; + size += tail.length; + } else if (Arrays.equals(FILE_MONIKER, moniker)){ + size += 2; //file_opts + size += 4; //address length + size += address.length(); + size += tail.length; + } + } else if((link_opts & HLINK_PLACE) != 0){ + size += 4; //address length + size += address.length()*2; + } + return size; } public String toString() @@ -300,71 +454,89 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface StringBuffer buffer = new StringBuffer(); buffer.append("[HYPERLINK RECORD]\n"); - buffer.append(" .row = ").append(Integer.toHexString(getRow())).append("\n"); - buffer.append(" .column = ").append(Integer.toHexString(getColumn())).append("\n"); - buffer.append(" .xfindex = ").append(Integer.toHexString(getXFIndex())).append("\n"); - buffer.append(" .label = ").append(field_9_label).append("\n"); - buffer.append(" .url = ").append(field_12_url).append("\n"); + buffer.append(" .rwFirst = ").append(Integer.toHexString(getFirstRow())).append("\n"); + buffer.append(" .rwLast = ").append(Integer.toHexString(getLastRow())).append("\n"); + buffer.append(" .colFirst = ").append(Integer.toHexString(getFirstColumn())).append("\n"); + buffer.append(" .colLast = ").append(Integer.toHexString(getLastColumn())).append("\n"); + buffer.append(" .guid = ").append(HexDump.toHex(guid)).append("\n"); + buffer.append(" .label_opts = ").append(label_opts).append("\n"); + buffer.append(" .label = ").append(getLabel()).append("\n"); + if((link_opts & HLINK_URL) != 0){ + buffer.append(" .moniker = ").append(HexDump.toHex(moniker)).append("\n"); + } + buffer.append(" .address = ").append(getAddress()).append("\n"); buffer.append("[/HYPERLINK RECORD]\n"); return buffer.toString(); } /** - * @return Returns the label. + * Initialize a new url link */ - public String getLabel() - { - if(field_9_label.length() == 0) { - return ""; - } else { - // Trim off \0 - return field_9_label.substring(0, field_9_label.length() - 1); - } + public void newUrlLink(){ + rwFirst = 0; + rwLast = 0; + colFirst = 0; + colLast = 0; + guid = STD_MONIKER; + label_opts = 0x2; + link_opts = HLINK_URL | HLINK_ABS | HLINK_LABEL; + label = "" + '\u0000'; + moniker = URL_MONIKER; + address = "" + '\u0000'; + tail = URL_TAIL; } /** - * @param label The label to set. + * Initialize a new file link */ - public void setLabel(String label) - { - this.field_9_label = label + '\u0000'; - this.field_8_label_len = field_9_label.length(); + public void newFileLink(){ + rwFirst = 0; + rwLast = 0; + colFirst = 0; + colLast = 0; + guid = STD_MONIKER; + label_opts = 0x2; + link_opts = HLINK_URL | HLINK_LABEL; + file_opts = 0; + label = "" + '\u0000'; + moniker = FILE_MONIKER; + address = "" + '\0'; + tail = FILE_TAIL; } /** - * @return Returns the Url. + * Initialize a new document link */ - public URL getUrl() throws MalformedURLException - { - return new URL(getUrlString()); - } - public String getUrlString() - { - if(field_12_url.length() == 0) { - return ""; - } else { - // Trim off \0 - return field_12_url.substring(0, field_12_url.length() - 1); - } + public void newDocumentLink(){ + rwFirst = 0; + rwLast = 0; + colFirst = 0; + colLast = 0; + guid = STD_MONIKER; + label_opts = 0x2; + link_opts = HLINK_LABEL | HLINK_PLACE; + label = "" + '\u0000'; + moniker = FILE_MONIKER; + address = "" + '\0'; + tail = new byte[]{}; } - /** - * @param url The url to set. - */ - public void setUrl(URL url) - { - setUrl(url.toString()); - } - /** - * @param url The url to set. - */ - public void setUrl(String url) - { - this.field_12_url = url + '\u0000'; - this.field_7_url_len = field_12_url.length(); + public Object clone() { + HyperlinkRecord rec = new HyperlinkRecord(); + rec.rwFirst = rwFirst; + rec.rwLast = rwLast; + rec.colFirst = colFirst; + rec.colLast = colLast; + rec.guid = guid; + rec.label_opts = label_opts; + rec.link_opts = link_opts; + rec.file_opts = file_opts; + rec.label = label; + rec.address = address; + rec.moniker = moniker; + rec.tail = tail; + return rec; } - public int getOptions(){ - return field_11_url_opts; - } + } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 7b6102b69d..646efe3103 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -1066,7 +1066,7 @@ public class HSSFCell Record rec = ( Record ) it.next(); if (rec instanceof HyperlinkRecord){ HyperlinkRecord link = (HyperlinkRecord)rec; - if(link.getColumn() == record.getColumn() && link.getRow() == record.getRow()){ + if(link.getFirstColumn() == record.getColumn() && link.getFirstRow() == record.getRow()){ return new HSSFHyperlink(link); } } @@ -1080,6 +1080,25 @@ public class HSSFCell * @param link hypelrink associated with this cell */ public void setHyperlink(HSSFHyperlink link){ + link.setFirstRow(record.getRow()); + link.setLastRow(record.getRow()); + link.setFirstColumn(record.getColumn()); + link.setLastColumn(record.getColumn()); + + switch(link.getType()){ + case HSSFHyperlink.LINK_EMAIL: + case HSSFHyperlink.LINK_URL: + link.setLabel("url"); + break; + case HSSFHyperlink.LINK_FILE: + link.setLabel("file"); + break; + case HSSFHyperlink.LINK_DOCUMENT: + link.setLabel("place"); + break; + } + int eofLoc = sheet.findFirstRecordLocBySid( EOFRecord.sid ); + sheet.getRecords().add( eofLoc, link.record ); } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java b/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java index e1bd28af6c..7f1c2639c9 100755 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFHyperlink.java @@ -27,9 +27,9 @@ import java.util.List; import java.util.Iterator; /** - * Represents a hyperlink. + * Represents an Excel hyperlink. * - * @author Yegor Kozlov + * @author Yegor Kozlov (yegor at apache dot org) */ public class HSSFHyperlink { @@ -49,67 +49,145 @@ public class HSSFHyperlink { public static final int LINK_EMAIL = 3; /** - * Unknown type + * Link to a file */ - public static final int LINK_UNKNOWN = 4; + public static final int LINK_FILE = 4; /** * Low-level record object that stores the actual hyperlink data */ - private HyperlinkRecord record = null; + protected HyperlinkRecord record = null; + /** + * If we create a new hypelrink remember its type + */ + protected int link_type; + + /** + * Construct a new hyperlink + * + * @param type the type of hyperlink to create + */ + public HSSFHyperlink( int type ) + { + this.link_type = type; + record = new HyperlinkRecord(); + switch(type){ + case LINK_URL: + case LINK_EMAIL: + record.newUrlLink(); + break; + case LINK_FILE: + record.newFileLink(); + break; + case LINK_DOCUMENT: + record.newDocumentLink(); + break; + } + } + + /** + * Initialize the hyperlink by a HyperlinkRecord record + * + * @param record + */ protected HSSFHyperlink( HyperlinkRecord record ) { this.record = record; } /** - * Return the row of the cell that contains the hyperlink + * Return the row of the first cell that contains the hyperlink * * @return the 0-based row of the cell that contains the hyperlink */ - public int getRow(){ - return record.getRow(); + public int getFirstRow(){ + return record.getFirstRow(); + } + + /** + * Set the row of the first cell that contains the hyperlink + * + * @param row the 0-based row of the first cell that contains the hyperlink + */ + public void setFirstRow(int row){ + record.setFirstRow(row); + } + + /** + * Return the row of the last cell that contains the hyperlink + * + * @return the 0-based row of the last cell that contains the hyperlink + */ + public int getLastRow(){ + return record.getLastRow(); + } + + /** + * Set the row of the last cell that contains the hyperlink + * + * @param row the 0-based row of the last cell that contains the hyperlink + */ + public void setLastRow(int row){ + record.setLastRow(row); + } + + /** + * Return the column of the first cell that contains the hyperlink + * + * @return the 0-based column of the first cell that contains the hyperlink + */ + public short getFirstColumn(){ + return record.getFirstColumn(); } /** - * Set the row of the cell that contains the hyperlink + * Set the column of the first cell that contains the hyperlink * - * @param row the 0-based row of the cell that contains the hyperlink + * @param col the 0-based column of the first cell that contains the hyperlink */ - public void setRow(int row){ - record.setRow(row); + public void setFirstColumn(short col){ + record.setFirstColumn(col); } /** - * Return the column of the cell that contains the hyperlink + * Return the column of the last cell that contains the hyperlink * - * @return the 0-based column of the cell that contains the hyperlink + * @return the 0-based column of the last cell that contains the hyperlink */ - public short getColumn(){ - return record.getColumn(); + public short getLastColumn(){ + return record.getLastColumn(); } /** - * Set the column of the cell that contains the hyperlink + * Set the column of the last cell that contains the hyperlink * - * @param col the 0-based column of the cell that contains the hyperlink + * @param col the 0-based column of the last cell that contains the hyperlink */ - public void setColumn(short col){ - record.setColumn(col); + public void setLastColumn(short col){ + record.setLastColumn(col); } /** - * Hypelink address. Depending on the hyperlink type it can be URL, e-mail, etc. + * Hypelink address. Depending on the hyperlink type it can be URL, e-mail, patrh to a file, etc. * * @return the address of this hyperlink */ public String getAddress(){ - return record.getUrlString(); + return record.getAddress(); } /** - * Return text to display for this hyperlink + * Hypelink address. Depending on the hyperlink type it can be URL, e-mail, patrh to a file, etc. + * + * @param address the address of this hyperlink + */ + public void setAddress(String address){ + record.setAddress(address); + } + + /** + * Return text label for this hyperlink * * @return text to display */ @@ -117,12 +195,21 @@ public class HSSFHyperlink { return record.getLabel(); } + /** + * Sets text label for this hyperlink + * + * @param label text label for this hyperlink + */ + public void setLabel(String label){ + record.setLabel(label); + } + /** * Return the type of this hyperlink * * @return the type of this hyperlink */ - public int getType(){ - throw new RuntimeException("Not implemented"); + protected int getType(){ + return link_type; } } diff --git a/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java b/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java index 70548fe95f..3d2ca406ce 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestHyperlinkRecord.java @@ -18,114 +18,311 @@ package org.apache.poi.hssf.record; import java.io.ByteArrayInputStream; import java.net.URL; +import java.util.Arrays; import junit.framework.TestCase; +/** + * Test HyperlinkRecord + * + * @author Nick Burch + * @author Yegor Kozlov + */ public class TestHyperlinkRecord extends TestCase { - protected void setUp() throws Exception { - super.setUp(); - } - - private byte[] data = new byte[] { - -72, 1, 110, 0, - // ??, Row, col, xf - 6, 0, 3, 0, 2, 0, 2, 0, - - // ?? - -48, -55, -22, 121, -7, -70, -50, 17, - -116, -126, 0, -86, 0, 75, -87, 11, - 2, 0, 0, 0, - - // URL length - 23, 0, 0, 0, - - // Label length - 4, 0, 0, 0, - - // Label - 76, 0, 44, 0, 65, 0, 0, 0, - - // ?? - -32, -55, -22, 121, -7, -70, -50, 17, - -116, -126, 0, -86, 0, 75, -87, 11, - 46, 0, 0, 0, - - // URL - 104, 0, 116, 0, 116, 0, 112, 0, 58, 0, 47, 0, 47, 0, 119, - 0, 119, 0, 119, 0, 46, 0, 108, 0, 97, 0, 107, 0, 105, - 0, 110, 0, 103, 0, 115, 0, 46, 0, 99, 0, 111, 0, - 109, 0, - 0, 0 }; - - private byte[] data2 = new byte[] { - -72, 1, -126, 0, - // ??, Row, col, xf - 2, 0, 2, 0, 4, 0, 4, 0, - - // ?? - -48, -55, -22, 121, -7, -70, -50, 17, - -116, -126, 0, -86, 0, 75, -87, 11, - 2, 0, 0, 0, - - // URL and Label lengths - 23, 0, 0, 0, - 15, 0, 0, 0, - - // Label - 83, 0, 116, 0, 97, 0, 99, 0, 105, 0, - 101, 0, 64, 0, 65, 0, 66, 0, 67, 0, - 46, 0, 99, 0, 111, 0, 109, 0, 0, 0, - - // ?? - -32, -55, -22, 121, -7, -70, -50, 17, - -116, -126, 0, -86, 0, 75, -87, 11, - 44, 0, 0, 0, - - // URL - 109, 0, 97, 0, 105, 0, 108, 0, 116, 0, - 111, 0, 58, 0, 83, 0, 116, 0, 97, 0, - 99, 0, 105, 0, 101, 0, 64, 0, 65, 0, - 66, 0, 67, 0, 46, 0, 99, 0, 111, 0, - 109, 0, 0, 0 }; - - public void testRecordParsing() throws Exception { - RecordInputStream inp = new RecordInputStream( - new ByteArrayInputStream(data) - ); - inp.nextRecord(); - - HyperlinkRecord r = new HyperlinkRecord(inp); - - assertEquals(3, r.getRow()); - assertEquals(2, r.getColumn()); - assertEquals(2, r.getXFIndex()); - - assertEquals("L,A", r.getLabel()); - assertEquals("http://www.lakings.com", r.getUrlString()); - assertEquals(new URL("http://www.lakings.com"), r.getUrl()); - - // Check it serialises as expected - assertEquals(data.length, r.getRecordSize()); - byte[] d = r.serialize(); - assertEquals(data.length, d.length); - for(int i=0; i