More Hyperlink support. Doesn't end up in HSSFCell just yet, as the records are in the wrong bit of the file, so don't get associated with the sheet. All tests still passing though

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@617555 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-02-01 16:52:58 +00:00
parent 93ccde2d0e
commit b3af87e4a6
6 changed files with 122 additions and 53 deletions

View File

@ -93,6 +93,8 @@ public class Workbook implements Model
protected ArrayList formats = new ArrayList();
protected ArrayList names = new ArrayList();
protected ArrayList hyperlinks = new ArrayList();
protected int numxfs = 0; // hold the number of extended format records
protected int numfonts = 0; // hold the number of font records
@ -133,7 +135,8 @@ public class Workbook implements Model
Workbook retval = new Workbook();
ArrayList records = new ArrayList(recs.size() / 3);
for (int k = 0; k < recs.size(); k++) {
int k;
for (k = 0; k < recs.size(); k++) {
Record rec = ( Record ) recs.get(k);
if (rec.getSid() == EOFRecord.sid) {
@ -248,6 +251,17 @@ public class Workbook implements Model
// retval.records.supbookpos = retval.records.bspos + 1;
// retval.records.namepos = retval.records.supbookpos + 1;
// }
// Look for other interesting values that
// follow the EOFRecord
for ( ; k < recs.size(); k++) {
Record rec = ( Record ) recs.get(k);
switch (rec.getSid()) {
case HyperlinkRecord.sid:
retval.hyperlinks.add(rec);
break;
}
}
retval.records.setRecords(records);
@ -2116,6 +2130,11 @@ public class Workbook implements Model
return null;
}
public List getHyperlinks()
{
return hyperlinks;
}
public List getRecords()
{
return records.getRecords();

View File

@ -14,11 +14,6 @@
limitations under the License.
==================================================================== */
/*
* HyperlinkRecord
*
* Created on February 20th, 2005, 16:00 PM
*/
package org.apache.poi.hssf.record;
import java.io.IOException;
@ -27,20 +22,15 @@ import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.StringUtil;
/**The <code>HyperlinkRecord</code> wraps an HLINK-record from the Excel-97 format
/**
* The <code>HyperlinkRecord</code> wraps an HLINK-record
* from the Excel-97 format.
* Supports only external links for now (eg http://)
*
* @version 20-feb-2005
* @author Mark Hissink Muller <a href="mailto:mark@hissinkmuller.nl >mark&064;hissinkmuller.nl</a>
*
* Version by date changes
* ------- --- -------- -------
* 0.1 MHM 20022005 draft version; need to fix serialize and getRecordSize()
*
* 1.0 MHM 20022005 (Initial version; only aims to support internet URLs (e.g. http://www.example.com) ) - added 31-08-07: AFAIK, this file is NOT functional/working. Please let me know if you turn it into something that does work.
*/
public class HyperlinkRecord extends Record implements CellValueRecordInterface
{
@ -60,10 +50,10 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
public final static short sid = 0x1b8;
private int field_1_row;
private short field_2_column;
private short field_3_xf_index;
private short field_4_unknown;
private short field_1_unknown;
private int field_2_row;
private short field_3_column;
private short field_4_xf_index;
private byte[] field_5_unknown;
private int field_6_label_opts;
private int field_7_url_len;
@ -89,7 +79,7 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
*/
public short getColumn()
{
return field_2_column;
return field_3_column;
}
/* (non-Javadoc)
@ -97,7 +87,7 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
*/
public int getRow()
{
return field_1_row;
return field_2_row;
}
/* (non-Javadoc)
@ -105,7 +95,7 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
*/
public short getXFIndex()
{
return field_3_xf_index;
return field_4_xf_index;
}
/* (non-Javadoc)
@ -161,7 +151,7 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
*/
public void setColumn(short col)
{
this.field_2_column = col;
this.field_3_column = col;
}
@ -170,7 +160,7 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
*/
public void setRow(int row)
{
this.field_1_row = row;
this.field_2_row = row;
}
@ -179,7 +169,7 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
*/
public void setXFIndex(short xf)
{
this.field_3_xf_index = xf;
this.field_4_xf_index = xf;
}
@ -196,10 +186,10 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
// if(1==1)
// throw new IllegalArgumentException("");
field_1_row = in.readUShort();
field_2_column = in.readShort();
field_3_xf_index = in.readShort();
field_4_unknown = in.readShort();
field_1_unknown = in.readShort();
field_2_row = in.readUShort();
field_3_column = in.readShort();
field_4_xf_index = in.readShort();
// Next up is 16 bytes we don't get
field_5_unknown = new byte[16];
@ -256,10 +246,10 @@ public class HyperlinkRecord extends Record implements CellValueRecordInterface
LittleEndian.putShort(data, 0 + offset, sid);
LittleEndian.putShort(data, 2 + offset,
( short )(getRecordSize()-4));
LittleEndian.putUShort(data, 4 + offset, field_1_row);
LittleEndian.putShort(data, 6 + offset, field_2_column);
LittleEndian.putShort(data, 8 + offset, field_3_xf_index);
LittleEndian.putShort(data, 10 + offset, field_4_unknown);
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<field_5_unknown.length; i++) {

View File

@ -28,7 +28,7 @@ public class TestHyperlinkRecord extends TestCase {
private byte[] data = new byte[] {
-72, 1, 110, 0,
// Row, col, xf, ??
// ??, Row, col, xf
6, 0, 3, 0, 2, 0, 2, 0,
// ??
@ -59,7 +59,7 @@ public class TestHyperlinkRecord extends TestCase {
private byte[] data2 = new byte[] {
-72, 1, -126, 0,
// Row, col, xf, ??
// ??, Row, col, xf
2, 0, 2, 0, 4, 0, 4, 0,
// ??
@ -96,8 +96,8 @@ public class TestHyperlinkRecord extends TestCase {
HyperlinkRecord r = new HyperlinkRecord(inp);
assertEquals(6, r.getRow());
assertEquals(3, r.getColumn());
assertEquals(3, r.getRow());
assertEquals(2, r.getColumn());
assertEquals(2, r.getXFIndex());
assertEquals("L,A", r.getLabel());
@ -122,7 +122,7 @@ public class TestHyperlinkRecord extends TestCase {
HyperlinkRecord r = new HyperlinkRecord(inp);
assertEquals(2, r.getRow());
assertEquals(2, r.getColumn());
assertEquals(4, r.getColumn());
assertEquals(4, r.getXFIndex());
assertEquals("Stacie@ABC.com", r.getLabel());

View File

@ -19,25 +19,20 @@
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.EOFRecord;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.util.TempFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Iterator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.record.HyperlinkRecord;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
/**
* Tests various functionity having to do with HSSFCell. For instance support for
@ -310,10 +305,75 @@ extends TestCase {
in.close();
}
public void testWithHyperlinks() throws Exception {
public void BROKENtestWithHyperlink() throws Exception {
String dir = System.getProperty("HSSF.testdata.path");
File f = new File(dir, "WithHyperlink.xls");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(f));
assertEquals(3, wb.getNumberOfSheets());
// Find our hyperlink record, and check they're
// as we'd expect
List records = wb.getWorkbook().getHyperlinks();
assertEquals(1, records.size());
HyperlinkRecord link = (HyperlinkRecord)
records.get(0);
assertNotNull(link);
// Is in A5
assertEquals("Foo", link.getLabel());
assertEquals("http://poi.apache.org/", link.getUrlString());
assertEquals(4, link.getRow());
assertEquals(0, link.getColumn());
// Now check at the HSSFCell level
assertEquals(3, wb.getNumberOfSheets());
HSSFSheet s = wb.getSheetAt(1);
HSSFRow r = s.getRow(4);
assertNotNull(r);
HSSFCell c = r.getCell((short)0);
}
public void BROKENtestWithTwoHyperlinks() throws Exception {
String dir = System.getProperty("HSSF.testdata.path");
File f = new File(dir, "WithTwoHyperLinks.xls");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(f));
assertEquals(3, wb.getNumberOfSheets());
// Find our hyperlink record, and check they're
// as we'd expect
List records = wb.getWorkbook().getHyperlinks();
assertEquals(2, records.size());
HyperlinkRecord linkA = (HyperlinkRecord)
records.get(0);
HyperlinkRecord linkB = (HyperlinkRecord)
records.get(1);
assertNotNull(linkA);
assertNotNull(linkB);
// Is in A5
assertEquals("Foo", linkA.getLabel());
assertEquals("http://poi.apache.org/", linkA.getUrlString());
assertEquals(4, linkA.getRow());
assertEquals(0, linkA.getColumn());
// Is in B9
assertEquals("Bar", linkB.getLabel());
assertEquals("http://poi.apache.org/", linkB.getUrlString());
assertEquals(8, linkB.getRow());
assertEquals(1, linkB.getColumn());
// Now check at the HSSFCell level
assertEquals(3, wb.getNumberOfSheets());
HSSFSheet s = wb.getSheetAt(1);
HSSFRow r = s.getRow(4);
assertNotNull(r);
HSSFCell c = r.getCell((short)0);
}
/*tests the toString() method of HSSFCell*/