From a09a3398ccb17ad5d8390800ef723ff90c96bbc9 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Fri, 30 Jun 2017 20:21:33 +0000 Subject: [PATCH] #61243 - Refactor and unify toString/toXml in DDF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1800452 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ddf/AbstractEscherOptRecord.java | 60 +---- .../apache/poi/ddf/EscherArrayProperty.java | 17 +- .../org/apache/poi/ddf/EscherBSERecord.java | 79 ++---- .../org/apache/poi/ddf/EscherBitmapBlip.java | 30 +-- .../org/apache/poi/ddf/EscherBlipRecord.java | 22 +- .../apache/poi/ddf/EscherBoolProperty.java | 3 +- .../poi/ddf/EscherChildAnchorRecord.java | 41 +-- .../poi/ddf/EscherClientAnchorRecord.java | 105 +++----- .../poi/ddf/EscherClientDataRecord.java | 40 +-- .../org/apache/poi/ddf/EscherColorRef.java | 36 ++- .../apache/poi/ddf/EscherComplexProperty.java | 2 +- .../apache/poi/ddf/EscherContainerRecord.java | 67 ++--- .../org/apache/poi/ddf/EscherDgRecord.java | 32 +-- .../org/apache/poi/ddf/EscherDggRecord.java | 73 +++--- src/java/org/apache/poi/ddf/EscherDump.java | 30 ++- .../apache/poi/ddf/EscherMetafileBlip.java | 101 ++++---- .../org/apache/poi/ddf/EscherOptRecord.java | 3 +- .../org/apache/poi/ddf/EscherPictBlip.java | 67 +++-- .../org/apache/poi/ddf/EscherProperty.java | 6 +- .../org/apache/poi/ddf/EscherRGBProperty.java | 2 +- src/java/org/apache/poi/ddf/EscherRecord.java | 241 +++++++++++++++--- .../apache/poi/ddf/EscherSimpleProperty.java | 20 +- .../org/apache/poi/ddf/EscherSpRecord.java | 38 +-- .../org/apache/poi/ddf/EscherSpgrRecord.java | 42 +-- .../poi/ddf/EscherSplitMenuColorsRecord.java | 42 +-- .../apache/poi/ddf/EscherTextboxRecord.java | 69 ++--- .../apache/poi/ddf/UnknownEscherRecord.java | 60 ++--- .../poi/hslf/record/EscherPlaceholder.java | 30 ++- .../apache/poi/ddf/TestEscherBSERecord.java | 67 +++-- .../apache/poi/ddf/TestEscherBlipRecord.java | 32 +-- .../poi/ddf/TestEscherBoolProperty.java | 7 +- .../poi/ddf/TestEscherChildAnchorRecord.java | 30 ++- .../poi/ddf/TestEscherClientAnchorRecord.java | 44 ++-- .../poi/ddf/TestEscherClientDataRecord.java | 25 +- .../poi/ddf/TestEscherContainerRecord.java | 124 +++++---- .../apache/poi/ddf/TestEscherDgRecord.java | 25 +- .../apache/poi/ddf/TestEscherDggRecord.java | 34 ++- .../apache/poi/ddf/TestEscherOptRecord.java | 87 +++---- .../apache/poi/ddf/TestEscherProperty.java | 13 +- .../apache/poi/ddf/TestEscherSpRecord.java | 26 +- .../apache/poi/ddf/TestEscherSpgrRecord.java | 30 ++- .../ddf/TestEscherSplitMenuColorsRecord.java | 30 ++- .../poi/ddf/TestUnknownEscherRecord.java | 39 ++- 43 files changed, 972 insertions(+), 999 deletions(-) diff --git a/src/java/org/apache/poi/ddf/AbstractEscherOptRecord.java b/src/java/org/apache/poi/ddf/AbstractEscherOptRecord.java index d0563151e..fa6851c5a 100644 --- a/src/java/org/apache/poi/ddf/AbstractEscherOptRecord.java +++ b/src/java/org/apache/poi/ddf/AbstractEscherOptRecord.java @@ -22,7 +22,6 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; /** @@ -175,53 +174,20 @@ public abstract class AbstractEscherOptRecord extends EscherRecord } } - /** - * Retrieve the string representation of this record. - */ @Override - public String toString() - { - String nl = System.getProperty( "line.separator" ); - - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append( getClass().getName() ); - stringBuilder.append( ":" ); - stringBuilder.append( nl ); - stringBuilder.append( " isContainer: " ); - stringBuilder.append( isContainerRecord() ); - stringBuilder.append( nl ); - stringBuilder.append( " version: 0x" ); - stringBuilder.append( HexDump.toHex( getVersion() ) ); - stringBuilder.append( nl ); - stringBuilder.append( " instance: 0x" ); - stringBuilder.append( HexDump.toHex( getInstance() ) ); - stringBuilder.append( nl ); - stringBuilder.append( " recordId: 0x" ); - stringBuilder.append( HexDump.toHex( getRecordId() ) ); - stringBuilder.append( nl ); - stringBuilder.append( " numchildren: " ); - stringBuilder.append( getChildRecords().size() ); - stringBuilder.append( nl ); - stringBuilder.append( " properties:" ); - stringBuilder.append( nl ); - - for ( EscherProperty property : properties ) - { - stringBuilder.append(" ").append(property).append(nl); + protected Object[][] getAttributeMap() { + List attrList = new ArrayList(properties.size()*2+2); + attrList.add("properties"); + attrList.add(properties.size()); + for ( EscherProperty property : properties ) { + attrList.add(property.getName()); + attrList.add(property); } - - return stringBuilder.toString(); - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), - HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))); - for (EscherProperty property: getEscherProperties()){ - builder.append(property.toXml(tab+"\t")); - } - builder.append(tab).append("\n"); - return builder.toString(); + + return new Object[][]{ + { "isContainer", isContainerRecord() }, + { "numchildren", getChildRecords().size() }, + attrList.toArray() + }; } } diff --git a/src/java/org/apache/poi/ddf/EscherArrayProperty.java b/src/java/org/apache/poi/ddf/EscherArrayProperty.java index aa5a51926..9033a49ef 100644 --- a/src/java/org/apache/poi/ddf/EscherArrayProperty.java +++ b/src/java/org/apache/poi/ddf/EscherArrayProperty.java @@ -121,6 +121,11 @@ public final class EscherArrayProperty extends EscherComplexProperty implements @Override public String toString() { StringBuilder results = new StringBuilder(); + results.append("propNum: ").append(getPropertyNumber()); + results.append(", propName: ").append(EscherProperties.getPropertyName( getPropertyNumber() )); + results.append(", complex: ").append(isComplex()); + results.append(", blipId: ").append(isBlipId()); + results.append(", data: \n"); results.append(" {EscherArrayProperty:" + '\n'); results.append(" Num Elements: ").append(getNumberOfElementsInArray()).append('\n'); results.append(" Num Elements In Memory: ").append(getNumberOfElementsInMemory()).append('\n'); @@ -130,11 +135,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements } results.append("}" + '\n'); - return "propNum: " + getPropertyNumber() - + ", propName: " + EscherProperties.getPropertyName( getPropertyNumber() ) - + ", complex: " + isComplex() - + ", blipId: " + isBlipId() - + ", data: " + '\n' + results; + return results.toString(); } @Override @@ -146,7 +147,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements for (int i = 0; i < getNumberOfElementsInArray(); i++) { builder.append("\t").append(tab).append("").append(HexDump.toHex(getElement(i))).append("\n"); } - builder.append(tab).append("\n"); + builder.append(tab).append(""); return builder.toString(); } @@ -220,7 +221,9 @@ public final class EscherArrayProperty extends EscherComplexProperty implements @Override public byte[] next() { - if (!hasNext()) throw new NoSuchElementException(); + if (!hasNext()) { + throw new NoSuchElementException(); + } return getElement(idx++); } diff --git a/src/java/org/apache/poi/ddf/EscherBSERecord.java b/src/java/org/apache/poi/ddf/EscherBSERecord.java index 776881038..efeb3f830 100644 --- a/src/java/org/apache/poi/ddf/EscherBSERecord.java +++ b/src/java/org/apache/poi/ddf/EscherBSERecord.java @@ -17,11 +17,10 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; /** - * The BSE record is related closely to the EscherBlipRecord and stores + * The BSE record is related closely to the {@code EscherBlipRecord} and stores * extra information about the blip. A blip record is actually stored inside * the BSE record even though the BSE record isn't actually a container record. * @@ -55,6 +54,10 @@ public final class EscherBSERecord extends EscherRecord { private byte[] _remainingData = new byte[0]; + public EscherBSERecord() { + setRecordId(RECORD_ID); + } + @Override public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) { int bytesRemaining = readHeader( data, offset ); @@ -103,8 +106,7 @@ public final class EscherBSERecord extends EscherRecord { data[offset + 8] = field_1_blipTypeWin32; data[offset + 9] = field_2_blipTypeMacOS; - for ( int i = 0; i < 16; i++ ) - data[offset + 10 + i] = field_3_uid[i]; + System.arraycopy(field_3_uid, 0, data, offset + 10, 16); LittleEndian.putShort( data, offset + 26, field_4_tag ); LittleEndian.putInt( data, offset + 28, field_5_size ); LittleEndian.putInt( data, offset + 32, field_6_ref ); @@ -114,8 +116,7 @@ public final class EscherBSERecord extends EscherRecord { data[offset + 42] = field_10_unused2; data[offset + 43] = field_11_unused3; int bytesWritten = 0; - if (field_12_blipRecord != null) - { + if (field_12_blipRecord != null) { bytesWritten = field_12_blipRecord.serialize( offset + 44, data, new NullEscherSerializationListener() ); } System.arraycopy( _remainingData, 0, data, offset + 44 + bytesWritten, _remainingData.length ); @@ -350,52 +351,7 @@ public final class EscherBSERecord extends EscherRecord { * @param remainingData the remaining bytes */ public void setRemainingData(byte[] remainingData) { - if (remainingData == null) { - _remainingData = new byte[0]; - } else { - _remainingData = remainingData.clone(); - } - } - - @Override - public String toString() { - String extraData = _remainingData == null ? null : HexDump.toHex(_remainingData, 32); - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex( RECORD_ID ) + '\n' + - " Version: 0x" + HexDump.toHex( getVersion() ) + '\n' + - " Instance: 0x" + HexDump.toHex( getInstance() ) + '\n' + - " BlipTypeWin32: " + field_1_blipTypeWin32 + '\n' + - " BlipTypeMacOS: " + field_2_blipTypeMacOS + '\n' + - " SUID: " + (field_3_uid == null ? "" : HexDump.toHex(field_3_uid)) + '\n' + - " Tag: " + field_4_tag + '\n' + - " Size: " + field_5_size + '\n' + - " Ref: " + field_6_ref + '\n' + - " Offset: " + field_7_offset + '\n' + - " Usage: " + field_8_usage + '\n' + - " Name: " + field_9_name + '\n' + - " Unused2: " + field_10_unused2 + '\n' + - " Unused3: " + field_11_unused3 + '\n' + - " blipRecord: " + field_12_blipRecord + '\n' + - " Extra Data:" + '\n' + extraData; - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(field_1_blipTypeWin32).append("\n") - .append(tab).append("\t").append("").append(field_2_blipTypeMacOS).append("\n") - .append(tab).append("\t").append("").append(field_3_uid == null ? "" : HexDump.toHex(field_3_uid)).append("\n") - .append(tab).append("\t").append("").append(field_4_tag).append("\n") - .append(tab).append("\t").append("").append(field_5_size).append("\n") - .append(tab).append("\t").append("").append(field_6_ref).append("\n") - .append(tab).append("\t").append("").append(field_7_offset).append("\n") - .append(tab).append("\t").append("").append(field_8_usage).append("\n") - .append(tab).append("\t").append("").append(field_9_name).append("\n") - .append(tab).append("\t").append("").append(field_10_unused2).append("\n") - .append(tab).append("\t").append("").append(field_11_unused3).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); + _remainingData = (remainingData == null) ? new byte[0] : remainingData.clone(); } /** @@ -421,4 +377,23 @@ public final class EscherBSERecord extends EscherRecord { } return " Client"; } + + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "BlipTypeWin32", field_1_blipTypeWin32 }, + { "BlipTypeMacOS", field_2_blipTypeMacOS }, + { "SUID", field_3_uid }, + { "Tag", field_4_tag }, + { "Size", field_5_size }, + { "Ref", field_6_ref }, + { "Offset", field_7_offset }, + { "Usage", field_8_usage }, + { "Name", field_9_name }, + { "Unused2", field_10_unused2 }, + { "Unused3", field_11_unused3 }, + { "Blip Record", field_12_blipRecord }, + { "Extra Data", _remainingData } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherBitmapBlip.java b/src/java/org/apache/poi/ddf/EscherBitmapBlip.java index 84a4824b2..825a4b390 100644 --- a/src/java/org/apache/poi/ddf/EscherBitmapBlip.java +++ b/src/java/org/apache/poi/ddf/EscherBitmapBlip.java @@ -17,7 +17,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; public class EscherBitmapBlip extends EscherBlipRecord { @@ -110,29 +109,10 @@ public class EscherBitmapBlip extends EscherBlipRecord { } @Override - public String toString() { - String nl = System.getProperty( "line.separator" ); - - String extraData = HexDump.dump(getPicturedata(), 0, 0); - - return getClass().getName() + ":" + nl + - " RecordId: 0x" + HexDump.toHex( getRecordId() ) + nl + - " Version: 0x" + HexDump.toHex( getVersion() ) + nl + - " Instance: 0x" + HexDump.toHex( getInstance() ) + nl + - " UID: 0x" + HexDump.toHex( field_1_UID ) + nl + - " Marker: 0x" + HexDump.toHex( field_2_marker ) + nl + - " Extra Data:" + nl + extraData; - } - - @Override - public String toXml(String tab) { - String extraData = HexDump.dump(getPicturedata(), 0, 0); - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("0x").append(HexDump.toHex(field_1_UID)).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex(field_2_marker)).append("\n") - .append(tab).append("\t").append("").append(extraData).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); + protected Object[][] getAttributeMap() { + return new Object[][] { + { "Marker", field_2_marker }, + { "Extra Data", getPicturedata() } + }; } } diff --git a/src/java/org/apache/poi/ddf/EscherBlipRecord.java b/src/java/org/apache/poi/ddf/EscherBlipRecord.java index ccacc7b9c..266c1f03b 100644 --- a/src/java/org/apache/poi/ddf/EscherBlipRecord.java +++ b/src/java/org/apache/poi/ddf/EscherBlipRecord.java @@ -18,7 +18,6 @@ package org.apache.poi.ddf; import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.HexDump; public class EscherBlipRecord extends EscherRecord { public static final short RECORD_ID_START = (short) 0xF018; @@ -100,22 +99,9 @@ public class EscherBlipRecord extends EscherRecord { } @Override - public String toString() { - String extraData = HexDump.toHex(field_pictureData, 32); - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex( getRecordId() ) + '\n' + - " Version: 0x" + HexDump.toHex( getVersion() ) + '\n' + - " Instance: 0x" + HexDump.toHex( getInstance() ) + '\n' + - " Extra Data:" + '\n' + extraData; - } - - @Override - public String toXml(String tab) { - String extraData = HexDump.toHex(field_pictureData, 32); - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(extraData).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); + protected Object[][] getAttributeMap() { + return new Object[][] { + { "Extra Data", getPicturedata() } + }; } } diff --git a/src/java/org/apache/poi/ddf/EscherBoolProperty.java b/src/java/org/apache/poi/ddf/EscherBoolProperty.java index ce36bc665..a0f9d33cd 100644 --- a/src/java/org/apache/poi/ddf/EscherBoolProperty.java +++ b/src/java/org/apache/poi/ddf/EscherBoolProperty.java @@ -59,6 +59,7 @@ public class EscherBoolProperty * * @deprecated use !isTrue() instead, planed to be removed in POI 3.17 */ + @Deprecated public boolean isFalse() { return !isTrue(); @@ -77,7 +78,7 @@ public class EscherBoolProperty StringBuilder builder = new StringBuilder(); builder.append(tab).append("<").append(getClass().getSimpleName()).append(" id=\"0x").append(HexDump.toHex(getId())) .append("\" name=\"").append(getName()).append("\" simpleValue=\"").append(getPropertyValue()).append("\" blipId=\"") - .append(isBlipId()).append("\" value=\"").append(isTrue()).append("\"").append("/>\n"); + .append(isBlipId()).append("\" value=\"").append(isTrue()).append("\"").append("/>"); return builder.toString(); } } diff --git a/src/java/org/apache/poi/ddf/EscherChildAnchorRecord.java b/src/java/org/apache/poi/ddf/EscherChildAnchorRecord.java index 1fb16444f..b7a7a8d78 100644 --- a/src/java/org/apache/poi/ddf/EscherChildAnchorRecord.java +++ b/src/java/org/apache/poi/ddf/EscherChildAnchorRecord.java @@ -18,7 +18,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; /** @@ -96,37 +95,6 @@ public class EscherChildAnchorRecord } - /** - * The string representation of this record - */ - @Override - public String toString() - { - String nl = System.getProperty("line.separator"); - - return getClass().getName() + ":" + nl + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl + - " Version: 0x" + HexDump.toHex(getVersion()) + nl + - " Instance: 0x" + HexDump.toHex(getInstance()) + nl + - " X1: " + field_1_dx1 + nl + - " Y1: " + field_2_dy1 + nl + - " X2: " + field_3_dx2 + nl + - " Y2: " + field_4_dy2 + nl ; - - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(field_1_dx1).append("\n") - .append(tab).append("\t").append("").append(field_2_dy1).append("\n") - .append(tab).append("\t").append("").append(field_3_dx2).append("\n") - .append(tab).append("\t").append("").append(field_4_dy2).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - /** * Retrieves offset within the parent coordinate space for the top left point. * @@ -207,4 +175,13 @@ public class EscherChildAnchorRecord this.field_4_dy2 = field_4_dy2; } + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "X1", field_1_dx1 }, + { "Y1", field_2_dy1 }, + { "X2", field_3_dx2 }, + { "Y2", field_4_dy2 } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java b/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java index bb1cf66ee..deb11d698 100644 --- a/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java +++ b/src/java/org/apache/poi/ddf/EscherClientAnchorRecord.java @@ -17,7 +17,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; /** @@ -94,7 +93,9 @@ public class EscherClientAnchorRecord { listener.beforeRecordSerialize( offset, getRecordId(), this ); - if (remainingData == null) remainingData = new byte[0]; + if (remainingData == null) { + remainingData = new byte[0]; + } LittleEndian.putShort( data, offset, getOptions() ); LittleEndian.putShort( data, offset + 2, getRecordId() ); int remainingBytes = remainingData.length + (shortRecord ? 8 : 18); @@ -133,53 +134,9 @@ public class EscherClientAnchorRecord return "ClientAnchor"; } - /** - * Returns the string representation for this record. - * - * @return A string - */ - @Override - public String toString() - { - String nl = System.getProperty("line.separator"); - String extraData = HexDump.dump(this.remainingData, 0, 0); - return getClass().getName() + ":" + nl + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl + - " Version: 0x" + HexDump.toHex(getVersion()) + nl + - " Instance: 0x" + HexDump.toHex(getInstance()) + nl + - " Flag: " + field_1_flag + nl + - " Col1: " + field_2_col1 + nl + - " DX1: " + field_3_dx1 + nl + - " Row1: " + field_4_row1 + nl + - " DY1: " + field_5_dy1 + nl + - " Col2: " + field_6_col2 + nl + - " DX2: " + field_7_dx2 + nl + - " Row2: " + field_8_row2 + nl + - " DY2: " + field_9_dy2 + nl + - " Extra Data:" + nl + extraData; - - } - - @Override - public String toXml(String tab) { - String extraData = HexDump.dump(this.remainingData, 0, 0).trim(); - return tab + formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance())) + - tab + "\t" + "" + field_1_flag + "\n" + - tab + "\t" + "" + field_2_col1 + "\n" + - tab + "\t" + "" + field_3_dx1 + "\n" + - tab + "\t" + "" + field_4_row1 + "\n" + - tab + "\t" + "" + field_5_dy1 + "\n" + - tab + "\t" + "" + field_6_col2 + "\n" + - tab + "\t" + "" + field_7_dx2 + "\n" + - tab + "\t" + "" + field_8_row2 + "\n" + - tab + "\t" + "" + field_9_dy2 + "\n" + - tab + "\t" + "" + extraData + "\n" + - tab + "\n"; - } - /** * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells. - * + * * @return the move/size flag */ public short getFlag() @@ -189,7 +146,7 @@ public class EscherClientAnchorRecord /** * 0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells. - * + * * @param field_1_flag the move/size flag */ public void setFlag( short field_1_flag ) @@ -199,7 +156,7 @@ public class EscherClientAnchorRecord /** * The column number for the top-left position. 0 based. - * + * * @return the column number of the top-left corner */ public short getCol1() @@ -209,7 +166,7 @@ public class EscherClientAnchorRecord /** * The column number for the top-left position. 0 based. - * + * * @param field_2_col1 the column number of the top-left corner */ public void setCol1( short field_2_col1 ) @@ -219,7 +176,7 @@ public class EscherClientAnchorRecord /** * The x offset within the top-left cell. Range is from 0 to 1023. - * + * * @return the x offset of the top-left corner */ public short getDx1() @@ -229,7 +186,7 @@ public class EscherClientAnchorRecord /** * The x offset within the top-left cell. Range is from 0 to 1023. - * + * * @param field_3_dx1 the x offset of the top-left corner */ public void setDx1( short field_3_dx1 ) @@ -239,7 +196,7 @@ public class EscherClientAnchorRecord /** * The row number for the top-left corner of the shape. - * + * * @return the row number of the top-left corner */ public short getRow1() @@ -249,7 +206,7 @@ public class EscherClientAnchorRecord /** * The row number of the top-left corner of the shape. - * + * * @param field_4_row1 the row number of the top-left corner */ public void setRow1( short field_4_row1 ) @@ -259,7 +216,7 @@ public class EscherClientAnchorRecord /** * The y offset within the top-left corner of the current shape. - * + * * @return the y offset of the top-left corner */ public short getDy1() @@ -269,7 +226,7 @@ public class EscherClientAnchorRecord /** * The y offset within the top-left corner of the current shape. - * + * * @param field_5_dy1 the y offset of the top-left corner */ public void setDy1( short field_5_dy1 ) @@ -280,7 +237,7 @@ public class EscherClientAnchorRecord /** * The column of the bottom right corner of this shape. - * + * * @return the column of the bottom right corner */ public short getCol2() @@ -290,7 +247,7 @@ public class EscherClientAnchorRecord /** * The column of the bottom right corner of this shape. - * + * * @param field_6_col2 the column of the bottom right corner */ public void setCol2( short field_6_col2 ) @@ -301,7 +258,7 @@ public class EscherClientAnchorRecord /** * The x offset withing the cell for the bottom-right corner of this shape. - * + * * @return the x offset of the bottom-right corner */ public short getDx2() @@ -311,7 +268,7 @@ public class EscherClientAnchorRecord /** * The x offset withing the cell for the bottom-right corner of this shape. - * + * * @param field_7_dx2 the x offset of the bottom-right corner */ public void setDx2( short field_7_dx2 ) @@ -322,7 +279,7 @@ public class EscherClientAnchorRecord /** * The row number for the bottom-right corner of the current shape. - * + * * @return the row number for the bottom-right corner */ public short getRow2() @@ -332,7 +289,7 @@ public class EscherClientAnchorRecord /** * The row number for the bottom-right corner of the current shape. - * + * * @param field_8_row2 the row number for the bottom-right corner */ public void setRow2( short field_8_row2 ) @@ -343,7 +300,7 @@ public class EscherClientAnchorRecord /** * The y offset withing the cell for the bottom-right corner of this shape. - * + * * @return the y offset of the bottom-right corner */ public short getDy2() @@ -353,7 +310,7 @@ public class EscherClientAnchorRecord /** * The y offset withing the cell for the bottom-right corner of this shape. - * + * * @param field_9_dy2 the y offset of the bottom-right corner */ public void setDy2( short field_9_dy2 ) @@ -364,7 +321,7 @@ public class EscherClientAnchorRecord /** * Any remaining data in the record - * + * * @return the remaining bytes */ public byte[] getRemainingData() @@ -374,7 +331,7 @@ public class EscherClientAnchorRecord /** * Any remaining data in the record - * + * * @param remainingData the remaining bytes */ public void setRemainingData( byte[] remainingData ) { @@ -384,4 +341,20 @@ public class EscherClientAnchorRecord this.remainingData = remainingData.clone(); } } + + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "Flag", field_1_flag }, + { "Col1", field_2_col1 }, + { "DX1", field_3_dx1 }, + { "Row1", field_4_row1 }, + { "DY1", field_5_dy1 }, + { "Col2", field_6_col2 }, + { "DX2", field_7_dx2 }, + { "Row2", field_8_row2 }, + { "DY2", field_9_dy2 }, + { "Extra Data", remainingData } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherClientDataRecord.java b/src/java/org/apache/poi/ddf/EscherClientDataRecord.java index 749007cfe..d84054de6 100644 --- a/src/java/org/apache/poi/ddf/EscherClientDataRecord.java +++ b/src/java/org/apache/poi/ddf/EscherClientDataRecord.java @@ -18,7 +18,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; /** @@ -46,7 +45,9 @@ public class EscherClientDataRecord public int serialize(int offset, byte[] data, EscherSerializationListener listener) { listener.beforeRecordSerialize( offset, getRecordId(), this ); - if (remainingData == null) remainingData = new byte[0]; + if (remainingData == null) { + remainingData = new byte[0]; + } LittleEndian.putShort( data, offset, getOptions() ); LittleEndian.putShort( data, offset + 2, getRecordId() ); LittleEndian.putInt( data, offset + 4, remainingData.length ); @@ -73,34 +74,6 @@ public class EscherClientDataRecord return "ClientData"; } - /** - * Returns the string representation of this record. - */ - @Override - public String toString() - { - String nl = System.getProperty("line.separator"); - String extraData = HexDump.dump(getRemainingData(), 0, 0); - return getClass().getName() + ":" + nl + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl + - " Version: 0x" + HexDump.toHex(getVersion()) + nl + - " Instance: 0x" + HexDump.toHex(getInstance()) + nl + - " Extra Data:" + nl + - extraData; - - } - - @Override - public String toXml(String tab) { - String extraData = HexDump.dump(getRemainingData(), 0, 0).trim(); - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), - HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(extraData).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - /** * Any data recording this record. * @@ -121,4 +94,11 @@ public class EscherClientDataRecord ? new byte[0] : remainingData.clone(); } + + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "Extra Data", getRemainingData() } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherColorRef.java b/src/java/org/apache/poi/ddf/EscherColorRef.java index cbd62e17b..ceaf94cba 100644 --- a/src/java/org/apache/poi/ddf/EscherColorRef.java +++ b/src/java/org/apache/poi/ddf/EscherColorRef.java @@ -230,10 +230,14 @@ public class EscherColorRef { * @return {@link SysIndexSource} if {@link #hasSysIndexFlag()} is {@code true}, otherwise null */ public SysIndexSource getSysIndexSource() { - if (!hasSysIndexFlag()) return null; + if (!hasSysIndexFlag()) { + return null; + } int val = FLAG_RED.getValue(colorRef); for (SysIndexSource sis : SysIndexSource.values()) { - if (sis.value == val) return sis; + if (sis.value == val) { + return sis; + } } return null; } @@ -243,11 +247,17 @@ public class EscherColorRef { * @return {@link SysIndexProcedure} if {@link #hasSysIndexFlag()} is {@code true}, otherwise null */ public SysIndexProcedure getSysIndexProcedure() { - if (!hasSysIndexFlag()) return null; + if (!hasSysIndexFlag()) { + return null; + } int val = FLAG_GREEN.getValue(colorRef); for (SysIndexProcedure sip : SysIndexProcedure.values()) { - if (sip == SysIndexProcedure.INVERT_AFTER || sip == SysIndexProcedure.INVERT_HIGHBIT_AFTER) continue; - if (sip.mask.isSet(val)) return sip; + if (sip == SysIndexProcedure.INVERT_AFTER || sip == SysIndexProcedure.INVERT_HIGHBIT_AFTER) { + continue; + } + if (sip.mask.isSet(val)) { + return sip; + } } return null; } @@ -257,10 +267,16 @@ public class EscherColorRef { * 2 for {@link SysIndexProcedure#INVERT_HIGHBIT_AFTER} */ public int getSysIndexInvert() { - if (!hasSysIndexFlag()) return 0; + if (!hasSysIndexFlag()) { + return 0; + } int val = FLAG_GREEN.getValue(colorRef); - if ((SysIndexProcedure.INVERT_AFTER.mask.isSet(val))) return 1; - if ((SysIndexProcedure.INVERT_HIGHBIT_AFTER.mask.isSet(val))) return 2; + if ((SysIndexProcedure.INVERT_AFTER.mask.isSet(val))) { + return 1; + } + if ((SysIndexProcedure.INVERT_HIGHBIT_AFTER.mask.isSet(val))) { + return 2; + } return 0; } @@ -270,7 +286,9 @@ public class EscherColorRef { * @see org.apache.poi.hslf.record.ColorSchemeAtom#getColor(int) */ public int getSchemeIndex() { - if (!hasSchemeIndexFlag()) return -1; + if (!hasSchemeIndexFlag()) { + return -1; + } return FLAG_RED.getValue(colorRef); } diff --git a/src/java/org/apache/poi/ddf/EscherComplexProperty.java b/src/java/org/apache/poi/ddf/EscherComplexProperty.java index 06bdaf5c3..380c9c3b0 100644 --- a/src/java/org/apache/poi/ddf/EscherComplexProperty.java +++ b/src/java/org/apache/poi/ddf/EscherComplexProperty.java @@ -153,7 +153,7 @@ public class EscherComplexProperty extends EscherProperty { return tab + "<" + getClass().getSimpleName() + " id=\"0x" + HexDump.toHex(getId()) + "\" name=\"" + getName() + "\" blipId=\"" + isBlipId() + "\">\n" + - tab + "\n"; + tab + ""; //builder.append("\t").append(tab).append(dataStr); } } diff --git a/src/java/org/apache/poi/ddf/EscherContainerRecord.java b/src/java/org/apache/poi/ddf/EscherContainerRecord.java index dece611be..b09fb855c 100644 --- a/src/java/org/apache/poi/ddf/EscherContainerRecord.java +++ b/src/java/org/apache/poi/ddf/EscherContainerRecord.java @@ -126,9 +126,9 @@ public final class EscherContainerRecord extends EscherRecord implements Iterabl /** * Do any of our (top level) children have the given recordId? - * + * * @param recordId the recordId of the child - * + * * @return true, if any child has the given recordId */ public boolean hasChildOfType(short recordId) { @@ -162,7 +162,7 @@ public final class EscherContainerRecord extends EscherRecord implements Iterabl public Iterator getChildIterator() { return iterator(); } - + /** * @return an iterator over the child records */ @@ -198,7 +198,7 @@ public final class EscherContainerRecord extends EscherRecord implements Iterabl /** * Returns all of our children which are also * EscherContainers (may be 0, 1, or vary rarely 2 or 3) - * + * * @return EscherContainer children */ public List getChildContainers() { @@ -266,48 +266,6 @@ public final class EscherContainerRecord extends EscherRecord implements Iterabl _childRecords.add(idx, record); } - @Override - public String toString() - { - String nl = System.getProperty( "line.separator" ); - - StringBuffer children = new StringBuffer(); - if ( _childRecords.size() > 0 ) - { - children.append( " children: " + nl ); - - int count = 0; - for ( EscherRecord record : this ) { - children.append( " Child " + count + ":" + nl ); - String childResult = String.valueOf( record ); - childResult = childResult.replaceAll( "\n", "\n " ); - children.append( " " ); - children.append( childResult ); - children.append( nl ); - count++; - } - } - - return getClass().getName() + " (" + getRecordName() + "):" + nl - + " isContainer: " + isContainerRecord() + nl - + " version: 0x" + HexDump.toHex( getVersion() ) + nl - + " instance: 0x" + HexDump.toHex( getInstance() ) + nl - + " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl - + " numchildren: " + _childRecords.size() + nl - + children; - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getRecordName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))); - for ( EscherRecord record : this ) { - builder.append(record.toXml(tab+"\t")); - } - builder.append(tab).append("\n"); - return builder.toString(); - } - public T getChildById( short recordId ) { for ( EscherRecord childRecord : this ) { if ( childRecord.getRecordId() == recordId ) { @@ -335,4 +293,21 @@ public final class EscherContainerRecord extends EscherRecord implements Iterabl } } } + + @Override + protected Object[][] getAttributeMap() { + List chList = new ArrayList(_childRecords.size()*2+2); + chList.add("children"); + chList.add(_childRecords.size()); + int count = 0; + for ( EscherRecord record : this ) { + chList.add("Child "+count); + chList.add(record); + count++; + } + return new Object[][] { + { "isContainer", isContainerRecord() }, + chList.toArray() + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherDgRecord.java b/src/java/org/apache/poi/ddf/EscherDgRecord.java index 7de76e241..956f0f647 100644 --- a/src/java/org/apache/poi/ddf/EscherDgRecord.java +++ b/src/java/org/apache/poi/ddf/EscherDgRecord.java @@ -18,7 +18,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; /** @@ -85,29 +84,6 @@ public class EscherDgRecord return "Dg"; } - /** - * Returns the string representation of this record. - */ - @Override - public String toString() { - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + '\n' + - " Version: 0x" + HexDump.toHex(getVersion()) + '\n' + - " Instance: 0x" + HexDump.toHex(getInstance()) + '\n' + - " NumShapes: " + field_1_numShapes + '\n' + - " LastMSOSPID: " + field_2_lastMSOSPID + '\n'; - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(field_1_numShapes).append("\n") - .append(tab).append("\t").append("").append(field_2_lastMSOSPID).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - /** * The number of shapes in this drawing group. * @@ -166,4 +142,12 @@ public class EscherDgRecord { this.field_1_numShapes++; } + + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "NumShapes", field_1_numShapes }, + { "LastMSOSPID", field_2_lastMSOSPID } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherDggRecord.java b/src/java/org/apache/poi/ddf/EscherDggRecord.java index 7464e5f45..8675fa776 100644 --- a/src/java/org/apache/poi/ddf/EscherDggRecord.java +++ b/src/java/org/apache/poi/ddf/EscherDggRecord.java @@ -17,12 +17,15 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + import org.apache.poi.util.LittleEndian; import org.apache.poi.util.RecordFormatException; -import java.util.*; - /** * This record defines the drawing groups used for a particular sheet. */ @@ -82,8 +85,9 @@ public final class EscherDggRecord extends EscherRecord { size += 8; } bytesRemaining -= size; - if (bytesRemaining != 0) + if (bytesRemaining != 0) { throw new RecordFormatException("Expecting no remaining data but got " + bytesRemaining + " byte(s)."); + } return 8 + size + bytesRemaining; } @@ -125,42 +129,6 @@ public final class EscherDggRecord extends EscherRecord { return "Dgg"; } - @Override - public String toString() { - - StringBuilder field_5_string = new StringBuilder(); - if(field_5_fileIdClusters != null) for (int i = 0; i < field_5_fileIdClusters.length; i++) { - field_5_string.append(" DrawingGroupId").append(i+1).append(": "); - field_5_string.append(field_5_fileIdClusters[i].field_1_drawingGroupId); - field_5_string.append('\n'); - field_5_string.append(" NumShapeIdsUsed").append(i+1).append(": "); - field_5_string.append(field_5_fileIdClusters[i].field_2_numShapeIdsUsed); - field_5_string.append('\n'); - } - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + '\n' + - " Version: 0x" + HexDump.toHex(getVersion()) + '\n' + - " Instance: 0x" + HexDump.toHex(getInstance()) + '\n' + - " ShapeIdMax: " + field_1_shapeIdMax + '\n' + - " NumIdClusters: " + getNumIdClusters() + '\n' + - " NumShapesSaved: " + field_3_numShapesSaved + '\n' + - " DrawingsSaved: " + field_4_drawingsSaved + '\n' + - "" + field_5_string; - - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(field_1_shapeIdMax).append("\n") - .append(tab).append("\t").append("").append(getNumIdClusters()).append("\n") - .append(tab).append("\t").append("").append(field_3_numShapesSaved).append("\n") - .append(tab).append("\t").append("").append(field_4_drawingsSaved).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - /** * Gets the next available shape id * @@ -280,7 +248,9 @@ public final class EscherDggRecord extends EscherRecord { public void addCluster( int dgId, int numShapedUsed, boolean sort ) { List clusters = new ArrayList(Arrays.asList(field_5_fileIdClusters)); clusters.add(new FileIdCluster(dgId, numShapedUsed)); - if(sort) Collections.sort(clusters, MY_COMP ); + if(sort) { + Collections.sort(clusters, MY_COMP ); + } maxDgId = Math.min(maxDgId, dgId); field_5_fileIdClusters = clusters.toArray( new FileIdCluster[clusters.size()] ); } @@ -297,4 +267,25 @@ public final class EscherDggRecord extends EscherRecord { return +1; } }; + + @Override + protected Object[][] getAttributeMap() { + List fldIds = new ArrayList(); + fldIds.add("FileId Clusters"); + fldIds.add(field_5_fileIdClusters.length); + if(field_5_fileIdClusters != null) { + for (FileIdCluster fic : field_5_fileIdClusters) { + fldIds.add("Group"+fic.field_1_drawingGroupId); + fldIds.add(fic.field_2_numShapeIdsUsed); + } + } + + return new Object[][] { + { "ShapeIdMax", field_1_shapeIdMax }, + { "NumIdClusters", getNumIdClusters() }, + { "NumShapesSaved", field_3_numShapesSaved }, + { "DrawingsSaved", field_4_drawingsSaved }, + fldIds.toArray() + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherDump.java b/src/java/org/apache/poi/ddf/EscherDump.java index f35b061b3..eac46a99b 100644 --- a/src/java/org/apache/poi/ddf/EscherDump.java +++ b/src/java/org/apache/poi/ddf/EscherDump.java @@ -17,16 +17,16 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; -import org.apache.poi.util.HexRead; -import org.apache.poi.util.LittleEndian; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.util.zip.InflaterInputStream; +import org.apache.poi.util.HexDump; +import org.apache.poi.util.HexRead; +import org.apache.poi.util.LittleEndian; + /** * Used to dump the contents of escher records to a PrintStream. */ @@ -185,12 +185,13 @@ public final class EscherDump { recordName = "MsofbtUDefProp"; break; default: - if ( recordId >= (short) 0xF018 && recordId <= (short) 0xF117 ) + if ( recordId >= (short) 0xF018 && recordId <= (short) 0xF117 ) { recordName = "MsofbtBLIP"; - else if ( ( options & (short) 0x000F ) == (short) 0x000F ) + } else if ( ( options & (short) 0x000F ) == (short) 0x000F ) { recordName = "UNKNOWN container"; - else + } else { recordName = "UNKNOWN ID"; + } } StringBuilder stringBuf = new StringBuilder(); @@ -308,8 +309,9 @@ public final class EscherDump { out.print( " " + propertyId ); if ( ( n16 & (short) 0x8000 ) == 0 ) { - if ( ( n16 & (short) 0x4000 ) != 0 ) + if ( ( n16 & (short) 0x4000 ) != 0 ) { out.print( ", fBlipID" ); + } out.print( ") " ); out.print( HexDump.toHex( n32 ) ); @@ -386,8 +388,9 @@ public final class EscherDump { byte[] buf = new byte[nDumpSize]; int read = in.read( buf ); - while ( read != -1 && read < nDumpSize ) + while ( read != -1 && read < nDumpSize ) { read += in.read( buf, read, buf.length ); + } ByteArrayInputStream bin = new ByteArrayInputStream( buf ); InputStream in1 = new InflaterInputStream( bin ); @@ -402,10 +405,11 @@ public final class EscherDump { boolean isContainer = ( options & (short) 0x000F ) == (short) 0x000F; if ( isContainer && remainingBytes >= 0 ) { // Container - if ( recordBytesRemaining <= (int) remainingBytes ) + if ( recordBytesRemaining <= (int) remainingBytes ) { out.println( " completed within" ); - else + } else { out.println( " continued elsewhere" ); + } } else if ( remainingBytes >= 0 ) // -> 0x0000 ... 0x0FFF @@ -417,9 +421,9 @@ public final class EscherDump { HexDump.dump( in, out, 0, nDumpSize ); remainingBytes -= nDumpSize; } - } - else + } else { out.println( " >> OVERRUN <<" ); + } } } diff --git a/src/java/org/apache/poi/ddf/EscherMetafileBlip.java b/src/java/org/apache/poi/ddf/EscherMetafileBlip.java index ffd740cde..5fdbae6c8 100644 --- a/src/java/org/apache/poi/ddf/EscherMetafileBlip.java +++ b/src/java/org/apache/poi/ddf/EscherMetafileBlip.java @@ -17,19 +17,18 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; -import org.apache.poi.hssf.usermodel.HSSFPictureData; - import java.awt.Dimension; import java.awt.Rectangle; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.zip.InflaterInputStream; import java.util.zip.DeflaterOutputStream; +import java.util.zip.InflaterInputStream; + +import org.apache.poi.hssf.usermodel.HSSFPictureData; +import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; public final class EscherMetafileBlip extends EscherBlipRecord { private static final POILogger log = POILogFactory.getLogger(EscherMetafileBlip.class); @@ -159,7 +158,9 @@ public final class EscherMetafileBlip extends EscherBlipRecord { @Override public int getRecordSize() { int size = 8 + 50 + raw_pictureData.length; - if(remainingData != null) size += remainingData.length; + if(remainingData != null) { + size += remainingData.length; + } if((getOptions() ^ getSignature()) == 0x10){ size += field_2_UID.length; } @@ -309,6 +310,26 @@ public final class EscherMetafileBlip extends EscherBlipRecord { field_6_fCompression = compressed ? 0 : (byte)0xFE; } + /** + * Gets the filter byte - this is usually 0xFE + * + * @return the filter byte + */ + public byte getFilter() { + return field_7_fFilter; + } + + /** + * Sets the filter byte - this is usually 0xFE + * + * @param filter the filter byte + */ + public void setFilter(byte filter) { + field_7_fFilter = filter; + } + + + /** * Returns any remaining bytes * @@ -317,48 +338,7 @@ public final class EscherMetafileBlip extends EscherBlipRecord { public byte[] getRemainingData() { return remainingData; } - - // filtering is always 254 according to available docs, so no point giving it a setter method. - - @Override - public String toString() { - String extraData = "";//HexDump.toHex(field_pictureData, 32); - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex( getRecordId() ) + '\n' + - " Version: 0x" + HexDump.toHex( getVersion() ) + '\n' + - " Instance: 0x" + HexDump.toHex( getInstance() ) + '\n' + - " UID: 0x" + HexDump.toHex( field_1_UID ) + '\n' + - (field_2_UID == null ? "" : (" UID2: 0x" + HexDump.toHex( field_2_UID ) + '\n')) + - " Uncompressed Size: " + HexDump.toHex( field_2_cb ) + '\n' + - " Bounds: " + getBounds() + '\n' + - " Size in EMU: " + getSizeEMU() + '\n' + - " Compressed Size: " + HexDump.toHex( field_5_cbSave ) + '\n' + - " Compression: " + HexDump.toHex( field_6_fCompression ) + '\n' + - " Filter: " + HexDump.toHex( field_7_fFilter ) + '\n' + - " Extra Data:" + '\n' + extraData + - (remainingData == null ? null : ("\n" + - " Remaining Data: " + HexDump.toHex(remainingData, 32))); - } - - @Override - public String toXml(String tab) { - String extraData = ""; - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_1_UID ) + '\n' + - (field_2_UID == null ? "" : (" UID2: 0x" + HexDump.toHex( field_2_UID ) + '\n'))).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_2_cb )).append("\n") - .append(tab).append("\t").append("").append(getBounds()).append("\n") - .append(tab).append("\t").append("").append(getSizeEMU()).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_5_cbSave )).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_6_fCompression )).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_7_fFilter )).append("\n") - .append(tab).append("\t").append("").append(extraData).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex(remainingData, 32)).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - + /** * Return the blip signature * @@ -399,12 +379,19 @@ public final class EscherMetafileBlip extends EscherBlipRecord { setCompressed(true); } - /** - * Sets the filter byte - usually this is 0xFE - * - * @param filter the filter byte - */ - public void setFilter(byte filter) { - field_7_fFilter = filter; + @Override + protected Object[][] getAttributeMap() { + return new Object[][]{ + // record, version, instance are directly fetched + { "UID", field_1_UID, "UID2", field_2_UID }, + { "Uncompressed Size", field_2_cb }, + { "Bounds", getBounds().toString() }, + { "Size in EMU", getSizeEMU().toString() }, + { "Compressed Size", field_5_cbSave }, + { "Compression", field_6_fCompression }, + { "Filter", field_7_fFilter }, + { "Extra Data", "" }, + { "Remaining Data", remainingData } + }; } } diff --git a/src/java/org/apache/poi/ddf/EscherOptRecord.java b/src/java/org/apache/poi/ddf/EscherOptRecord.java index 94b226b3a..f941c672e 100644 --- a/src/java/org/apache/poi/ddf/EscherOptRecord.java +++ b/src/java/org/apache/poi/ddf/EscherOptRecord.java @@ -65,9 +65,10 @@ public class EscherOptRecord extends AbstractEscherOptRecord @Override public void setVersion( short value ) { - if ( value != 0x3 ) + if ( value != 0x3 ) { throw new IllegalArgumentException( RECORD_DESCRIPTION + " can have only '0x3' version" ); + } super.setVersion( value ); } diff --git a/src/java/org/apache/poi/ddf/EscherPictBlip.java b/src/java/org/apache/poi/ddf/EscherPictBlip.java index cbd5fe20a..c94f56f18 100644 --- a/src/java/org/apache/poi/ddf/EscherPictBlip.java +++ b/src/java/org/apache/poi/ddf/EscherPictBlip.java @@ -17,11 +17,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; - import java.awt.Dimension; import java.awt.Rectangle; import java.io.ByteArrayInputStream; @@ -29,6 +24,10 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.InflaterInputStream; +import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; + public final class EscherPictBlip extends EscherBlipRecord { private static final POILogger log = POILogFactory.getLogger(EscherPictBlip.class); @@ -260,39 +259,35 @@ public final class EscherPictBlip extends EscherBlipRecord { field_6_fCompression = compressed ? 0 : (byte)0xFE; } - // filtering is always 254 according to available docs, so no point giving it a setter method. - - @Override - public String toString() { - String extraData = HexDump.toHex(getPicturedata(), 32); - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex( getRecordId() ) + '\n' + - " Version: 0x" + HexDump.toHex( getVersion() ) + '\n' + - " Instance: 0x" + HexDump.toHex( getInstance() ) + '\n' + - " UID: 0x" + HexDump.toHex( field_1_UID ) + '\n' + - " Uncompressed Size: " + HexDump.toHex( field_2_cb ) + '\n' + - " Bounds: " + getBounds() + '\n' + - " Size in EMU: " + getSizeEMU() + '\n' + - " Compressed Size: " + HexDump.toHex( field_5_cbSave ) + '\n' + - " Compression: " + HexDump.toHex( field_6_fCompression ) + '\n' + - " Filter: " + HexDump.toHex( field_7_fFilter ) + '\n' + - " Extra Data:" + '\n' + extraData; + /** + * Gets the filter byte - this is usually 0xFE + * + * @return the filter byte + */ + public byte getFilter() { + return field_7_fFilter; + } + + /** + * Sets the filter byte - this is usually 0xFE + * + * @param filter the filter byte + */ + public void setFilter(byte filter) { + field_7_fFilter = filter; } @Override - public String toXml(String tab) { - String extraData = ""; - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_1_UID )).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_2_cb )).append("\n") - .append(tab).append("\t").append("").append(getBounds()).append("\n") - .append(tab).append("\t").append("").append(getSizeEMU()).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_5_cbSave )).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_6_fCompression )).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex( field_7_fFilter )).append("\n") - .append(tab).append("\t").append("").append(extraData).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); + protected Object[][] getAttributeMap() { + return new Object[][]{ + { "UID", field_1_UID }, + { "Uncompressed Size", field_2_cb }, + { "Bounds", getBounds().toString() }, + { "Size in EMU", getSizeEMU().toString() }, + { "Compressed Size", field_5_cbSave }, + { "Compression", field_6_fCompression }, + { "Filter", field_7_fFilter }, + { "Extra Data", getPicturedata() }, + }; } } diff --git a/src/java/org/apache/poi/ddf/EscherProperty.java b/src/java/org/apache/poi/ddf/EscherProperty.java index d883fe0d0..bb1355c00 100644 --- a/src/java/org/apache/poi/ddf/EscherProperty.java +++ b/src/java/org/apache/poi/ddf/EscherProperty.java @@ -107,4 +107,8 @@ public abstract class EscherProperty { * @return the length of the part */ abstract public int serializeComplexPart( byte[] data, int pos ); -} + + + @Override + abstract public String toString(); +} \ No newline at end of file diff --git a/src/java/org/apache/poi/ddf/EscherRGBProperty.java b/src/java/org/apache/poi/ddf/EscherRGBProperty.java index 38b77f4a9..96d3e5557 100644 --- a/src/java/org/apache/poi/ddf/EscherRGBProperty.java +++ b/src/java/org/apache/poi/ddf/EscherRGBProperty.java @@ -68,7 +68,7 @@ public class EscherRGBProperty StringBuilder builder = new StringBuilder(); builder.append(tab).append("<").append(getClass().getSimpleName()).append(" id=\"0x").append(HexDump.toHex(getId())) .append("\" name=\"").append(getName()).append("\" blipId=\"") - .append(isBlipId()).append("\" value=\"0x").append(HexDump.toHex(getRgbColor())).append("\"/>\n"); + .append(isBlipId()).append("\" value=\"0x").append(HexDump.toHex(getRgbColor())).append("\"/>"); return builder.toString(); } } diff --git a/src/java/org/apache/poi/ddf/EscherRecord.java b/src/java/org/apache/poi/ddf/EscherRecord.java index 65926a8c1..c3962b8b7 100644 --- a/src/java/org/apache/poi/ddf/EscherRecord.java +++ b/src/java/org/apache/poi/ddf/EscherRecord.java @@ -48,11 +48,11 @@ public abstract class EscherRecord implements Cloneable { /** * Delegates to fillFields(byte[], int, EscherRecordFactory) - * + * * @param data they bytes to serialize from * @param f the escher record factory * @return The number of bytes written. - * + * * @see #fillFields(byte[], int, org.apache.poi.ddf.EscherRecordFactory) */ protected int fillFields( byte[] data, EscherRecordFactory f ) @@ -100,7 +100,7 @@ public abstract class EscherRecord implements Cloneable { /** * Determine whether this is a container record by inspecting the option field. - * + * * @return true is this is a container field. */ public boolean isContainerRecord() { @@ -110,7 +110,7 @@ public abstract class EscherRecord implements Cloneable { /** * Note that options is an internal field. * Use {@link #setInstance(short)} ()} and {@link #setVersion(short)} ()} to set the actual fields. - * + * * @return The options field for this record. All records have one. */ @Internal @@ -122,10 +122,10 @@ public abstract class EscherRecord implements Cloneable { /** * Set the options this this record. Container records should have the * last nibble set to 0xF.

- * + * * Note that {@code options} is an internal field. * Use {@link #getInstance()} and {@link #getVersion()} to access actual fields. - * + * * @param options the record options */ @Internal @@ -198,7 +198,7 @@ public abstract class EscherRecord implements Cloneable { /** * Sets the record id for this record. - * + * * @param recordId the record id */ public void setRecordId( short recordId ) { @@ -226,9 +226,9 @@ public abstract class EscherRecord implements Cloneable { /** * Escher records may need to be clonable in the future. - * + * * @return the cloned object - * + * * @throws CloneNotSupportedException if the subclass hasn't implemented {@link Cloneable} */ @Override @@ -238,7 +238,7 @@ public abstract class EscherRecord implements Cloneable { /** * Returns the indexed child record. - * + * * @param index the index of the child within the child records * @return the indexed child record */ @@ -255,20 +255,22 @@ public abstract class EscherRecord implements Cloneable { */ public void display(PrintWriter w, int indent) { - for (int i = 0; i < indent * 4; i++) w.print(' '); + for (int i = 0; i < indent * 4; i++) { + w.print(' '); + } w.println(getRecordName()); } /** * Subclasses should return the short name for this escher record. - * + * * @return the short name for this escher record */ public abstract String getRecordName(); /** * Returns the instance part of the option record. - * + * * @return The instance part of the record */ public short getInstance() @@ -278,7 +280,7 @@ public abstract class EscherRecord implements Cloneable { /** * Sets the instance part of record - * + * * @param value instance part value */ public void setInstance( short value ) @@ -288,7 +290,7 @@ public abstract class EscherRecord implements Cloneable { /** * Returns the version part of the option record. - * + * * @return The version part of the option record */ public short getVersion() @@ -298,7 +300,7 @@ public abstract class EscherRecord implements Cloneable { /** * Sets the version part of record - * + * * @param value version part value */ public void setVersion( short value ) @@ -306,23 +308,196 @@ public abstract class EscherRecord implements Cloneable { _options = fVersion.setShortValue( _options, value ); } - /** - * @param tab - each children must be a right of his parent - * @return xml representation of this record - */ - public String toXml(String tab){ - return tab + "<" + getClass().getSimpleName() + ">\n" + - tab + "\t" + "0x" + HexDump.toHex(_recordId) + "\n" + - tab + "\t" + "" + _options + "\n" + - tab + "\n"; - } - - protected String formatXmlRecordHeader(String className, String recordId, String version, String instance){ - return "<" + className + " recordId=\"0x" + recordId + "\" version=\"0x" + - version + "\" instance=\"0x" + instance + "\" size=\"" + getRecordSize() + "\">\n"; - } - public String toXml(){ return toXml(""); } -} + + /** + * @param tab - each children must be indented right relative to its parent + * @return xml representation of this record + */ + public final String toXml(String tab){ + final String nl = System.getProperty( "line.separator" ); + String clsNm = getClass().getSimpleName(); + StringBuilder sb = new StringBuilder(1000); + sb.append(tab).append("<").append(clsNm) + .append(" recordId=\"0x").append(HexDump.toHex(getRecordId())) + .append("\" version=\"0x").append(HexDump.toHex(getVersion())) + .append("\" instance=\"0x").append(HexDump.toHex(getInstance())) + .append("\" options=\"0x").append(HexDump.toHex(getOptions())) + .append("\" recordSize=\"").append(getRecordSize()); + Object[][] attrList = getAttributeMap(); + if (attrList == null || attrList.length == 0) { + sb.append("\" />").append(nl); + } else { + sb.append("\">").append(nl); + String childTab = tab+" "; + for (Object[] attrs : attrList) { + String tagName = capitalizeAndTrim((String)attrs[0]); + boolean hasValue = false; + boolean lastChildComplex = false; + for (int i=0; i"); + } + // add names for optional attributes + String optName = capitalizeAndTrim((String)attrs[i+0]); + if (i>0) { + sb.append(nl).append(childTab).append(" <").append(optName).append(">"); + } + lastChildComplex = appendValue(sb, value, true, childTab); + if (i>0) { + sb.append(nl).append(childTab).append(" "); + } + hasValue = true; + } + if (hasValue) { + if (lastChildComplex) { + sb.append(nl).append(childTab); + } + sb.append("").append(nl); + } + } + sb.append(tab).append(""); + } + return sb.toString(); + } + + @Override + public final String toString() { + final String nl = System.getProperty( "line.separator" ); + StringBuilder sb = new StringBuilder(1000); + sb.append(getClass().getName()).append(" (").append(getRecordName()).append("):").append(nl) + .append(" RecordId: 0x").append(HexDump.toHex( getRecordId() )).append(nl) + .append(" Version: 0x").append(HexDump.toHex( getVersion() )).append(nl) + .append(" Instance: 0x").append(HexDump.toHex( getInstance() )).append(nl) + .append(" Options: 0x").append(HexDump.toHex( getOptions() )).append(nl) + .append(" Record Size: ").append( getRecordSize() ); + + Object[][] attrList = getAttributeMap(); + if (attrList != null && attrList.length > 0) { + String childTab = " "; + for (Object[] attrs : attrList) { + for (int i=0; i + * + * Null values won't be printed.

+ * + * The attributes record, version, instance, options must not be returned. + * + * @return the attribute map + * + * @since POI 3.17-beta2 + */ + @Internal + protected abstract Object[][] getAttributeMap(); + + private static String capitalizeAndTrim(final String str) { + if (str == null || str.length() == 0) { + return str; + } + + StringBuilder sb = new StringBuilder(str.length()); + boolean capitalizeNext = true; + for (char ch : str.toCharArray()) { + if (!Character.isLetterOrDigit(ch)) { + capitalizeNext = true; + continue; + } + + if (capitalizeNext) { + if (!Character.isLetter(ch)) { + sb.append('_'); + } else { + ch = Character.toTitleCase(ch); + } + capitalizeNext = false; + } + sb.append(ch); + } + + return sb.toString(); + } + + private static void escapeXML(String s, StringBuilder out) { + if (s == null || s.isEmpty()) { + return; + } + for (char c : s.toCharArray()) { + if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') { + out.append("&#"); + out.append((int) c); + out.append(';'); + } else { + out.append(c); + } + } + } +} \ No newline at end of file diff --git a/src/java/org/apache/poi/ddf/EscherSimpleProperty.java b/src/java/org/apache/poi/ddf/EscherSimpleProperty.java index 57b1d9df3..f5430e60e 100644 --- a/src/java/org/apache/poi/ddf/EscherSimpleProperty.java +++ b/src/java/org/apache/poi/ddf/EscherSimpleProperty.java @@ -17,8 +17,8 @@ package org.apache.poi.ddf; -import org.apache.poi.util.LittleEndian; import org.apache.poi.util.HexDump; +import org.apache.poi.util.LittleEndian; /** * A simple property is of fixed length and as a property number in addition @@ -93,13 +93,21 @@ public class EscherSimpleProperty extends EscherProperty @Override public boolean equals( Object o ) { - if ( this == o ) return true; - if ( !( o instanceof EscherSimpleProperty ) ) return false; + if ( this == o ) { + return true; + } + if ( !( o instanceof EscherSimpleProperty ) ) { + return false; + } final EscherSimpleProperty escherSimpleProperty = (EscherSimpleProperty) o; - if ( propertyValue != escherSimpleProperty.propertyValue ) return false; - if ( getId() != escherSimpleProperty.getId() ) return false; + if ( propertyValue != escherSimpleProperty.propertyValue ) { + return false; + } + if ( getId() != escherSimpleProperty.getId() ) { + return false; + } return true; } @@ -134,7 +142,7 @@ public class EscherSimpleProperty extends EscherProperty builder.append(tab).append("<").append(getClass().getSimpleName()).append(" id=\"0x").append(HexDump.toHex(getId())) .append("\" name=\"").append(getName()).append("\" blipId=\"") .append(isBlipId()).append("\" complex=\"").append(isComplex()).append("\" value=\"").append("0x") - .append(HexDump.toHex(propertyValue)).append("\"/>\n"); + .append(HexDump.toHex(propertyValue)).append("\"/>"); return builder.toString(); } } diff --git a/src/java/org/apache/poi/ddf/EscherSpRecord.java b/src/java/org/apache/poi/ddf/EscherSpRecord.java index 3fec761ab..b08ff4dde 100644 --- a/src/java/org/apache/poi/ddf/EscherSpRecord.java +++ b/src/java/org/apache/poi/ddf/EscherSpRecord.java @@ -101,35 +101,6 @@ public class EscherSpRecord return "Sp"; } - - /** - * @return the string representing this shape. - */ - @Override - public String toString() - { - String nl = System.getProperty("line.separator"); - - return getClass().getName() + ":" + nl + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl + - " Version: 0x" + HexDump.toHex(getVersion()) + nl + - " ShapeType: 0x" + HexDump.toHex(getShapeType()) + nl + - " ShapeId: " + field_1_shapeId + nl + - " Flags: " + decodeFlags(field_2_flags) + " (0x" + HexDump.toHex(field_2_flags) + ")" + nl; - - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("0x").append(HexDump.toHex(getShapeType())).append("\n") - .append(tab).append("\t").append("").append(field_1_shapeId).append("\n") - .append(tab).append("\t").append("").append(decodeFlags(field_2_flags) + " (0x" + HexDump.toHex(field_2_flags) + ")").append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - /** * Converts the shape flags into a more descriptive name. */ @@ -242,4 +213,13 @@ public class EscherSpRecord { setInstance( value ); } + + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "ShapeType", getShapeType() }, + { "ShapeId", field_1_shapeId }, + { "Flags", decodeFlags(field_2_flags)+" (0x"+HexDump.toHex(field_2_flags)+")" } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherSpgrRecord.java b/src/java/org/apache/poi/ddf/EscherSpgrRecord.java index a4d30b39a..199439ced 100644 --- a/src/java/org/apache/poi/ddf/EscherSpgrRecord.java +++ b/src/java/org/apache/poi/ddf/EscherSpgrRecord.java @@ -17,7 +17,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.RecordFormatException; @@ -46,7 +45,9 @@ public class EscherSpgrRecord field_3_rectX2 = LittleEndian.getInt( data, pos + size );size+=4; field_4_rectY2 = LittleEndian.getInt( data, pos + size );size+=4; bytesRemaining -= size; - if (bytesRemaining != 0) throw new RecordFormatException("Expected no remaining bytes but got " + bytesRemaining); + if (bytesRemaining != 0) { + throw new RecordFormatException("Expected no remaining bytes but got " + bytesRemaining); + } // remainingData = new byte[bytesRemaining]; // System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining ); return 8 + size + bytesRemaining; @@ -86,33 +87,6 @@ public class EscherSpgrRecord return "Spgr"; } - /** - * @return the string representation of this record. - */ - @Override - public String toString() { - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + '\n' + - " Version: 0x" + HexDump.toHex(getVersion()) + '\n' + - " Instance: 0x" + HexDump.toHex(getInstance()) + '\n' + - " RectX: " + field_1_rectX1 + '\n' + - " RectY: " + field_2_rectY1 + '\n' + - " RectWidth: " + field_3_rectX2 + '\n' + - " RectHeight: " + field_4_rectY2 + '\n'; - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(field_1_rectX1).append("\n") - .append(tab).append("\t").append("").append(field_2_rectY1).append("\n") - .append(tab).append("\t").append("").append(field_3_rectX2).append("\n") - .append(tab).append("\t").append("").append(field_4_rectY2).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - /** * The starting top-left coordinate of child records. * @@ -191,4 +165,14 @@ public class EscherSpgrRecord public void setRectY2(int rectY2) { this.field_4_rectY2 = rectY2; } + + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "RectX", field_1_rectX1 }, + { "RectY", field_2_rectY1 }, + { "RectWidth", field_3_rectX2 }, + { "RectHeight", field_4_rectY2 } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherSplitMenuColorsRecord.java b/src/java/org/apache/poi/ddf/EscherSplitMenuColorsRecord.java index 3467d0a44..25071c0ea 100644 --- a/src/java/org/apache/poi/ddf/EscherSplitMenuColorsRecord.java +++ b/src/java/org/apache/poi/ddf/EscherSplitMenuColorsRecord.java @@ -17,7 +17,6 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.RecordFormatException; @@ -46,8 +45,9 @@ public class EscherSplitMenuColorsRecord field_3_color3 = LittleEndian.getInt( data, pos + size );size+=4; field_4_color4 = LittleEndian.getInt( data, pos + size );size+=4; bytesRemaining -= size; - if (bytesRemaining != 0) + if (bytesRemaining != 0) { throw new RecordFormatException("Expecting no remaining data but got " + bytesRemaining + " byte(s)."); + } return 8 + size + bytesRemaining; } @@ -85,34 +85,6 @@ public class EscherSplitMenuColorsRecord return "SplitMenuColors"; } - /** - * @return a string representation of this record. - */ - @Override - public String toString() { - return getClass().getName() + ":" + '\n' + - " RecordId: 0x" + HexDump.toHex(RECORD_ID) + '\n' + - " Version: 0x" + HexDump.toHex(getVersion()) + '\n' + - " Instance: 0x" + HexDump.toHex(getInstance()) + '\n' + - " Color1: 0x" + HexDump.toHex(field_1_color1) + '\n' + - " Color2: 0x" + HexDump.toHex(field_2_color2) + '\n' + - " Color3: 0x" + HexDump.toHex(field_3_color3) + '\n' + - " Color4: 0x" + HexDump.toHex(field_4_color4) + '\n' + - ""; - } - - @Override - public String toXml(String tab) { - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("0x").append(HexDump.toHex(field_1_color1)).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex(field_2_color2)).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex(field_3_color3)).append("\n") - .append(tab).append("\t").append("0x").append(HexDump.toHex(field_4_color4)).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - /** * Gets the fill color * @@ -184,4 +156,14 @@ public class EscherSplitMenuColorsRecord public void setColor4( int field_4_color4 ) { this.field_4_color4 = field_4_color4; } + + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "Color1", field_1_color1 }, + { "Color2", field_2_color2 }, + { "Color3", field_3_color3 }, + { "Color4", field_4_color4 } + }; + } } diff --git a/src/java/org/apache/poi/ddf/EscherTextboxRecord.java b/src/java/org/apache/poi/ddf/EscherTextboxRecord.java index 779808000..f869d4a9e 100644 --- a/src/java/org/apache/poi/ddf/EscherTextboxRecord.java +++ b/src/java/org/apache/poi/ddf/EscherTextboxRecord.java @@ -17,7 +17,9 @@ package org.apache.poi.ddf; -import org.apache.poi.util.HexDump; +import java.util.ArrayList; +import java.util.List; + import org.apache.poi.util.LittleEndian; import org.apache.poi.util.RecordFormatException; @@ -65,8 +67,9 @@ public final class EscherTextboxRecord extends EscherRecord implements Cloneable listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this ); int size = pos - offset; - if (size != getRecordSize()) + if (size != getRecordSize()) { throw new RecordFormatException(size + " bytes written but getRecordSize() reports " + getRecordSize()); + } return size; } @@ -130,54 +133,20 @@ public final class EscherTextboxRecord extends EscherRecord implements Cloneable } @Override - public String toString() - { - String nl = System.getProperty( "line.separator" ); - - String theDumpHex = ""; - try - { - if (thedata.length != 0) - { - theDumpHex = " Extra Data:" + nl; - theDumpHex += HexDump.dump(thedata, 0, 0); - } + protected Object[][] getAttributeMap() { + int numCh = getChildRecords().size(); + List chLst = new ArrayList(numCh*2+2); + chLst.add("children"); + chLst.add(numCh); + for (EscherRecord er : getChildRecords()) { + chLst.add(er.getRecordName()); + chLst.add(er); } - catch ( Exception e ) - { - theDumpHex = "Error!!"; - } - - return getClass().getName() + ":" + nl + - " isContainer: " + isContainerRecord() + nl + - " version: 0x" + HexDump.toHex( getVersion() ) + nl + - " instance: 0x" + HexDump.toHex( getInstance() ) + nl + - " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl + - " numchildren: " + getChildRecords().size() + nl + - theDumpHex; - } - - @Override - public String toXml(String tab) { - String theDumpHex = ""; - try - { - if (thedata.length != 0) - { - theDumpHex += HexDump.dump(thedata, 0, 0); - } - } - catch ( Exception e ) - { - theDumpHex = "Error!!"; - } - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(theDumpHex).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); + + return new Object[][] { + { "isContainer", isContainerRecord() }, + chLst.toArray(), + { "Extra Data", thedata } + }; } } - - - diff --git a/src/java/org/apache/poi/ddf/UnknownEscherRecord.java b/src/java/org/apache/poi/ddf/UnknownEscherRecord.java index aca4e19b1..f70e5de4d 100644 --- a/src/java/org/apache/poi/ddf/UnknownEscherRecord.java +++ b/src/java/org/apache/poi/ddf/UnknownEscherRecord.java @@ -18,7 +18,6 @@ package org.apache.poi.ddf; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.apache.poi.util.HexDump; @@ -130,48 +129,25 @@ public final class UnknownEscherRecord extends EscherRecord implements Cloneable return "Unknown 0x" + HexDump.toHex(getRecordId()); } - @Override - public String toString() { - StringBuffer children = new StringBuffer(); - if (getChildRecords().size() > 0) { - children.append( " children: " + '\n' ); - for (EscherRecord record : _childRecords) { - children.append(record); - children.append( '\n' ); - } - } - - String theDumpHex = HexDump.toHex(thedata, 32); - - return getClass().getName() + ":" + '\n' + - " isContainer: " + isContainerRecord() + '\n' + - " version: 0x" + HexDump.toHex( getVersion() ) + '\n' + - " instance: 0x" + HexDump.toHex( getInstance() ) + '\n' + - " recordId: 0x" + HexDump.toHex( getRecordId() ) + '\n' + - " numchildren: " + getChildRecords().size() + '\n' + - theDumpHex + - children; - } - - @Override - public String toXml(String tab) { - String theDumpHex = HexDump.toHex(thedata, 32); - StringBuilder builder = new StringBuilder(); - builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance()))) - .append(tab).append("\t").append("").append(isContainerRecord()).append("\n") - .append(tab).append("\t").append("").append(HexDump.toHex(_childRecords.size())).append("\n"); - for ( Iterator iterator = _childRecords.iterator(); iterator - .hasNext(); ) - { - EscherRecord record = iterator.next(); - builder.append(record.toXml(tab+"\t")); - } - builder.append(theDumpHex).append("\n"); - builder.append(tab).append("\n"); - return builder.toString(); - } - public void addChildRecord(EscherRecord childRecord) { getChildRecords().add( childRecord ); } + + @Override + protected Object[][] getAttributeMap() { + int numCh = getChildRecords().size(); + List chLst = new ArrayList(numCh*2+2); + chLst.add("children"); + chLst.add(numCh); + for (EscherRecord er : _childRecords) { + chLst.add(er.getRecordName()); + chLst.add(er); + } + + return new Object[][] { + { "isContainer", isContainerRecord() }, + chLst.toArray(), + { "Extra Data", thedata } + }; + } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/EscherPlaceholder.java b/src/scratchpad/src/org/apache/poi/hslf/record/EscherPlaceholder.java index 4a944516b..a5e120d82 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/EscherPlaceholder.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/EscherPlaceholder.java @@ -17,8 +17,10 @@ package org.apache.poi.hslf.record; -import org.apache.poi.ddf.*; -import org.apache.poi.util.*; +import org.apache.poi.ddf.EscherRecord; +import org.apache.poi.ddf.EscherRecordFactory; +import org.apache.poi.ddf.EscherSerializationListener; +import org.apache.poi.util.LittleEndian; /** * An atom record that specifies whether a shape is a placeholder shape. @@ -33,9 +35,10 @@ public class EscherPlaceholder extends EscherRecord { private byte placementId = 0; private byte size = 0; private short unused = 0; - + public EscherPlaceholder() {} - + + @Override public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) { int bytesRemaining = readHeader( data, offset ); @@ -43,14 +46,15 @@ public class EscherPlaceholder extends EscherRecord { placementId = data[offset+12]; size = data[offset+13]; unused = LittleEndian.getShort(data, offset+14); - + assert(bytesRemaining + 8 == 16); return bytesRemaining + 8; } + @Override public int serialize(int offset, byte[] data, EscherSerializationListener listener) { listener.beforeRecordSerialize( offset, getRecordId(), this ); - + LittleEndian.putShort(data, offset, getOptions()); LittleEndian.putShort(data, offset+2, getRecordId()); LittleEndian.putInt(data, offset+4, 8); @@ -58,18 +62,28 @@ public class EscherPlaceholder extends EscherRecord { LittleEndian.putByte(data, offset+12, placementId); LittleEndian.putByte(data, offset+13, size); LittleEndian.putShort(data, offset+14, unused); - + listener.afterRecordSerialize( offset+getRecordSize(), getRecordId(), getRecordSize(), this ); return getRecordSize(); } + @Override public int getRecordSize() { return 8 + 8; } + @Override public String getRecordName() { return "ClientTextboxPlaceholder"; } - + @Override + protected Object[][] getAttributeMap() { + return new Object[][] { + { "position", position }, + { "placementId", placementId }, + { "placehoder size", size }, + { "unused", unused } + }; + } } diff --git a/src/testcases/org/apache/poi/ddf/TestEscherBSERecord.java b/src/testcases/org/apache/poi/ddf/TestEscherBSERecord.java index 6b26799f6..903ccb24b 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherBSERecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherBSERecord.java @@ -17,11 +17,18 @@ package org.apache.poi.ddf; -import junit.framework.TestCase; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.apache.poi.poifs.storage.RawDataUtil; import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public final class TestEscherBSERecord extends TestCase { +public final class TestEscherBSERecord { + @Test public void testFillFields() { String data = "01 00 00 00 24 00 00 00 05 05 01 02 03 04 " + " 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 00 00 00 " + @@ -43,17 +50,16 @@ public final class TestEscherBSERecord extends TestCase { assertEquals( 0, r.getRemainingData().length ); } - public void testSerialize() { + @Test + public void testSerialize() throws IOException { EscherBSERecord r = createRecord(); - + String exp64 = "H4sIAAAAAAAAAGNkYP+gwsDAwMrKyMTMwsrGzsHJxc3Dy8fPwMgAAkxAzAzEICkAgs9OoSwAAAA="; + byte[] expected = RawDataUtil.decompress(exp64); + byte[] data = new byte[8 + 36]; int bytesWritten = r.serialize( 0, data, new NullEscherSerializationListener() ); - assertEquals( 44, bytesWritten ); - assertEquals( "[01, 00, 00, 00, 24, 00, 00, 00, 05, 05, 01, 02, 03, 04, " + - "05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 00, 01, 00, 00, 00, " + - "00, 00, 02, 00, 00, 00, 03, 00, 00, 00, 04, 05, 06, 07]", - HexDump.toHex(data)); - + assertEquals(data.length, bytesWritten); + assertArrayEquals(expected, data); } private EscherBSERecord createRecord() { @@ -74,26 +80,31 @@ public final class TestEscherBSERecord extends TestCase { } + @Test public void testToString() { + String nl = System.getProperty("line.separator"); EscherBSERecord record = createRecord(); - String expected = "org.apache.poi.ddf.EscherBSERecord:" + '\n' + - " RecordId: 0xF007" + '\n' + - " Version: 0x0001" + '\n' + - " Instance: 0x0000" + '\n' + - " BlipTypeWin32: 5" + '\n' + - " BlipTypeMacOS: 5" + '\n' + - " SUID: [01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 00]" + '\n' + - " Tag: 1" + '\n' + - " Size: 0" + '\n' + - " Ref: 2" + '\n' + - " Offset: 3" + '\n' + - " Usage: 4" + '\n' + - " Name: 5" + '\n' + - " Unused2: 6" + '\n' + - " Unused3: 7" + '\n' + - " blipRecord: null" + '\n' + - " Extra Data:" + '\n' + - ": 0"; + String expected = + "org.apache.poi.ddf.EscherBSERecord (BSE):" + nl + + " RecordId: 0xF007" + nl + + " Version: 0x0001" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0001" + nl + + " Record Size: 44" + nl + + " BlipTypeWin32: 0x05" + nl + + " BlipTypeMacOS: 0x05" + nl + + " SUID: " + nl + + " 00: 01, 02, 03, 04, 05, 06, 07, 08, 09, 0A, 0B, 0C, 0D, 0E, 0F, 00" + nl + + " Tag: 0x0001" + nl + + " Size: 0x00000000" + nl + + " Ref: 0x00000002" + nl + + " Offset: 0x00000003" + nl + + " Usage: 0x04" + nl + + " Name: 0x05" + nl + + " Unused2: 0x06" + nl + + " Unused3: 0x07" + nl + + " Extra Data: " + nl + + " : 0"; String actual = record.toString(); assertEquals( expected, actual ); } diff --git a/src/testcases/org/apache/poi/ddf/TestEscherBlipRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherBlipRecord.java index 899af6e46..806118655 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherBlipRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherBlipRecord.java @@ -18,24 +18,21 @@ package org.apache.poi.ddf; import static org.junit.Assert.assertArrayEquals; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.apache.poi.POIDataSamples; +import org.junit.Test; /** * Test read/serialize of escher blip records - * - * @author Yegor Kozlov - */ -public final class TestEscherBlipRecord extends TestCase { +*/ +public final class TestEscherBlipRecord { private static final POIDataSamples _samples = POIDataSamples.getDDFInstance(); //test reading/serializing of a PNG blip + @Test public void testReadPNG() { //provided in bug-44886 byte[] data = _samples.readFile("Container.dat"); @@ -81,6 +78,7 @@ public final class TestEscherBlipRecord extends TestCase { } //test reading/serializing of a PICT metafile + @Test public void testReadPICT() { //provided in bug-44886 byte[] data = _samples.readFile("Container.dat"); @@ -133,6 +131,7 @@ public final class TestEscherBlipRecord extends TestCase { } //integral test: check that the read-write-read round trip is consistent + @Test public void testContainer() { byte[] data = _samples.readFile("Container.dat"); @@ -143,21 +142,10 @@ public final class TestEscherBlipRecord extends TestCase { assertArrayEquals(data, ser); } - private byte[] read(File file) { - byte[] data = new byte[(int)file.length()]; - try { - FileInputStream is = new FileInputStream(file); - is.read(data); - is.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return data; - } - /** * The test data was created from pl031405.xls attached to Bugzilla #47143 */ + @Test public void test47143() { byte[] data = _samples.readFile("47143.dat"); EscherBSERecord bse = new EscherBSERecord(); diff --git a/src/testcases/org/apache/poi/ddf/TestEscherBoolProperty.java b/src/testcases/org/apache/poi/ddf/TestEscherBoolProperty.java index f1713533a..5e65da898 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherBoolProperty.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherBoolProperty.java @@ -17,9 +17,12 @@ package org.apache.poi.ddf; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; -public final class TestEscherBoolProperty extends TestCase { +import org.junit.Test; + +public final class TestEscherBoolProperty { + @Test public void testToString() { EscherBoolProperty p = new EscherBoolProperty((short)1, 1); assertEquals("propNum: 1, RAW: 0x0001, propName: unknown, complex: false, blipId: false, value: 1 (0x00000001)", p.toString()); diff --git a/src/testcases/org/apache/poi/ddf/TestEscherChildAnchorRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherChildAnchorRecord.java index 4f8815ade..c1cc52c84 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherChildAnchorRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherChildAnchorRecord.java @@ -17,17 +17,20 @@ package org.apache.poi.ddf; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public final class TestEscherChildAnchorRecord extends TestCase { +public final class TestEscherChildAnchorRecord { + @Test public void testSerialize() { EscherChildAnchorRecord r = createRecord(); byte[] data = new byte[8 + 16]; int bytesWritten = r.serialize( 0, data, new NullEscherSerializationListener() ); - assertEquals( 24, bytesWritten ); + assertEquals( data.length, bytesWritten ); assertEquals( "[01, 00, " + "0F, F0, " + "10, 00, 00, 00, " + @@ -37,6 +40,7 @@ public final class TestEscherChildAnchorRecord extends TestCase { "04, 00, 00, 00]", HexDump.toHex( data ) ); } + @Test public void testFillFields() { String hexData = "01 00 " + "0F F0 " + @@ -58,17 +62,21 @@ public final class TestEscherChildAnchorRecord extends TestCase { assertEquals( (short) 0x0001, r.getOptions() ); } + @Test public void testToString(){ String nl = System.getProperty( "line.separator" ); - String expected = "org.apache.poi.ddf.EscherChildAnchorRecord:" + nl + - " RecordId: 0xF00F" + nl + - " Version: 0x0001" + nl + - " Instance: 0x0000" + nl + - " X1: 1" + nl + - " Y1: 2" + nl + - " X2: 3" + nl + - " Y2: 4" + nl; + String expected = + "org.apache.poi.ddf.EscherChildAnchorRecord (ChildAnchor):" + nl + + " RecordId: 0xF00F" + nl + + " Version: 0x0001" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0001" + nl + + " Record Size: 24" + nl + + " X1: 0x00000001" + nl + + " Y1: 0x00000002" + nl + + " X2: 0x00000003" + nl + + " Y2: 0x00000004"; assertEquals( expected, createRecord().toString() ); } diff --git a/src/testcases/org/apache/poi/ddf/TestEscherClientAnchorRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherClientAnchorRecord.java index a1eb171f4..e9f153699 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherClientAnchorRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherClientAnchorRecord.java @@ -17,12 +17,14 @@ package org.apache.poi.ddf; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public class TestEscherClientAnchorRecord extends TestCase -{ +public class TestEscherClientAnchorRecord { + @Test public void testSerialize() { EscherClientAnchorRecord r = createRecord(); @@ -38,6 +40,7 @@ public class TestEscherClientAnchorRecord extends TestCase "FF, DD]", HexDump.toHex( data ) ); } + @Test public void testFillFields() { String hexData = "01 00 " + "10 F0 " + @@ -65,24 +68,27 @@ public class TestEscherClientAnchorRecord extends TestCase assertEquals( (byte) 0xDD, r.getRemainingData()[1] ); } + @Test public void testToString() { String nl = System.getProperty("line.separator"); - - String expected = "org.apache.poi.ddf.EscherClientAnchorRecord:" + nl + - " RecordId: 0xF010" + nl + - " Version: 0x0001" + nl + - " Instance: 0x0000" + nl + - " Flag: 77" + nl + - " Col1: 55" + nl + - " DX1: 33" + nl + - " Row1: 88" + nl + - " DY1: 11" + nl + - " Col2: 44" + nl + - " DX2: 22" + nl + - " Row2: 99" + nl + - " DY2: 66" + nl + - " Extra Data:" + nl + - "00000000 FF DD .." + nl; + String expected = + "org.apache.poi.ddf.EscherClientAnchorRecord (ClientAnchor):" + nl + + " RecordId: 0xF010" + nl + + " Version: 0x0001" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0001" + nl + + " Record Size: 28" + nl + + " Flag: 0x004D" + nl + + " Col1: 0x0037" + nl + + " DX1: 0x0021" + nl + + " Row1: 0x0058" + nl + + " DY1: 0x000B" + nl + + " Col2: 0x002C" + nl + + " DX2: 0x0016" + nl + + " Row2: 0x0063" + nl + + " DY2: 0x0042" + nl + + " Extra Data: " + nl + + " 0: FF, DD"; assertEquals( expected, createRecord().toString() ); } diff --git a/src/testcases/org/apache/poi/ddf/TestEscherClientDataRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherClientDataRecord.java index 0282ed091..4844c0682 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherClientDataRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherClientDataRecord.java @@ -17,12 +17,14 @@ package org.apache.poi.ddf; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public class TestEscherClientDataRecord extends TestCase -{ +public class TestEscherClientDataRecord { + @Test public void testSerialize() { EscherClientDataRecord r = createRecord(); @@ -35,6 +37,7 @@ public class TestEscherClientDataRecord extends TestCase HexDump.toHex( data ) ); } + @Test public void testFillFields() { String hexData = "02 00 " + "11 F0 " + @@ -48,15 +51,19 @@ public class TestEscherClientDataRecord extends TestCase assertEquals( "[]", HexDump.toHex(r.getRemainingData()) ); } + @Test public void testToString() { String nl = System.getProperty("line.separator"); - String expected = "org.apache.poi.ddf.EscherClientDataRecord:" + nl + - " RecordId: 0xF011" + nl + - " Version: 0x0002" + nl + - " Instance: 0x0000" + nl + - " Extra Data:" + nl + - "No Data" + nl ; + String expected = + "org.apache.poi.ddf.EscherClientDataRecord (ClientData):" + nl + + " RecordId: 0xF011" + nl + + " Version: 0x0002" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0002" + nl + + " Record Size: 8" + nl + + " Extra Data: " + nl + + " : 0"; assertEquals( expected, createRecord().toString() ); } diff --git a/src/testcases/org/apache/poi/ddf/TestEscherContainerRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherContainerRecord.java index 026bc41de..dd57d837e 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherContainerRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherContainerRecord.java @@ -17,19 +17,24 @@ package org.apache.poi.ddf; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.util.List; -import junit.framework.TestCase; import org.apache.poi.POIDataSamples; import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; /** * Tests for {@link EscherContainerRecord} */ -public final class TestEscherContainerRecord extends TestCase { +public final class TestEscherContainerRecord { private static final POIDataSamples _samples = POIDataSamples.getDDFInstance(); + @Test public void testFillFields() { EscherRecordFactory f = new DefaultEscherRecordFactory(); byte[] data = HexRead.readFromString("0F 02 11 F1 00 00 00 00"); @@ -49,6 +54,7 @@ public final class TestEscherContainerRecord extends TestCase { assertEquals((short) 0xF222, c.getRecordId()); } + @Test public void testSerialize() { UnknownEscherRecord r = new UnknownEscherRecord(); r.setOptions((short) 0x123F); @@ -69,72 +75,79 @@ public final class TestEscherContainerRecord extends TestCase { } + @Test public void testToString() { EscherContainerRecord r = new EscherContainerRecord(); r.setRecordId(EscherContainerRecord.SP_CONTAINER); r.setOptions((short) 0x000F); String nl = System.getProperty("line.separator"); - assertEquals("org.apache.poi.ddf.EscherContainerRecord (SpContainer):" + nl + - " isContainer: true" + nl + - " version: 0x000F" + nl + - " instance: 0x0000" + nl + - " recordId: 0xF004" + nl + - " numchildren: 0" + nl - , r.toString()); + String expected = + "org.apache.poi.ddf.EscherContainerRecord (SpContainer):" + nl + + " RecordId: 0xF004" + nl + + " Version: 0x000F" + nl + + " Instance: 0x0000" + nl + + " Options: 0x000F" + nl + + " Record Size: 8" + nl + + " isContainer: true" + nl + + " children: 0x00000000"; + assertEquals(expected, r.toString()); EscherOptRecord r2 = new EscherOptRecord(); // don't try to shoot in foot, please -- vlsergey // r2.setOptions((short) 0x9876); r2.setRecordId(EscherOptRecord.RECORD_ID); - String expected; r.addChildRecord(r2); - expected = "org.apache.poi.ddf.EscherContainerRecord (SpContainer):" + nl + - " isContainer: true" + nl + - " version: 0x000F" + nl + - " instance: 0x0000" + nl + - " recordId: 0xF004" + nl + - " numchildren: 1" + nl + - " children: " + nl + - " Child 0:" + nl + - " org.apache.poi.ddf.EscherOptRecord:" + nl + - " isContainer: false" + nl + - " version: 0x0003" + nl + - " instance: 0x0000" + nl + - " recordId: 0xF00B" + nl + - " numchildren: 0" + nl + - " properties:" + nl + - " " + nl; - assertEquals(expected, r.toString()); + expected = + "org.apache.poi.ddf.EscherContainerRecord (SpContainer):" + nl + + " RecordId: 0xF004" + nl + + " Version: 0x000F" + nl + + " Instance: 0x0000" + nl + + " Options: 0x000F" + nl + + " Record Size: 16" + nl + + " isContainer: true" + nl + + " children: 0x00000001" + nl + + " Child 0: org.apache.poi.ddf.EscherOptRecord (Opt):" + nl + + " RecordId: 0xF00B" + nl + + " Version: 0x0003" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0003" + nl + + " Record Size: 8" + nl + + " isContainer: false" + nl + + " numchildren: 0x00000000" + nl + + " properties: 0x00000000"; + assertEquals(expected, r.toString()); r.addChildRecord(r2); - expected = "org.apache.poi.ddf.EscherContainerRecord (SpContainer):" + nl + - " isContainer: true" + nl + - " version: 0x000F" + nl + - " instance: 0x0000" + nl + - " recordId: 0xF004" + nl + - " numchildren: 2" + nl + - " children: " + nl + - " Child 0:" + nl + - " org.apache.poi.ddf.EscherOptRecord:" + nl + - " isContainer: false" + nl + - " version: 0x0003" + nl + - " instance: 0x0000" + nl + - " recordId: 0xF00B" + nl + - " numchildren: 0" + nl + - " properties:" + nl + - " " + nl + - " Child 1:" + nl + - " org.apache.poi.ddf.EscherOptRecord:" + nl + - " isContainer: false" + nl + - " version: 0x0003" + nl + - " instance: 0x0000" + nl + - " recordId: 0xF00B" + nl + - " numchildren: 0" + nl + - " properties:" + nl + - " " + nl; + expected = + "org.apache.poi.ddf.EscherContainerRecord (SpContainer):" + nl + + " RecordId: 0xF004" + nl + + " Version: 0x000F" + nl + + " Instance: 0x0000" + nl + + " Options: 0x000F" + nl + + " Record Size: 24" + nl + + " isContainer: true" + nl + + " children: 0x00000002" + nl + + " Child 0: org.apache.poi.ddf.EscherOptRecord (Opt):" + nl + + " RecordId: 0xF00B" + nl + + " Version: 0x0003" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0003" + nl + + " Record Size: 8" + nl + + " isContainer: false" + nl + + " numchildren: 0x00000000" + nl + + " properties: 0x00000000" + nl + + " Child 1: org.apache.poi.ddf.EscherOptRecord (Opt):" + nl + + " RecordId: 0xF00B" + nl + + " Version: 0x0003" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0003" + nl + + " Record Size: 8" + nl + + " isContainer: false" + nl + + " numchildren: 0x00000000" + nl + + " properties: 0x00000000"; assertEquals(expected, r.toString()); - } + } private static final class DummyEscherRecord extends EscherRecord { public DummyEscherRecord() { } @@ -146,8 +159,11 @@ public final class TestEscherContainerRecord extends TestCase { public int getRecordSize() { return 10; } @Override public String getRecordName() { return ""; } + @Override + protected Object[][] getAttributeMap() { return null; } } + @Test public void testGetRecordSize() { EscherContainerRecord r = new EscherContainerRecord(); r.addChildRecord(new DummyEscherRecord()); @@ -158,6 +174,7 @@ public final class TestEscherContainerRecord extends TestCase { * We were having problems with reading too much data on an UnknownEscherRecord, * but hopefully we now read the correct size. */ + @Test public void testBug44857() throws Exception { byte[] data = _samples.readFile("Container.dat"); @@ -169,6 +186,7 @@ public final class TestEscherContainerRecord extends TestCase { /** * Ensure {@link EscherContainerRecord} doesn't spill its guts everywhere */ + @Test public void testChildren() { EscherContainerRecord ecr = new EscherContainerRecord(); List children0 = ecr.getChildRecords(); diff --git a/src/testcases/org/apache/poi/ddf/TestEscherDgRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherDgRecord.java index 2e0fd4b10..159781d41 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherDgRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherDgRecord.java @@ -17,11 +17,14 @@ package org.apache.poi.ddf; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public final class TestEscherDgRecord extends TestCase { +public final class TestEscherDgRecord { + @Test public void testSerialize() { EscherDgRecord r = createRecord(); @@ -36,6 +39,7 @@ public final class TestEscherDgRecord extends TestCase { HexDump.toHex( data ) ); } + @Test public void testFillFields() { String hexData = "10 00 " + "08 F0 " + @@ -51,13 +55,18 @@ public final class TestEscherDgRecord extends TestCase { assertEquals( 1025, r.getLastMSOSPID() ); } + @Test public void testToString() { - String expected = "org.apache.poi.ddf.EscherDgRecord:" + '\n' + - " RecordId: 0xF008" + '\n' + - " Version: 0x0000" + '\n' + - " Instance: 0x0001" + '\n' + - " NumShapes: 2" + '\n' + - " LastMSOSPID: 1025" + '\n'; + String nl = System.getProperty("line.separator"); + String expected = + "org.apache.poi.ddf.EscherDgRecord (Dg):" + nl + + " RecordId: 0xF008" + nl + + " Version: 0x0000" + nl + + " Instance: 0x0001" + nl + + " Options: 0x0010" + nl + + " Record Size: 16" + nl + + " NumShapes: 0x00000002" + nl + + " LastMSOSPID: 0x00000401"; assertEquals( expected, createRecord().toString() ); } diff --git a/src/testcases/org/apache/poi/ddf/TestEscherDggRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherDggRecord.java index 2587a3b2d..1008c1de2 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherDggRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherDggRecord.java @@ -17,11 +17,14 @@ package org.apache.poi.ddf; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; + import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public final class TestEscherDggRecord extends TestCase { +public final class TestEscherDggRecord { + @Test public void testSerialize() { EscherDggRecord r = createRecord(); @@ -39,6 +42,7 @@ public final class TestEscherDggRecord extends TestCase { HexDump.toHex( data ) ); } + @Test public void testFillFields() { String hexData = "00 00 " + "06 F0 " + @@ -62,17 +66,22 @@ public final class TestEscherDggRecord extends TestCase { assertEquals( 0x02, r.getFileIdClusters()[0].getNumShapeIdsUsed()); } + @Test public void testToString() { - String expected = "org.apache.poi.ddf.EscherDggRecord:" + '\n' + - " RecordId: 0xF006" + '\n' + - " Version: 0x0000" + '\n' + - " Instance: 0x0000" + '\n' + - " ShapeIdMax: 1026" + '\n' + - " NumIdClusters: 2" + '\n' + - " NumShapesSaved: 2" + '\n' + - " DrawingsSaved: 1" + '\n' + - " DrawingGroupId1: 1" + '\n' + - " NumShapeIdsUsed1: 2" + '\n'; + String nl = System.getProperty("line.separator"); + String expected = + "org.apache.poi.ddf.EscherDggRecord (Dgg):" + nl + + " RecordId: 0xF006" + nl + + " Version: 0x0000" + nl + + " Instance: 0x0000" + nl + + " Options: 0x0000" + nl + + " Record Size: 32" + nl + + " ShapeIdMax: 0x00000402" + nl + + " NumIdClusters: 0x00000002" + nl + + " NumShapesSaved: 0x00000002" + nl + + " DrawingsSaved: 0x00000001" + nl + + " FileId Clusters: 0x00000001" + nl + + " Group1: 0x00000002"; assertEquals( expected, createRecord().toString() ); } @@ -90,6 +99,7 @@ public final class TestEscherDggRecord extends TestCase { return r; } + @Test public void testGetRecordSize() { EscherDggRecord r = new EscherDggRecord(); r.setFileIdClusters(new EscherDggRecord.FileIdCluster[] { new EscherDggRecord.FileIdCluster(0,0) } ); diff --git a/src/testcases/org/apache/poi/ddf/TestEscherOptRecord.java b/src/testcases/org/apache/poi/ddf/TestEscherOptRecord.java index 0b43a0edf..0359f5029 100644 --- a/src/testcases/org/apache/poi/ddf/TestEscherOptRecord.java +++ b/src/testcases/org/apache/poi/ddf/TestEscherOptRecord.java @@ -18,13 +18,18 @@ package org.apache.poi.ddf; import static org.junit.Assert.assertArrayEquals; -import junit.framework.TestCase; +import static org.junit.Assert.assertEquals; +import java.io.IOException; + +import org.apache.poi.poifs.storage.RawDataUtil; import org.apache.poi.util.HexDump; import org.apache.poi.util.HexRead; +import org.junit.Test; -public final class TestEscherOptRecord extends TestCase { +public final class TestEscherOptRecord { + @Test public void testFillFields() { checkFillFieldsSimple(); checkFillFieldsComplex(); @@ -74,6 +79,7 @@ public final class TestEscherOptRecord extends TestCase { assertEquals( prop3, r.getEscherProperty( 2 ) ); } + @Test public void testSerialize() { checkSerializeSimple(); checkSerializeComplex(); @@ -130,6 +136,7 @@ public final class TestEscherOptRecord extends TestCase { assertEquals( 26, bytesWritten ); } + @Test public void testToString() { String nl = System.getProperty("line.separator"); EscherOptRecord r = new EscherOptRecord(); @@ -138,21 +145,25 @@ public final class TestEscherOptRecord extends TestCase { r.setRecordId(EscherOptRecord.RECORD_ID); EscherProperty prop1 = new EscherBoolProperty((short)1, 1); r.addEscherProperty(prop1); - String expected = "org.apache.poi.ddf.EscherOptRecord:" + nl + - " isContainer: false" + nl + - " version: 0x0003" + nl + - " instance: 0x0001" + nl + - " recordId: 0x" + HexDump.toHex(EscherOptRecord.RECORD_ID) + nl + - " numchildren: 0" + nl + - " properties:" + nl + - " propNum: 1, RAW: 0x0001, propName: unknown, complex: false, blipId: false, value: 1 (0x00000001)" + nl; + String expected = + "org.apache.poi.ddf.EscherOptRecord (Opt):" + nl + + " RecordId: 0xF00B" + nl + + " Version: 0x0003" + nl + + " Instance: 0x0001" + nl + + " Options: 0x0013" + nl + + " Record Size: 14" + nl + + " isContainer: false" + nl + + " numchildren: 0x00000000" + nl + + " properties: 0x00000001" + nl + + " unknown: propNum: 1, RAW: 0x0001, propName: unknown, complex: false, blipId: false, value: 1 (0x00000001)"; assertEquals( expected, r.toString()); } /** - * Test serialisation of a particually complex example + * Test serialization of a particularly complex example * This test is currently broken! */ + @Test public void testComplexSerialise() { byte[] data = { 0x53, 0x01, 0x0B, 0xF0-256, 0x9C-256, 0x01, 0x00, 0x00, @@ -233,15 +244,13 @@ public final class TestEscherOptRecord extends TestCase { assertEquals(data.length, filled); assertEquals(data.length, r.getRecordSize()); - // Serialise it + // Serialize it byte[] dest = new byte[data.length]; int written = r.serialize(0, dest); // Check it serialised it back to the same data assertEquals(data.length, written); - for(int i=0; i