KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > ddf > EscherContainerRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

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 JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27
28 /**
29  * Escher container records store other escher records as children.
30  * The container records themselves never store any information beyond
31  * the standard header used by all escher records. This one record is
32  * used to represent many different types of records.
33  *
34  * @author Glen Stampoultzis
35  */

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 JavaDoc childRecords = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getChildRecords()
104     {
105         return childRecords;
106     }
107
108     public void setChildRecords( List JavaDoc childRecords )
109     {
110         this.childRecords = childRecords;
111     }
112
113     public String JavaDoc 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 JavaDoc w, int indent )
135     {
136         super.display( w, indent );
137         for ( Iterator JavaDoc 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 JavaDoc toString()
150     {
151         String JavaDoc nl = System.getProperty( "line.separator" );
152
153         StringBuffer JavaDoc children = new StringBuffer JavaDoc();
154         if ( getChildRecords().size() > 0 )
155         {
156             children.append( " children: " + nl );
157             for ( Iterator JavaDoc iterator = getChildRecords().iterator(); iterator.hasNext(); )
158             {
159                 EscherRecord record = (EscherRecord) iterator.next();
160                 children.append( record.toString() );
161 // children.append( nl );
162
}
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 JavaDoc 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