KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > AbstractEscherHolderRecord


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

16
17
18 package org.apache.poi.hssf.record;
19
20 import org.apache.poi.ddf.DefaultEscherRecordFactory;
21 import org.apache.poi.ddf.EscherRecord;
22 import org.apache.poi.ddf.EscherRecordFactory;
23 import org.apache.poi.ddf.NullEscherSerializationListener;
24 import org.apache.poi.util.LittleEndian;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * The escher container record is used to hold escher records. It is abstract and
32  * must be subclassed for maximum benefit.
33  *
34  * @author Glen Stampoultzis (glens at apache.org)
35  * @author Michael Zalewski (zalewski at optonline.net)
36  */

37 public abstract class AbstractEscherHolderRecord
38     extends Record
39 {
40     private static final boolean DESERIALISE = System.getProperty("poi.deserialize.escher") != null;
41
42     private List JavaDoc escherRecords;
43     private byte[] rawData;
44
45
46     public AbstractEscherHolderRecord()
47     {
48         escherRecords = new ArrayList JavaDoc();
49     }
50
51     /**
52      * Constructs a Bar record and sets its fields appropriately.
53      *
54      * @param id id must be 0x1017 or an exception
55      * will be throw upon validation
56      * @param size size the size of the data area of the record
57      * @param data data of the record (should not contain sid/len)
58      */

59
60     public AbstractEscherHolderRecord(short id, short size, byte [] data)
61     {
62         super(id, size, data);
63     
64     }
65
66     /**
67      * Constructs a Bar record and sets its fields appropriately.
68      *
69      * @param id id must be 0x1017 or an exception
70      * will be throw upon validation
71      * @param size size the size of the data area of the record
72      * @param data data of the record (should not contain sid/len)
73      * @param offset of the record's data
74      */

75
76     public AbstractEscherHolderRecord(short id, short size, byte [] data, int offset)
77     {
78         super(id, size, data, offset);
79     
80     }
81
82     /**
83      * Checks the sid matches the expected side for this record
84      *
85      * @param id the expected sid.
86      */

87     protected void validateSid(short id)
88     {
89         if (id != getSid())
90         {
91             throw new RecordFormatException("Not a Bar record");
92         }
93     }
94
95     protected void fillFields(byte [] data, short size, int offset)
96     {
97         escherRecords = new ArrayList JavaDoc();
98         if (! DESERIALISE )
99         {
100             rawData = new byte[size];
101             System.arraycopy(data, offset, rawData, 0, size);
102         }
103         else
104         {
105             EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
106             int pos = offset;
107             while ( pos < offset + size )
108             {
109                 EscherRecord r = recordFactory.createRecord(data, pos);
110                 int bytesRead = r.fillFields(data, pos, recordFactory );
111                 escherRecords.add(r);
112                 pos += bytesRead;
113             }
114         }
115     }
116
117     public String JavaDoc toString()
118     {
119         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
120
121         final String JavaDoc nl = System.getProperty("line.separator");
122         buffer.append('[' + getRecordName() + ']' + nl);
123         for ( Iterator JavaDoc iterator = escherRecords.iterator(); iterator.hasNext(); )
124         {
125             EscherRecord r = (EscherRecord) iterator.next();
126             buffer.append(r.toString());
127         }
128         buffer.append("[/" + getRecordName() + ']' + nl);
129
130         return buffer.toString();
131     }
132
133     protected abstract String JavaDoc getRecordName();
134
135     public int serialize(int offset, byte[] data)
136     {
137         LittleEndian.putShort( data, 0 + offset, getSid() );
138         LittleEndian.putShort( data, 2 + offset, (short) ( getRecordSize() - 4 ) );
139         if ( escherRecords.size() == 0 && rawData != null )
140         {
141             System.arraycopy( rawData, 0, data, offset + 4, rawData.length );
142         }
143         else
144         {
145             int pos = offset + 4;
146             for ( Iterator JavaDoc iterator = escherRecords.iterator(); iterator.hasNext(); )
147             {
148                 EscherRecord r = (EscherRecord) iterator.next();
149                 pos += r.serialize( pos, data, new NullEscherSerializationListener() );
150             }
151         }
152         return getRecordSize();
153     }
154
155 // public int serialize(int offset, byte[] data)
156
// {
157
// if (escherRecords.size() == 0 && rawData != null)
158
// {
159
// System.arraycopy( rawData, 0, data, offset, rawData.length);
160
// return rawData.length;
161
// }
162
// else
163
// {
164
// collapseShapeInformation();
165
//
166
// LittleEndian.putShort(data, 0 + offset, getSid());
167
// LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
168
//
169
// int pos = offset + 4;
170
// for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
171
// {
172
// EscherRecord r = (EscherRecord) iterator.next();
173
// pos += r.serialize(pos, data, new NullEscherSerializationListener() );
174
// }
175
//
176
// return getRecordSize();
177
// }
178
// }
179

180     /**
181      * Size of record (including 4 byte header)
182      */

183     public int getRecordSize()
184     {
185         if (escherRecords.size() == 0 && rawData != null)
186         {
187             return rawData.length + 4;
188         }
189         else
190         {
191             int size = 4;
192             for ( Iterator JavaDoc iterator = escherRecords.iterator(); iterator.hasNext(); )
193             {
194                 EscherRecord r = (EscherRecord) iterator.next();
195                 size += r.getRecordSize();
196             }
197             return size;
198         }
199     }
200
201 //
202
// /**
203
// * Size of record (including 4 byte header)
204
// */
205
// public int getRecordSize()
206
// {
207
// if (escherRecords.size() == 0 && rawData != null)
208
// {
209
// return rawData.length;
210
// }
211
// else
212
// {
213
// collapseShapeInformation();
214
//
215
// int size = 4;
216
// for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
217
// {
218
// EscherRecord r = (EscherRecord) iterator.next();
219
// size += r.getRecordSize();
220
// }
221
// return size;
222
// }
223
// }
224

225     public abstract short getSid();
226
227     public Object JavaDoc clone()
228     {
229         throw new IllegalStateException JavaDoc("Not implemented yet.");
230     }
231
232     public void addEscherRecord(int index, EscherRecord element)
233     {
234         escherRecords.add( index, element );
235     }
236
237     public boolean addEscherRecord(EscherRecord element)
238     {
239         return escherRecords.add( element );
240     }
241
242     public List JavaDoc getEscherRecords()
243     {
244         return escherRecords;
245     }
246
247     public void clearEscherRecords()
248     {
249         escherRecords.clear();
250     }
251
252
253     public EscherRecord getEscherRecord(int index)
254     {
255         return (EscherRecord) escherRecords.get(index);
256     }
257
258
259 } // END OF CLASS
260

261
262
263
264
Popular Tags