KickJava   Java API By Example, From Geeks To Geeks.

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


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.LittleEndian;
21 import org.apache.poi.util.HexDump;
22
23 import java.util.*;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * The opt record is used to store property values for a shape. It is the key to determining
28  * the attributes of a shape. Properties can be of two types: simple or complex. Simple types
29  * are fixed length. Complex properties are variable length.
30  *
31  * @author Glen Stampoultzis
32  */

33 public class EscherOptRecord
34         extends EscherRecord
35 {
36     public static final short RECORD_ID = (short) 0xF00B;
37     public static final String JavaDoc RECORD_DESCRIPTION = "msofbtOPT";
38
39     private List properties = new ArrayList();
40
41     /**
42      * This method deserializes the record from a byte array.
43      *
44      * @param data The byte array containing the escher record information
45      * @param offset The starting offset into <code>data</code>.
46      * @param recordFactory May be null since this is not a container record.
47      * @return The number of bytes read from the byte array.
48      */

49     public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory )
50     {
51         int bytesRemaining = readHeader( data, offset );
52         int pos = offset + 8;
53
54         EscherPropertyFactory f = new EscherPropertyFactory();
55         properties = f.createProperties( data, pos, getInstance() );
56         return bytesRemaining + 8;
57     }
58
59     /**
60      * This method serializes this escher record into a byte array.
61      *
62      * @param offset The offset into <code>data</code> to start writing the record data to.
63      * @param data The byte array to serialize to.
64      * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
65      *
66      * @return The number of bytes written.
67      * @see NullEscherSerializationListener
68      */

69     public int serialize( int offset, byte[] data, EscherSerializationListener listener )
70     {
71         listener.beforeRecordSerialize( offset, getRecordId(), this );
72
73         LittleEndian.putShort( data, offset, getOptions() );
74         LittleEndian.putShort( data, offset + 2, getRecordId() );
75         LittleEndian.putInt( data, offset + 4, getPropertiesSize() );
76         int pos = offset + 8;
77         for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
78         {
79             EscherProperty escherProperty = (EscherProperty) iterator.next();
80             pos += escherProperty.serializeSimplePart( data, pos );
81         }
82         for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
83         {
84             EscherProperty escherProperty = (EscherProperty) iterator.next();
85             pos += escherProperty.serializeComplexPart( data, pos );
86         }
87         listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
88         return pos - offset;
89     }
90
91     /**
92      * Returns the number of bytes that are required to serialize this record.
93      *
94      * @return Number of bytes
95      */

96     public int getRecordSize()
97     {
98         return 8 + getPropertiesSize();
99     }
100
101     /**
102      * Automatically recalculate the correct option
103      */

104     public short getOptions()
105     {
106         setOptions( (short) ( ( properties.size() << 4 ) | 0x3 ) );
107         return super.getOptions();
108     }
109
110     /**
111      * The short name for this record
112      */

113     public String JavaDoc getRecordName()
114     {
115         return "Opt";
116     }
117
118     private int getPropertiesSize()
119     {
120         int totalSize = 0;
121         for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
122         {
123             EscherProperty escherProperty = (EscherProperty) iterator.next();
124             totalSize += escherProperty.getPropertySize();
125         }
126         return totalSize;
127     }
128
129     /**
130      * Retrieve the string representation of this record.
131      */

132     public String JavaDoc toString()
133     {
134         String JavaDoc nl = System.getProperty( "line.separator" );
135         StringBuffer JavaDoc propertiesBuf = new StringBuffer JavaDoc();
136         for ( Iterator iterator = properties.iterator(); iterator.hasNext(); )
137             propertiesBuf.append( " "
138                     + iterator.next().toString()
139                     + nl );
140
141         return "org.apache.poi.ddf.EscherOptRecord:" + nl +
142                 " isContainer: " + isContainerRecord() + nl +
143                 " options: 0x" + HexDump.toHex( getOptions() ) + nl +
144                 " recordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
145                 " numchildren: " + getChildRecords().size() + nl +
146                 " properties:" + nl +
147                 propertiesBuf.toString();
148     }
149
150     /**
151      * The list of properties stored by this record.
152      */

153     public List getEscherProperties()
154     {
155         return properties;
156     }
157
158     /**
159      * The list of properties stored by this record.
160      */

161     public EscherProperty getEscherProperty( int index )
162     {
163         return (EscherProperty) properties.get( index );
164     }
165
166     /**
167      * Add a property to this record.
168      */

169     public void addEscherProperty( EscherProperty prop )
170     {
171         properties.add( prop );
172     }
173
174     /**
175      * Records should be sorted by property number before being stored.
176      */

177     public void sortProperties()
178     {
179         Collections.sort( properties, new Comparator()
180         {
181             public int compare( Object JavaDoc o1, Object JavaDoc o2 )
182             {
183                 EscherProperty p1 = (EscherProperty) o1;
184                 EscherProperty p2 = (EscherProperty) o2;
185                 return new Short JavaDoc( p1.getPropertyNumber() ).compareTo( new Short JavaDoc( p2.getPropertyNumber() ) );
186             }
187         } );
188     }
189
190
191 }
192
Popular Tags