KickJava   Java API By Example, From Geeks To Geeks.

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


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
19
20 package org.apache.poi.hssf.record;
21
22
23
24 import org.apache.poi.util.*;
25
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 /**
31  * The obj record is used to hold various graphic objects and controls.
32  *
33  * @author Glen Stampoultzis (glens at apache.org)
34  */

35 public class ObjRecord
36     extends Record
37 {
38     public final static short sid = 0x5D;
39     private List JavaDoc subrecords;
40
41     //00000000 15 00 12 00 01 00 01 00 11 60 00 00 00 00 00 0D .........`......
42
//00000010 26 01 00 00 00 00 00 00 00 00 &.........
43

44
45     public ObjRecord()
46     {
47         subrecords = new ArrayList JavaDoc(2);
48     }
49
50     /**
51      * Constructs a OBJ record and sets its fields appropriately.
52      *
53      * @param id id must be 0x5D or an exception
54      * will be throw upon validation
55      * @param size size the size of the data area of the record
56      * @param data data of the record (should not contain sid/len)
57      */

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

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

83     protected void validateSid(short id)
84     {
85         if (id != sid)
86         {
87             throw new RecordFormatException("Not an OBJ record");
88         }
89     }
90
91     protected void fillFields(byte [] data, short size, int offset)
92     {
93         subrecords = new ArrayList JavaDoc();
94         int pos = offset;
95         while (pos - offset < size)
96         {
97             short subRecordSid = LittleEndian.getShort(data, pos);
98             short subRecordSize = LittleEndian.getShort(data, pos + 2);
99             Record subRecord = SubRecord.createSubRecord(subRecordSid, subRecordSize, data, pos + 4);
100             subrecords.add(subRecord);
101             pos += subRecord.getRecordSize();
102         }
103
104     }
105
106     public String JavaDoc toString()
107     {
108         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
109
110         buffer.append("[OBJ]\n");
111         for ( Iterator JavaDoc iterator = subrecords.iterator(); iterator.hasNext(); )
112         {
113             Record record = (Record) iterator.next();
114             buffer.append("SUBRECORD: " + record.toString());
115         }
116         buffer.append("[/OBJ]\n");
117         return buffer.toString();
118     }
119
120     public int serialize(int offset, byte[] data)
121     {
122         int pos = 0;
123
124         LittleEndian.putShort(data, 0 + offset, sid);
125         LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
126
127         pos = offset + 4;
128         for ( Iterator JavaDoc iterator = subrecords.iterator(); iterator.hasNext(); )
129         {
130             Record record = (Record) iterator.next();
131             pos += record.serialize(pos, data);
132         }
133
134         return getRecordSize();
135     }
136
137     /**
138      * Size of record (excluding 4 byte header)
139      */

140     public int getRecordSize()
141     {
142         int size = 0;
143         for ( Iterator JavaDoc iterator = subrecords.iterator(); iterator.hasNext(); )
144         {
145             Record record = (Record) iterator.next();
146             size += record.getRecordSize();
147         }
148         return 4 + size;
149     }
150
151     public short getSid()
152     {
153         return sid;
154     }
155
156     public List JavaDoc getSubRecords()
157     {
158         return subrecords;
159     }
160
161     public void clearSubRecords()
162     {
163         subrecords.clear();
164     }
165
166     public void addSubRecord(int index, Object JavaDoc element)
167     {
168         subrecords.add( index, element );
169     }
170
171     public boolean addSubRecord(Object JavaDoc o)
172     {
173         return subrecords.add( o );
174     }
175
176     public Object JavaDoc clone()
177     {
178         ObjRecord rec = new ObjRecord();
179
180         for ( Iterator JavaDoc iterator = subrecords.iterator(); iterator.hasNext(); )
181             rec.addSubRecord(( (Record) iterator.next() ).clone());
182
183         return rec;
184     }
185
186 } // END OF CLASS
187

188
189
190
191
Popular Tags