KickJava   Java API By Example, From Geeks To Geeks.

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


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.hssf.record.RecordFormatException;
22
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * Generates a property given a reference into the byte array storing that property.
29  *
30  * @author Glen Stampoultzis
31  */

32 public class EscherPropertyFactory
33 {
34     /**
35      * Create new properties from a byte array.
36      *
37      * @param data The byte array containing the property
38      * @param offset The starting offset into the byte array
39      * @return The new properties
40      */

41     public List JavaDoc createProperties( byte[] data, int offset, short numProperties )
42     {
43         List JavaDoc results = new ArrayList JavaDoc();
44
45         int pos = offset;
46         int complexBytes = 0;
47 // while ( bytesRemaining >= 6 )
48
for (int i = 0; i < numProperties; i++)
49         {
50             short propId;
51             int propData;
52             propId = LittleEndian.getShort( data, pos );
53             propData = LittleEndian.getInt( data, pos + 2 );
54             short propNumber = (short) ( propId & (short) 0x3FFF );
55             boolean isComplex = ( propId & (short) 0x8000 ) != 0;
56             boolean isBlipId = ( propId & (short) 0x4000 ) != 0;
57             if ( isComplex )
58                 complexBytes = propData;
59             else
60                 complexBytes = 0;
61             byte propertyType = EscherProperties.getPropertyType( (short) propNumber );
62             if ( propertyType == EscherPropertyMetaData.TYPE_BOOLEAN )
63                 results.add( new EscherBoolProperty( propNumber, propData ) );
64             else if ( propertyType == EscherPropertyMetaData.TYPE_RGB )
65                 results.add( new EscherRGBProperty( propNumber, propData ) );
66             else if ( propertyType == EscherPropertyMetaData.TYPE_SHAPEPATH )
67                 results.add( new EscherShapePathProperty( propNumber, propData ) );
68             else
69             {
70                 if ( !isComplex )
71                     results.add( new EscherSimpleProperty( propNumber, propData ) );
72                 else
73                 {
74                     if ( propertyType == EscherPropertyMetaData.TYPE_ARRAY)
75                         results.add( new EscherArrayProperty( propId, new byte[propData]) );
76                     else
77                         results.add( new EscherComplexProperty( propId, new byte[propData]) );
78
79                 }
80             }
81             pos += 6;
82 // bytesRemaining -= 6 + complexBytes;
83
}
84
85         // Get complex data
86
for ( Iterator JavaDoc iterator = results.iterator(); iterator.hasNext(); )
87         {
88             EscherProperty p = (EscherProperty) iterator.next();
89             if (p instanceof EscherComplexProperty)
90             {
91                 if (p instanceof EscherArrayProperty)
92                 {
93                     pos += ((EscherArrayProperty)p).setArrayData(data, pos);
94                 }
95                 else
96                 {
97                     byte[] complexData = ((EscherComplexProperty)p).getComplexData();
98                     System.arraycopy(data, pos, complexData, 0, complexData.length);
99                     pos += complexData.length;
100                 }
101             }
102         }
103
104         return results;
105     }
106
107
108 }
109
Popular Tags