1 2 17 18 package org.apache.poi.ddf; 19 20 import org.apache.poi.util.LittleEndian; 21 import org.apache.poi.util.HexDump; 22 23 import java.util.*; 24 import java.io.IOException ; 25 26 33 public class EscherOptRecord 34 extends EscherRecord 35 { 36 public static final short RECORD_ID = (short) 0xF00B; 37 public static final String RECORD_DESCRIPTION = "msofbtOPT"; 38 39 private List properties = new ArrayList(); 40 41 49 public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory ) 50 { 51 int bytesRemaining = readHeader( data, offset ); 52 int pos = offset + 8; 53 54 EscherPropertyFactory f = new EscherPropertyFactory(); 55 properties = f.createProperties( data, pos, getInstance() ); 56 return bytesRemaining + 8; 57 } 58 59 69 public int serialize( int offset, byte[] data, EscherSerializationListener listener ) 70 { 71 listener.beforeRecordSerialize( offset, getRecordId(), this ); 72 73 LittleEndian.putShort( data, offset, getOptions() ); 74 LittleEndian.putShort( data, offset + 2, getRecordId() ); 75 LittleEndian.putInt( data, offset + 4, getPropertiesSize() ); 76 int pos = offset + 8; 77 for ( Iterator iterator = properties.iterator(); iterator.hasNext(); ) 78 { 79 EscherProperty escherProperty = (EscherProperty) iterator.next(); 80 pos += escherProperty.serializeSimplePart( data, pos ); 81 } 82 for ( Iterator iterator = properties.iterator(); iterator.hasNext(); ) 83 { 84 EscherProperty escherProperty = (EscherProperty) iterator.next(); 85 pos += escherProperty.serializeComplexPart( data, pos ); 86 } 87 listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this ); 88 return pos - offset; 89 } 90 91 96 public int getRecordSize() 97 { 98 return 8 + getPropertiesSize(); 99 } 100 101 104 public short getOptions() 105 { 106 setOptions( (short) ( ( properties.size() << 4 ) | 0x3 ) ); 107 return super.getOptions(); 108 } 109 110 113 public String getRecordName() 114 { 115 return "Opt"; 116 } 117 118 private int getPropertiesSize() 119 { 120 int totalSize = 0; 121 for ( Iterator iterator = properties.iterator(); iterator.hasNext(); ) 122 { 123 EscherProperty escherProperty = (EscherProperty) iterator.next(); 124 totalSize += escherProperty.getPropertySize(); 125 } 126 return totalSize; 127 } 128 129 132 public String toString() 133 { 134 String nl = System.getProperty( "line.separator" ); 135 StringBuffer propertiesBuf = new StringBuffer (); 136 for ( Iterator iterator = properties.iterator(); iterator.hasNext(); ) 137 propertiesBuf.append( " " 138 + iterator.next().toString() 139 + nl ); 140 141 return "org.apache.poi.ddf.EscherOptRecord:" + nl + 142 " isContainer: " + isContainerRecord() + nl + 143 " options: 0x" + HexDump.toHex( getOptions() ) + nl + 144 " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl + 145 " numchildren: " + getChildRecords().size() + nl + 146 " properties:" + nl + 147 propertiesBuf.toString(); 148 } 149 150 153 public List getEscherProperties() 154 { 155 return properties; 156 } 157 158 161 public EscherProperty getEscherProperty( int index ) 162 { 163 return (EscherProperty) properties.get( index ); 164 } 165 166 169 public void addEscherProperty( EscherProperty prop ) 170 { 171 properties.add( prop ); 172 } 173 174 177 public void sortProperties() 178 { 179 Collections.sort( properties, new Comparator() 180 { 181 public int compare( Object o1, Object o2 ) 182 { 183 EscherProperty p1 = (EscherProperty) o1; 184 EscherProperty p2 = (EscherProperty) o2; 185 return new Short ( p1.getPropertyNumber() ).compareTo( new Short ( p2.getPropertyNumber() ) ); 186 } 187 } ); 188 } 189 190 191 } 192 | Popular Tags |