1 2 17 18 package org.apache.poi.ddf; 19 20 import org.apache.poi.util.HexDump; 21 import org.apache.poi.util.LittleEndian; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.io.PrintWriter ; 27 28 36 public class EscherContainerRecord extends EscherRecord 37 { 38 public static final short DGG_CONTAINER = (short)0xF000; 39 public static final short BSTORE_CONTAINER = (short)0xF001; 40 public static final short DG_CONTAINER = (short)0xF002; 41 public static final short SPGR_CONTAINER = (short)0xF003; 42 public static final short SP_CONTAINER = (short)0xF004; 43 public static final short SOLVER_CONTAINER = (short)0xF005; 44 45 private List childRecords = new ArrayList (); 46 47 public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory ) 48 { 49 int bytesRemaining = readHeader( data, offset ); 50 int bytesWritten = 8; 51 offset += 8; 52 while ( bytesRemaining > 0 && offset < data.length ) 53 { 54 EscherRecord child = recordFactory.createRecord(data, offset); 55 int childBytesWritten = child.fillFields( data, offset, recordFactory ); 56 bytesWritten += childBytesWritten; 57 offset += childBytesWritten; 58 bytesRemaining -= childBytesWritten; 59 getChildRecords().add( child ); 60 if (offset >= data.length && bytesRemaining > 0) 61 { 62 System.out.println("WARNING: " + bytesRemaining + " bytes remaining but no space left"); 63 } 64 } 65 return bytesWritten; 66 } 67 68 public int serialize( int offset, byte[] data, EscherSerializationListener listener ) 69 { 70 listener.beforeRecordSerialize( offset, getRecordId(), this ); 71 72 LittleEndian.putShort(data, offset, getOptions()); 73 LittleEndian.putShort(data, offset+2, getRecordId()); 74 int remainingBytes = 0; 75 for ( Iterator iterator = getChildRecords().iterator(); iterator.hasNext(); ) 76 { 77 EscherRecord r = (EscherRecord) iterator.next(); 78 remainingBytes += r.getRecordSize(); 79 } 80 LittleEndian.putInt(data, offset+4, remainingBytes); 81 int pos = offset+8; 82 for ( Iterator iterator = getChildRecords().iterator(); iterator.hasNext(); ) 83 { 84 EscherRecord r = (EscherRecord) iterator.next(); 85 pos += r.serialize(pos, data, listener ); 86 } 87 88 listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this ); 89 return pos - offset; 90 } 91 92 public int getRecordSize() 93 { 94 int childRecordsSize = 0; 95 for ( Iterator iterator = getChildRecords().iterator(); iterator.hasNext(); ) 96 { 97 EscherRecord r = (EscherRecord) iterator.next(); 98 childRecordsSize += r.getRecordSize(); 99 } 100 return 8 + childRecordsSize; 101 } 102 103 public List getChildRecords() 104 { 105 return childRecords; 106 } 107 108 public void setChildRecords( List childRecords ) 109 { 110 this.childRecords = childRecords; 111 } 112 113 public String getRecordName() 114 { 115 switch ((short)getRecordId()) 116 { 117 case DGG_CONTAINER: 118 return "DggContainer"; 119 case BSTORE_CONTAINER: 120 return "BStoreContainer"; 121 case DG_CONTAINER: 122 return "DgContainer"; 123 case SPGR_CONTAINER: 124 return "SpgrContainer"; 125 case SP_CONTAINER: 126 return "SpContainer"; 127 case SOLVER_CONTAINER: 128 return "SolverContainer"; 129 default: 130 return "Container 0x" + HexDump.toHex(getRecordId()); 131 } 132 } 133 134 public void display( PrintWriter w, int indent ) 135 { 136 super.display( w, indent ); 137 for ( Iterator iterator = childRecords.iterator(); iterator.hasNext(); ) 138 { 139 EscherRecord escherRecord = (EscherRecord) iterator.next(); 140 escherRecord.display( w, indent + 1 ); 141 } 142 } 143 144 public void addChildRecord( EscherRecord record ) 145 { 146 this.childRecords.add( record ); 147 } 148 149 public String toString() 150 { 151 String nl = System.getProperty( "line.separator" ); 152 153 StringBuffer children = new StringBuffer (); 154 if ( getChildRecords().size() > 0 ) 155 { 156 children.append( " children: " + nl ); 157 for ( Iterator iterator = getChildRecords().iterator(); iterator.hasNext(); ) 158 { 159 EscherRecord record = (EscherRecord) iterator.next(); 160 children.append( record.toString() ); 161 } 163 } 164 165 return getClass().getName() + " (" + getRecordName() + "):" + nl + 166 " isContainer: " + isContainerRecord() + nl + 167 " options: 0x" + HexDump.toHex( getOptions() ) + nl + 168 " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl + 169 " numchildren: " + getChildRecords().size() + nl + 170 children.toString(); 171 172 } 173 174 public EscherSpRecord getChildById( short recordId ) 175 { 176 for ( Iterator iterator = childRecords.iterator(); iterator.hasNext(); ) 177 { 178 EscherRecord escherRecord = (EscherRecord) iterator.next(); 179 if (escherRecord.getRecordId() == recordId) 180 return (EscherSpRecord) escherRecord; 181 } 182 return null; 183 } 184 185 } 186 | Popular Tags |