Fixes / additions for reserialization of HyperlinkRecord

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@719546 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-11-21 09:09:25 +00:00
parent e3df2fb0b0
commit e796abd3fa
4 changed files with 667 additions and 340 deletions

File diff suppressed because it is too large Load Diff

View File

@ -169,6 +169,18 @@ public class HSSFHyperlink implements Hyperlink {
public String getAddress(){ public String getAddress(){
return record.getAddress(); return record.getAddress();
} }
public String getTextMark(){
return record.getTextMark();
}
public void setTextMark(String textMark) {
record.setTextMark(textMark);
}
public String getShortFilename(){
return record.getShortFilename();
}
public void setShortFilename(String shortFilename) {
record.setShortFilename(shortFilename);
}
/** /**
* Hypelink address. Depending on the hyperlink type it can be URL, e-mail, patrh to a file, etc. * Hypelink address. Depending on the hyperlink type it can be URL, e-mail, patrh to a file, etc.

View File

@ -16,11 +16,17 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import java.io.ByteArrayInputStream;
import java.util.Arrays; import java.util.Arrays;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.record.HyperlinkRecord.GUID;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.LittleEndianByteArrayInputStream;
import org.apache.poi.util.LittleEndianByteArrayOutputStream;
/** /**
* Test HyperlinkRecord * Test HyperlinkRecord
* *
@ -143,7 +149,7 @@ public final class TestHyperlinkRecord extends TestCase {
}; };
//link to a place in worksheet: Sheet1!A1 //link to a place in worksheet: Sheet1!A1
byte[] data4 = {0x03, 0x00, private static final byte[] data4 = {0x03, 0x00,
0x03, 0x00, 0x03, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
@ -166,8 +172,79 @@ public final class TestHyperlinkRecord extends TestCase {
0x53, 0x00, 0x68, 0x00, 0x65, 0x00, 0x65, 0x00, 0x74, 0x00, 0x31, 0x00, 0x21, 0x53, 0x00, 0x68, 0x00, 0x65, 0x00, 0x65, 0x00, 0x74, 0x00, 0x31, 0x00, 0x21,
0x00, 0x41, 0x00, 0x31, 0x00, 0x00, 0x00}; 0x00, 0x41, 0x00, 0x31, 0x00, 0x00, 0x00};
private void confirmGUID(byte[] expectedGuid, byte[] actualGuid) { private static final byte[] dataLinkToWorkbook = HexRead.readFromString("01 00 01 00 01 00 01 00 " +
assertTrue(Arrays.equals(expectedGuid, actualGuid)); "D0 C9 EA 79 F9 BA CE 11 8C 82 00 AA 00 4B A9 0B " +
"02 00 00 00 " +
"1D 00 00 00 " + // options: LABEL | PLACE | FILE_OR_URL
// label: "My Label"
"09 00 00 00 " +
"4D 00 79 00 20 00 4C 00 61 00 62 00 65 00 6C 00 00 00 " +
"03 03 00 00 00 00 00 00 C0 00 00 00 00 00 00 46 " + // file GUID
"00 00 " + // file options
// shortFileName: "YEARFR~1.XLS"
"0D 00 00 00 " +
"59 45 41 52 46 52 7E 31 2E 58 4C 53 00 " +
// FILE_TAIL - unknown byte sequence
"FF FF AD DE 00 00 00 00 " +
"00 00 00 00 00 00 00 00 " +
"00 00 00 00 00 00 00 00 " +
// field len, char data len
"2E 00 00 00 " +
"28 00 00 00 " +
"03 00 " + // unknown ushort
// _address: "yearfracExamples.xls"
"79 00 65 00 61 00 72 00 66 00 72 00 61 00 63 00 " +
"45 00 78 00 61 00 6D 00 70 00 6C 00 65 00 73 00 " +
"2E 00 78 00 6C 00 73 00 " +
// textMark: "Sheet1!B6"
"0A 00 00 00 " +
"53 00 68 00 65 00 65 00 74 00 31 00 21 00 42 00 " +
"36 00 00 00");
private static final byte[] dataTargetFrame = HexRead.readFromString("0E 00 0E 00 00 00 00 00 " +
"D0 C9 EA 79 F9 BA CE 11 8C 82 00 AA 00 4B A9 0B " +
"02 00 00 00 " +
"83 00 00 00 " + // options: TARGET_FRAME | ABS | FILE_OR_URL
// targetFrame: "_blank"
"07 00 00 00 " +
"5F 00 62 00 6C 00 61 00 6E 00 6B 00 00 00 " +
// url GUID
"E0 C9 EA 79 F9 BA CE 11 8C 82 00 AA 00 4B A9 0B " +
// address: "http://www.regnow.com/softsell/nph-softsell.cgi?currency=USD&item=7924-37"
"94 00 00 00 " +
"68 00 74 00 74 00 70 00 3A 00 2F 00 2F 00 77 00 " +
"77 00 77 00 2E 00 72 00 65 00 67 00 6E 00 6F 00 " +
"77 00 2E 00 63 00 6F 00 6D 00 2F 00 73 00 6F 00 " +
"66 00 74 00 73 00 65 00 6C 00 6C 00 2F 00 6E 00 " +
"70 00 68 00 2D 00 73 00 6F 00 66 00 74 00 73 00 " +
"65 00 6C 00 6C 00 2E 00 63 00 67 00 69 00 3F 00 " +
"63 00 75 00 72 00 72 00 65 00 6E 00 63 00 79 00 " +
"3D 00 55 00 53 00 44 00 26 00 69 00 74 00 65 00 " +
"6D 00 3D 00 37 00 39 00 32 00 34 00 2D 00 33 00 " +
"37 00 00 00");
private static final byte[] dataUNC = HexRead.readFromString("01 00 01 00 01 00 01 00 " +
"D0 C9 EA 79 F9 BA CE 11 8C 82 00 AA 00 4B A9 0B " +
"02 00 00 00 " +
"1F 01 00 00 " + // options: UNC_PATH | LABEL | TEXT_MARK | ABS | FILE_OR_URL
"09 00 00 00 " + // label: "My Label"
"4D 00 79 00 20 00 6C 00 61 00 62 00 65 00 6C 00 00 00 " +
// note - no moniker GUID
"27 00 00 00 " + // "\\\\MyServer\\my-share\\myDir\\PRODNAME.xls"
"5C 00 5C 00 4D 00 79 00 53 00 65 00 72 00 76 00 " +
"65 00 72 00 5C 00 6D 00 79 00 2D 00 73 00 68 00 " +
"61 00 72 00 65 00 5C 00 6D 00 79 00 44 00 69 00 " +
"72 00 5C 00 50 00 52 00 4F 00 44 00 4E 00 41 00 " +
"4D 00 45 00 2E 00 78 00 6C 00 73 00 00 00 " +
"0C 00 00 00 " + // textMark: PRODNAME!C2
"50 00 52 00 4F 00 44 00 4E 00 41 00 4D 00 45 00 21 00 " +
"43 00 32 00 00 00");
private void confirmGUID(GUID expectedGuid, GUID actualGuid) {
assertEquals(expectedGuid, actualGuid);
} }
public void testReadURLLink(){ public void testReadURLLink(){
RecordInputStream is = TestcaseRecordInputStream.create(HyperlinkRecord.sid, data1); RecordInputStream is = TestcaseRecordInputStream.create(HyperlinkRecord.sid, data1);
@ -203,7 +280,8 @@ public final class TestHyperlinkRecord extends TestCase {
assertEquals(opts, link.getLinkOptions()); assertEquals(opts, link.getLinkOptions());
assertEquals("file", link.getLabel()); assertEquals("file", link.getLabel());
assertEquals("link1.xls", link.getAddress()); assertEquals("link1.xls", link.getShortFilename());
assertEquals(null, link.getAddress());
} }
public void testReadEmailLink(){ public void testReadEmailLink(){
@ -238,7 +316,8 @@ public final class TestHyperlinkRecord extends TestCase {
assertEquals(opts, link.getLinkOptions()); assertEquals(opts, link.getLinkOptions());
assertEquals("place", link.getLabel()); assertEquals("place", link.getLabel());
assertEquals("Sheet1!A1", link.getAddress()); assertEquals("Sheet1!A1", link.getTextMark());
assertEquals(null, link.getAddress());
} }
private void serialize(byte[] data){ private void serialize(byte[] data){
@ -280,7 +359,7 @@ public final class TestHyperlinkRecord extends TestCase {
link.setFirstRow((short)0); link.setFirstRow((short)0);
link.setLastRow((short)0); link.setLastRow((short)0);
link.setLabel("file"); link.setLabel("file");
link.setAddress("link1.xls"); link.setShortFilename("link1.xls");
byte[] tmp = link.serialize(); byte[] tmp = link.serialize();
byte[] ser = new byte[tmp.length-4]; byte[] ser = new byte[tmp.length-4];
@ -295,7 +374,7 @@ public final class TestHyperlinkRecord extends TestCase {
link.setFirstRow((short)3); link.setFirstRow((short)3);
link.setLastRow((short)3); link.setLastRow((short)3);
link.setLabel("place"); link.setLabel("place");
link.setAddress("Sheet1!A1"); link.setTextMark("Sheet1!A1");
byte[] tmp = link.serialize(); byte[] tmp = link.serialize();
byte[] ser = new byte[tmp.length-4]; byte[] ser = new byte[tmp.length-4];
@ -329,4 +408,70 @@ public final class TestHyperlinkRecord extends TestCase {
} }
} }
public void testReserializeTargetFrame() {
RecordInputStream in = TestcaseRecordInputStream.create(HyperlinkRecord.sid, dataTargetFrame);
HyperlinkRecord hr = new HyperlinkRecord(in);
byte[] ser = hr.serialize();
TestcaseRecordInputStream.confirmRecordEncoding(HyperlinkRecord.sid, dataTargetFrame, ser);
}
public void testReserializeLinkToWorkbook() {
RecordInputStream in = TestcaseRecordInputStream.create(HyperlinkRecord.sid, dataLinkToWorkbook);
HyperlinkRecord hr = new HyperlinkRecord(in);
byte[] ser = hr.serialize();
TestcaseRecordInputStream.confirmRecordEncoding(HyperlinkRecord.sid, dataLinkToWorkbook, ser);
if ("YEARFR~1.XLS".equals(hr.getAddress())) {
throw new AssertionFailedError("Identified bug in reading workbook link");
}
assertEquals("yearfracExamples.xls", hr.getAddress());
}
public void testReserializeUNC() {
RecordInputStream in = TestcaseRecordInputStream.create(HyperlinkRecord.sid, dataUNC);
HyperlinkRecord hr = new HyperlinkRecord(in);
byte[] ser = hr.serialize();
TestcaseRecordInputStream.confirmRecordEncoding(HyperlinkRecord.sid, dataUNC, ser);
}
public void testGUID() {
GUID g;
g = GUID.parse("3F2504E0-4F89-11D3-9A0C-0305E82C3301");
confirmGUID(g, 0x3F2504E0, 0x4F89, 0x11D3, 0x9A0C0305E82C3301L);
assertEquals("3F2504E0-4F89-11D3-9A0C-0305E82C3301", g.formatAsString());
g = GUID.parse("13579BDF-0246-8ACE-0123-456789ABCDEF");
confirmGUID(g, 0x13579BDF, 0x0246, 0x8ACE, 0x0123456789ABCDEFL);
assertEquals("13579BDF-0246-8ACE-0123-456789ABCDEF", g.formatAsString());
byte[] buf = new byte[16];
g.serialize(new LittleEndianByteArrayOutputStream(buf, 0));
String expectedDump = "[DF, 9B, 57, 13, 46, 02, CE, 8A, 01, 23, 45, 67, 89, AB, CD, EF]";
assertEquals(expectedDump, HexDump.toHex(buf));
// STD Moniker
g = createFromStreamDump("[D0, C9, EA, 79, F9, BA, CE, 11, 8C, 82, 00, AA, 00, 4B, A9, 0B]");
assertEquals("79EAC9D0-BAF9-11CE-8C82-00AA004BA90B", g.formatAsString());
// URL Moniker
g = createFromStreamDump("[E0, C9, EA, 79, F9, BA, CE, 11, 8C, 82, 00, AA, 00, 4B, A9, 0B]");
assertEquals("79EAC9E0-BAF9-11CE-8C82-00AA004BA90B", g.formatAsString());
// File Moniker
g = createFromStreamDump("[03, 03, 00, 00, 00, 00, 00, 00, C0, 00, 00, 00, 00, 00, 00, 46]");
assertEquals("00000303-0000-0000-C000-000000000046", g.formatAsString());
}
private static GUID createFromStreamDump(String s) {
return new GUID(new LittleEndianByteArrayInputStream(HexRead.readFromString(s)));
}
private void confirmGUID(GUID g, int d1, int d2, int d3, long d4) {
assertEquals(new String(HexDump.intToHex(d1)), new String(HexDump.intToHex(g.getD1())));
assertEquals(new String(HexDump.shortToHex(d2)), new String(HexDump.shortToHex(g.getD2())));
assertEquals(new String(HexDump.shortToHex(d3)), new String(HexDump.shortToHex(g.getD3())));
assertEquals(new String(HexDump.longToHex(d4)), new String(HexDump.longToHex(g.getD4())));
}
} }

View File

@ -70,7 +70,7 @@ public final class TestHSSFHyperlink extends TestCase {
assertNotNull(link); assertNotNull(link);
assertEquals("Link To First Sheet", link.getLabel()); assertEquals("Link To First Sheet", link.getLabel());
assertEquals("Link To First Sheet", cell.getRichStringCellValue().getString()); assertEquals("Link To First Sheet", cell.getRichStringCellValue().getString());
assertEquals("WebLinks!A1", link.getAddress()); assertEquals("WebLinks!A1", link.getTextMark());
} }
public void testModify() throws Exception { public void testModify() throws Exception {
@ -136,7 +136,7 @@ public final class TestHSSFHyperlink extends TestCase {
cell = sheet.createRow(3).createCell(0); cell = sheet.createRow(3).createCell(0);
cell.setCellValue("Worksheet Link"); cell.setCellValue("Worksheet Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT); link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
link.setAddress("'Target Sheet'!A1"); link.setTextMark("'Target Sheet'!A1");
cell.setHyperlink(link); cell.setHyperlink(link);
//serialize and read again //serialize and read again
@ -163,7 +163,7 @@ public final class TestHSSFHyperlink extends TestCase {
cell = sheet.getRow(3).getCell(0); cell = sheet.getRow(3).getCell(0);
link = cell.getHyperlink(); link = cell.getHyperlink();
assertNotNull(link); assertNotNull(link);
assertEquals("'Target Sheet'!A1", link.getAddress()); assertEquals("'Target Sheet'!A1", link.getTextMark());
} }
public void testCloneSheet() { public void testCloneSheet() {