KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * A complex property differs from a simple property in that the data can not fit inside a 32 bit
29  * integer. See the specification for more detailed information regarding exactly what is
30  * stored here.
31  *
32  * @author Glen Stampoultzis
33  */

34 public class EscherComplexProperty
35         extends EscherProperty
36 {
37     byte[] complexData = new byte[0];
38
39     /**
40      * Create a complex property using the property id and a byte array containing the complex
41      * data value.
42      *
43      * @param id The id consists of the property number, a flag indicating whether this is a blip id and a flag
44      * indicating that this is a complex property.
45      * @param complexData The value of this property.
46      */

47     public EscherComplexProperty( short id, byte[] complexData )
48     {
49         super( id );
50         this.complexData = complexData;
51     }
52
53     /**
54      * Create a complex property using the property number, a flag to indicate whether this is a
55      * blip reference and the complex property data.
56      *
57      * @param propertyNumber The property number
58      * @param isBlipId Whether this is a blip id. Should be false.
59      * @param complexData The value of this complex property.
60      */

61     public EscherComplexProperty( short propertyNumber, boolean isBlipId, byte[] complexData )
62     {
63         super( propertyNumber, true, isBlipId );
64         this.complexData = complexData;
65     }
66
67     /**
68      * Serializes the simple part of this property. ie the first 6 bytes.
69      */

70     public int serializeSimplePart( byte[] data, int pos )
71     {
72         LittleEndian.putShort(data, pos, getId());
73         LittleEndian.putInt(data, pos + 2, complexData.length);
74         return 6;
75     }
76
77     /**
78      * Serializes the complex part of this property
79      *
80      * @param data The data array to serialize to
81      * @param pos The offset within data to start serializing to.
82      * @return The number of bytes serialized.
83      */

84     public int serializeComplexPart( byte[] data, int pos )
85     {
86         System.arraycopy(complexData, 0, data, pos, complexData.length);
87         return complexData.length;
88     }
89
90     /**
91      * Get the complex data value.
92      */

93     public byte[] getComplexData()
94     {
95         return complexData;
96     }
97
98     /**
99      * Determine whether this property is equal to another property.
100      *
101      * @param o The object to compare to.
102      * @return True if the objects are equal.
103      */

104     public boolean equals( Object JavaDoc o )
105     {
106         if ( this == o ) return true;
107         if ( !( o instanceof EscherComplexProperty ) ) return false;
108
109         final EscherComplexProperty escherComplexProperty = (EscherComplexProperty) o;
110
111         if ( !Arrays.equals( complexData, escherComplexProperty.complexData ) ) return false;
112
113         return true;
114     }
115
116     /**
117      * Caclulates the number of bytes required to serialize this property.
118      *
119      * @return Number of bytes
120      */

121     public int getPropertySize()
122     {
123         return 6 + complexData.length;
124     }
125
126     /**
127      * Calculates a hashcode for this property.
128      */

129     public int hashCode()
130     {
131         return getId() * 11;
132     }
133
134     /**
135      * Retrieves the string representation for this property.
136      */

137     public String JavaDoc toString()
138     {
139         String JavaDoc dataStr;
140         ByteArrayOutputStream JavaDoc b = new ByteArrayOutputStream JavaDoc();
141         try
142         {
143             HexDump.dump( this.complexData, 0, b, 0 );
144             dataStr = b.toString();
145         }
146         catch ( Exception JavaDoc e )
147         {
148             dataStr = e.toString();
149         }
150         finally
151         {
152             try
153             {
154                 b.close();
155             }
156             catch ( IOException JavaDoc e )
157             {
158                 e.printStackTrace();
159             }
160         }
161
162         return "propNum: " + getPropertyNumber()
163                 + ", propName: " + EscherProperties.getPropertyName( getPropertyNumber() )
164                 + ", complex: " + isComplex()
165                 + ", blipId: " + isBlipId()
166                 + ", data: " + System.getProperty("line.separator") + dataStr;
167     }
168
169 }
170
Popular Tags