KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 /**
28  * This record is used whenever a escher record is encountered that
29  * we do not explicitly support.
30  *
31  * @author Glen Stampoultzis (glens at apache.org)
32  */

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

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

87     public int serialize( int offset, byte[] data, EscherSerializationListener listener )
88     {
89         listener.beforeRecordSerialize( offset, getRecordId(), this );
90
91         LittleEndian.putShort(data, offset, getOptions());
92         LittleEndian.putShort(data, offset+2, getRecordId());
93         int remainingBytes = thedata.length;
94         for ( Iterator JavaDoc iterator = getChildRecords().iterator(); iterator.hasNext(); )
95         {
96             EscherRecord r = (EscherRecord) iterator.next();
97             remainingBytes += r.getRecordSize();
98         }
99         LittleEndian.putInt(data, offset+4, remainingBytes);
100         System.arraycopy(thedata, 0, data, offset+8, thedata.length);
101         int pos = offset+8+thedata.length;
102         for ( Iterator JavaDoc iterator = getChildRecords().iterator(); iterator.hasNext(); )
103         {
104             EscherRecord r = (EscherRecord) iterator.next();
105             pos += r.serialize(pos, data, listener );
106         }
107
108         listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
109         return pos - offset;
110     }
111
112     public byte[] getData()
113     {
114         return thedata;
115     }
116
117     /**
118      * Returns the number of bytes that are required to serialize this record.
119      *
120      * @return Number of bytes
121      */

122     public int getRecordSize()
123     {
124         return 8 + thedata.length;
125     }
126
127     public List JavaDoc getChildRecords()
128     {
129         return childRecords;
130     }
131
132     public void setChildRecords( List JavaDoc childRecords )
133     {
134         this.childRecords = childRecords;
135     }
136
137     public Object JavaDoc clone()
138     {
139         // shallow clone
140
return super.clone();
141     }
142
143     /**
144      * The short name for this record
145      */

146     public String JavaDoc getRecordName()
147     {
148         return "Unknown 0x" + HexDump.toHex(getRecordId());
149     }
150
151     public String JavaDoc toString()
152     {
153         String JavaDoc nl = System.getProperty( "line.separator" );
154
155         StringBuffer JavaDoc children = new StringBuffer JavaDoc();
156         if ( getChildRecords().size() > 0 )
157         {
158             children.append( " children: " + nl );
159             for ( Iterator JavaDoc iterator = getChildRecords().iterator(); iterator.hasNext(); )
160             {
161                 EscherRecord record = (EscherRecord) iterator.next();
162                 children.append( record.toString() );
163                 children.append( nl );
164             }
165         }
166
167         String JavaDoc theDumpHex = "";
168         try
169         {
170             if (thedata.length != 0)
171             {
172                 theDumpHex = " Extra Data:" + nl;
173                 theDumpHex += HexDump.dump(thedata, 0, 0);
174             }
175         }
176         catch ( Exception JavaDoc e )
177         {
178             theDumpHex = "Error!!";
179         }
180
181         return getClass().getName() + ":" + nl +
182                 " isContainer: " + isContainerRecord() + nl +
183                 " options: 0x" + HexDump.toHex( getOptions() ) + nl +
184                 " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
185                 " numchildren: " + getChildRecords().size() + nl +
186                 theDumpHex +
187                 children.toString();
188     }
189
190     public void addChildRecord( EscherRecord childRecord )
191     {
192         getChildRecords().add( childRecord );
193     }
194
195 }
196
197
198
199
Popular Tags