Fixed NoteRecord to allow for unicode author names

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@719084 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-11-19 21:49:17 +00:00
parent b5ff76d149
commit 19e41b0614
2 changed files with 93 additions and 70 deletions

View File

@ -17,14 +17,15 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianOutput;
import org.apache.poi.util.StringUtil;
/** /**
* NOTE: Comment Associated with a Cell (1Ch) * NOTE: Comment Associated with a Cell (0x001C)<p/>
* *
* @author Yegor Kozlov * @author Yegor Kozlov
*/ */
public final class NoteRecord extends Record { public final class NoteRecord extends StandardRecord {
public final static short sid = 0x001C; public final static short sid = 0x001C;
/** /**
@ -37,88 +38,95 @@ public final class NoteRecord extends Record {
*/ */
public final static short NOTE_VISIBLE = 0x2; public final static short NOTE_VISIBLE = 0x2;
private short field_1_row; private static final Byte DEFAULT_PADDING = new Byte((byte)0);
private short field_2_col;
private short field_3_flags; private short field_1_row;
private short field_4_shapeid; private short field_2_col;
private String field_5_author; private short field_3_flags;
private short field_4_shapeid;
private boolean field_5_hasMultibyte;
private String field_6_author;
/**
* Saves padding byte value to reduce delta during round-trip serialization.<br/>
*
* The documentation is not clear about how padding should work. In any case
* Excel(2007) does something different.
*/
private Byte field_7_padding;
/** /**
* Construct a new <code>NoteRecord</code> and * Construct a new <code>NoteRecord</code> and
* fill its data with the default values * fill its data with the default values
*/ */
public NoteRecord() public NoteRecord() {
{ field_6_author = "";
field_5_author = "";
field_3_flags = 0; field_3_flags = 0;
field_7_padding = DEFAULT_PADDING; // seems to be always present regardless of author text
} }
/** /**
* @return id of this record. * @return id of this record.
*/ */
public short getSid() public short getSid() {
{
return sid; return sid;
} }
/** /**
* Read the record data from the supplied <code>RecordInputStream</code> * Read the record data from the supplied <code>RecordInputStream</code>
*/ */
public NoteRecord(RecordInputStream in) public NoteRecord(RecordInputStream in) {
{
field_1_row = in.readShort(); field_1_row = in.readShort();
field_2_col = in.readShort(); field_2_col = in.readShort();
field_3_flags = in.readShort(); field_3_flags = in.readShort();
field_4_shapeid = in.readShort(); field_4_shapeid = in.readShort();
int length = in.readShort(); int length = in.readShort();
byte[] bytes = in.readRemainder(); field_5_hasMultibyte = in.readByte() != 0x00;
field_5_author = new String(bytes, 1, length); if (field_5_hasMultibyte) {
field_6_author = StringUtil.readUnicodeLE(in, length);
} else {
field_6_author = StringUtil.readCompressedUnicode(in, length);
}
if (in.available() == 1) {
field_7_padding = new Byte(in.readByte());
}
} }
/** public void serialize(LittleEndianOutput out) {
* Serialize the record data into the supplied array of bytes out.writeShort(field_1_row);
* out.writeShort(field_2_col);
* @param offset offset in the <code>data</code> out.writeShort(field_3_flags);
* @param data the data to serialize into out.writeShort(field_4_shapeid);
* out.writeShort(field_6_author.length());
* @return size of the record out.writeByte(field_5_hasMultibyte ? 0x01 : 0x00);
*/ if (field_5_hasMultibyte) {
public int serialize(int offset, byte [] data) StringUtil.putUnicodeLE(field_6_author, out);
{ } else {
LittleEndian.putShort(data, 0 + offset, sid); StringUtil.putCompressedUnicode(field_6_author, out);
LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4)); }
if (field_7_padding != null) {
LittleEndian.putShort(data, 4 + offset , field_1_row); out.writeByte(field_7_padding.intValue());
LittleEndian.putShort(data, 6 + offset , field_2_col); }
LittleEndian.putShort(data, 8 + offset , field_3_flags);
LittleEndian.putShort(data, 10 + offset , field_4_shapeid);
LittleEndian.putShort(data, 12 + offset , (short)field_5_author.length());
byte[] str = field_5_author.getBytes();
System.arraycopy(str, 0, data, 15 + offset, str.length);
return getRecordSize();
} }
protected int getDataSize() { protected int getDataSize() {
return 2 + 2 + 2 + 2 + 2 + 1 + field_5_author.length() + 1; return 11 // 5 shorts + 1 byte
+ field_6_author.length() * (field_5_hasMultibyte ? 2 : 1)
+ (field_7_padding == null ? 0 : 1);
} }
/** /**
* Convert this record to string. * Convert this record to string.
* Used by BiffViewer and other utilities. * Used by BiffViewer and other utilities.
*/ */
public String toString() public String toString() {
{
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append("[NOTE]\n"); buffer.append("[NOTE]\n");
buffer.append(" .recordid = 0x" + Integer.toHexString( getSid() ) + ", size = " + getRecordSize() + "\n"); buffer.append(" .row = ").append(field_1_row).append("\n");
buffer.append(" .row = " + field_1_row + "\n"); buffer.append(" .col = ").append(field_2_col).append("\n");
buffer.append(" .col = " + field_2_col + "\n"); buffer.append(" .flags = ").append(field_3_flags).append("\n");
buffer.append(" .flags = " + field_3_flags + "\n"); buffer.append(" .shapeid= ").append(field_4_shapeid).append("\n");
buffer.append(" .shapeid = " + field_4_shapeid + "\n"); buffer.append(" .author = ").append(field_6_author).append("\n");
buffer.append(" .author = " + field_5_author + "\n");
buffer.append("[/NOTE]\n"); buffer.append("[/NOTE]\n");
return buffer.toString(); return buffer.toString();
} }
@ -201,7 +209,7 @@ public final class NoteRecord extends Record {
* @return the name of the original author of the comment * @return the name of the original author of the comment
*/ */
public String getAuthor(){ public String getAuthor(){
return field_5_author; return field_6_author;
} }
/** /**
@ -210,7 +218,7 @@ public final class NoteRecord extends Record {
* @param author the name of the original author of the comment * @param author the name of the original author of the comment
*/ */
public void setAuthor(String author){ public void setAuthor(String author){
field_5_author = author; field_6_author = author;
} }
public Object clone() { public Object clone() {
@ -219,8 +227,7 @@ public final class NoteRecord extends Record {
rec.field_2_col = field_2_col; rec.field_2_col = field_2_col;
rec.field_3_flags = field_3_flags; rec.field_3_flags = field_3_flags;
rec.field_4_shapeid = field_4_shapeid; rec.field_4_shapeid = field_4_shapeid;
rec.field_5_author = field_5_author; rec.field_6_author = field_6_author;
return rec; return rec;
} }
} }

View File

@ -17,11 +17,13 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.Arrays; import java.util.Arrays;
import org.apache.poi.util.HexRead;
/** /**
* Tests the serialization and deserialization of the NoteRecord * Tests the serialization and deserialization of the NoteRecord
* class works correctly. Test data taken directly from a real * class works correctly. Test data taken directly from a real
@ -30,16 +32,16 @@ import java.util.Arrays;
* @author Yegor Kozlov * @author Yegor Kozlov
*/ */
public final class TestNoteRecord extends TestCase { public final class TestNoteRecord extends TestCase {
private byte[] data = new byte[] { private byte[] testData = HexRead.readFromString(
0x06, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02, 0x04, 0x1A, 0x00, "06 00 01 00 02 00 02 04 " +
0x00, 0x41, 0x70, 0x61, 0x63, 0x68, 0x65, 0x20, 0x53, 0x6F, "1A 00 00 " +
0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x20, 0x46, 0x6F, 0x75, "41 70 61 63 68 65 20 53 6F 66 74 77 61 72 65 20 46 6F 75 6E 64 61 74 69 6F 6E " +
0x6E, 0x64, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x00 "00" // padding byte
}; );
public void testRead() { public void testRead() {
NoteRecord record = new NoteRecord(TestcaseRecordInputStream.create(NoteRecord.sid, data)); NoteRecord record = new NoteRecord(TestcaseRecordInputStream.create(NoteRecord.sid, testData));
assertEquals(NoteRecord.sid, record.getSid()); assertEquals(NoteRecord.sid, record.getSid());
assertEquals(6, record.getRow()); assertEquals(6, record.getRow());
@ -47,7 +49,6 @@ public final class TestNoteRecord extends TestCase {
assertEquals(NoteRecord.NOTE_VISIBLE, record.getFlags()); assertEquals(NoteRecord.NOTE_VISIBLE, record.getFlags());
assertEquals(1026, record.getShapeId()); assertEquals(1026, record.getShapeId());
assertEquals("Apache Software Foundation", record.getAuthor()); assertEquals("Apache Software Foundation", record.getAuthor());
} }
public void testWrite() { public void testWrite() {
@ -60,16 +61,11 @@ public final class TestNoteRecord extends TestCase {
record.setShapeId((short)1026); record.setShapeId((short)1026);
record.setAuthor("Apache Software Foundation"); record.setAuthor("Apache Software Foundation");
byte [] ser = record.serialize(); byte[] ser = record.serialize();
assertEquals(ser.length - 4, data.length); TestcaseRecordInputStream.confirmRecordEncoding(NoteRecord.sid, testData, ser);
byte[] recdata = new byte[ser.length - 4];
System.arraycopy(ser, 4, recdata, 0, recdata.length);
assertTrue(Arrays.equals(data, recdata));
} }
public void testClone() public void testClone() {
{
NoteRecord record = new NoteRecord(); NoteRecord record = new NoteRecord();
record.setRow((short)1); record.setRow((short)1);
@ -90,4 +86,24 @@ public final class TestNoteRecord extends TestCase {
byte[] cln = cloned.serialize(); byte[] cln = cloned.serialize();
assertTrue(Arrays.equals(src, cln)); assertTrue(Arrays.equals(src, cln));
} }
public void testUnicodeAuthor() {
// This sample data was created by setting the 'user name' field in the 'Personalize'
// section of Excel's options to \u30A2\u30D1\u30C3\u30C1\u65CF, and then
// creating a cell comment.
byte[] data = HexRead.readFromString("01 00 01 00 00 00 03 00 " +
"05 00 01 " + // len=5, 16bit
"A2 30 D1 30 C3 30 C1 30 CF 65 " + // character data
"00 " // padding byte
);
RecordInputStream in = TestcaseRecordInputStream.create(NoteRecord.sid, data);
NoteRecord nr = new NoteRecord(in);
if ("\u00A2\u0030\u00D1\u0030\u00C3".equals(nr.getAuthor())) {
throw new AssertionFailedError("Identified bug in reading note with unicode author");
}
assertEquals("\u30A2\u30D1\u30C3\u30C1\u65CF", nr.getAuthor());
byte[] ser = nr.serialize();
TestcaseRecordInputStream.confirmRecordEncoding(NoteRecord.sid, data, ser);
}
} }