KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.hssf.record.RecordFormatException;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 /**
29  * Supports text boxes
30  *
31  * @author Glen Stampoultzis (glens at apache.org)
32  */

33 public class EscherTextboxRecord extends EscherRecord
34 {
35     public static final short RECORD_ID = (short)0xF00D;
36     public static final String JavaDoc RECORD_DESCRIPTION = "msofbtClientTextbox";
37
38     private static final byte[] NO_BYTES = new byte[0];
39
40     /** The data for this record not including the the 8 byte header */
41     private byte[] thedata = NO_BYTES;
42
43     public EscherTextboxRecord()
44     {
45     }
46
47     /**
48      * This method deserializes the record from a byte array.
49      *
50      * @param data The byte array containing the escher record information
51      * @param offset The starting offset into <code>data</code>.
52      * @param recordFactory May be null since this is not a container record.
53      * @return The number of bytes read from the byte array.
54      */

55     public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory )
56     {
57         int bytesRemaining = readHeader( data, offset );
58         if ( isContainerRecord() )
59         {
60             int bytesWritten = 0;
61             thedata = new byte[0];
62             offset += 8;
63             bytesWritten += 8;
64             while ( bytesRemaining > 0 )
65             {
66                 EscherRecord child = recordFactory.createRecord( data, offset );
67                 int childBytesWritten = child.fillFields( data, offset, recordFactory );
68                 bytesWritten += childBytesWritten;
69                 offset += childBytesWritten;
70                 bytesRemaining -= childBytesWritten;
71                 getChildRecords().add( child );
72             }
73             return bytesWritten;
74         }
75         else
76         {
77             thedata = new byte[bytesRemaining];
78             System.arraycopy( data, offset + 8, thedata, 0, bytesRemaining );
79             return bytesRemaining + 8;
80         }
81     }
82
83     /**
84      * Writes this record and any contained records to the supplied byte
85      * array.
86      *
87      * @return the number of bytes written.
88      */

89     public int serialize( int offset, byte[] data, EscherSerializationListener listener )
90     {
91         listener.beforeRecordSerialize( offset, getRecordId(), this );
92
93         LittleEndian.putShort(data, offset, getOptions());
94         LittleEndian.putShort(data, offset+2, getRecordId());
95         int remainingBytes = thedata.length;
96         for ( Iterator JavaDoc iterator = getChildRecords().iterator(); iterator.hasNext(); )
97         {
98             EscherRecord r = (EscherRecord) iterator.next();
99             remainingBytes += r.getRecordSize();
100         }
101         LittleEndian.putInt(data, offset+4, remainingBytes);
102         System.arraycopy(thedata, 0, data, offset+8, thedata.length);
103         int pos = offset+8+thedata.length;
104         for ( Iterator JavaDoc iterator = getChildRecords().iterator(); iterator.hasNext(); )
105         {
106             EscherRecord r = (EscherRecord) iterator.next();
107             pos += r.serialize(pos, data, listener );
108         }
109
110         listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
111         int size = pos - offset;
112         if (size != getRecordSize())
113             throw new RecordFormatException(size + " bytes written but getRecordSize() reports " + getRecordSize());
114         return size;
115     }
116
117     /**
118      * Returns any extra data associated with this record. In practice excel
119      * does not seem to put anything here.
120      */

121     public byte[] getData()
122     {
123         return thedata;
124     }
125
126     /**
127      * Returns the number of bytes that are required to serialize this record.
128      *
129      * @return Number of bytes
130      */

131     public int getRecordSize()
132     {
133         return 8 + thedata.length;
134     }
135
136     public Object JavaDoc clone()
137     {
138         // shallow clone
139
return super.clone();
140     }
141
142     /**
143      * The short name for this record
144      */

145     public String JavaDoc getRecordName()
146     {
147         return "ClientTextbox";
148     }
149
150     public String JavaDoc toString()
151     {
152         String JavaDoc nl = System.getProperty( "line.separator" );
153
154         String JavaDoc theDumpHex = "";
155         try
156         {
157             if (thedata.length != 0)
158             {
159                 theDumpHex = " Extra Data:" + nl;
160                 theDumpHex += HexDump.dump(thedata, 0, 0);
161             }
162         }
163         catch ( Exception JavaDoc e )
164         {
165             theDumpHex = "Error!!";
166         }
167
168         return getClass().getName() + ":" + nl +
169                 " isContainer: " + isContainerRecord() + nl +
170                 " options: 0x" + HexDump.toHex( getOptions() ) + nl +
171                 " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
172                 " numchildren: " + getChildRecords().size() + nl +
173                 theDumpHex;
174     }
175
176 }
177
178
179
180
Popular Tags