KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > model > StyleDescription


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
19
20 package org.apache.poi.hwpf.model;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.Arrays JavaDoc;
24
25 import org.apache.poi.hwpf.usermodel.CharacterProperties;
26 import org.apache.poi.hwpf.usermodel.ParagraphProperties;
27 import org.apache.poi.util.LittleEndian;
28 import org.apache.poi.util.BitField;
29 /**
30  * Comment me
31  *
32  * @author Ryan Ackley
33  */

34
35 public class StyleDescription implements HDFType
36 {
37
38   private final static int PARAGRAPH_STYLE = 1;
39   private final static int CHARACTER_STYLE = 2;
40
41   private int _istd;
42   private int _baseLength;
43   private short _infoShort;
44     private static BitField _sti = new BitField(0xfff);
45     private static BitField _fScratch = new BitField(0x1000);
46     private static BitField _fInvalHeight = new BitField(0x2000);
47     private static BitField _fHasUpe = new BitField(0x4000);
48     private static BitField _fMassCopy = new BitField(0x8000);
49   private short _infoShort2;
50     private static BitField _styleTypeCode = new BitField(0xf);
51     private static BitField _baseStyle = new BitField(0xfff0);
52   private short _infoShort3;
53     private static BitField _numUPX = new BitField(0xf);
54     private static BitField _nextStyle = new BitField(0xfff0);
55   private short _bchUpe;
56   private short _infoShort4;
57     private static BitField _fAutoRedef = new BitField(0x1);
58     private static BitField _fHidden = new BitField(0x2);
59
60   UPX[] _upxs;
61   String JavaDoc _name;
62   ParagraphProperties _pap;
63   CharacterProperties _chp;
64
65   public StyleDescription()
66   {
67 // _pap = new ParagraphProperties();
68
// _chp = new CharacterProperties();
69
}
70   public StyleDescription(byte[] std, int baseLength, int offset, boolean word9)
71   {
72      _baseLength = baseLength;
73      int nameStart = offset + baseLength;
74       _infoShort = LittleEndian.getShort(std, offset);
75       offset += LittleEndian.SHORT_SIZE;
76       _infoShort2 = LittleEndian.getShort(std, offset);
77       offset += LittleEndian.SHORT_SIZE;
78       _infoShort3 = LittleEndian.getShort(std, offset);
79       offset += LittleEndian.SHORT_SIZE;
80       _bchUpe = LittleEndian.getShort(std, offset);
81       offset += LittleEndian.SHORT_SIZE;
82       _infoShort4 = LittleEndian.getShort(std, offset);
83       offset += LittleEndian.SHORT_SIZE;
84
85       //first byte(s) of variable length section of std is the length of the
86
//style name and aliases string
87
int nameLength = 0;
88       int multiplier = 1;
89       if(word9)
90       {
91           nameLength = LittleEndian.getShort(std, nameStart);
92           multiplier = 2;
93           nameStart += LittleEndian.SHORT_SIZE;
94       }
95       else
96       {
97           nameLength = std[nameStart];
98       }
99
100       try
101       {
102         _name = new String JavaDoc(std, nameStart, nameLength * multiplier, "UTF-16LE");
103       }
104       catch (UnsupportedEncodingException JavaDoc ignore)
105       {
106         // ignore
107
}
108
109       //length then null terminator.
110
int grupxStart = ((nameLength + 1) * multiplier) + nameStart;
111
112       // the spec only refers to two possible upxs but it mentions
113
// that more may be added in the future
114
int varOffset = grupxStart;
115       int numUPX = _numUPX.getValue(_infoShort3);
116       _upxs = new UPX[numUPX];
117       for(int x = 0; x < numUPX; x++)
118       {
119           int upxSize = LittleEndian.getShort(std, varOffset);
120           varOffset += LittleEndian.SHORT_SIZE;
121
122           byte[] upx = new byte[upxSize];
123           System.arraycopy(std, varOffset, upx, 0, upxSize);
124           _upxs[x] = new UPX(upx);
125           varOffset += upxSize;
126
127
128           // the upx will always start on a word boundary.
129
if(upxSize % 2 == 1)
130           {
131               ++varOffset;
132           }
133
134       }
135
136
137   }
138   public int getBaseStyle()
139   {
140     return _baseStyle.getValue(_infoShort2);
141   }
142   public byte[] getCHPX()
143   {
144     switch (_styleTypeCode.getValue(_infoShort2))
145     {
146       case PARAGRAPH_STYLE:
147         if (_upxs.length > 1)
148         {
149           return _upxs[1].getUPX();
150         }
151         return null;
152       case CHARACTER_STYLE:
153         return _upxs[0].getUPX();
154       default:
155         return null;
156     }
157
158   }
159   public byte[] getPAPX()
160   {
161     switch (_styleTypeCode.getValue(_infoShort2))
162     {
163       case PARAGRAPH_STYLE:
164         return _upxs[0].getUPX();
165       default:
166         return null;
167     }
168   }
169   public ParagraphProperties getPAP()
170   {
171       return _pap;
172   }
173   public CharacterProperties getCHP()
174   {
175       return _chp;
176   }
177   void setPAP(ParagraphProperties pap)
178   {
179       _pap = pap;
180   }
181   void setCHP(CharacterProperties chp)
182   {
183       _chp = chp;
184   }
185
186   public String JavaDoc getName()
187   {
188     return _name;
189   }
190
191   public byte[] toByteArray()
192   {
193     // size equals _baseLength bytes for known variables plus 2 bytes for name
194
// length plus name length * 2 plus 2 bytes for null plus upx's preceded by
195
// length
196
int size = _baseLength + 2 + ((_name.length() + 1) * 2);
197
198     // determine the size needed for the upxs. They always fall on word
199
// boundaries.
200
size += _upxs[0].size() + 2;
201     for (int x = 1; x < _upxs.length; x++)
202     {
203       size += _upxs[x-1].size() % 2;
204       size += _upxs[x].size() + 2;
205     }
206
207
208     byte[] buf = new byte[size];
209
210     int offset = 0;
211     LittleEndian.putShort(buf, offset, _infoShort);
212     offset += LittleEndian.SHORT_SIZE;
213     LittleEndian.putShort(buf, offset, _infoShort2);
214     offset += LittleEndian.SHORT_SIZE;
215     LittleEndian.putShort(buf, offset, _infoShort3);
216     offset += LittleEndian.SHORT_SIZE;
217     LittleEndian.putShort(buf, offset, _bchUpe);
218     offset += LittleEndian.SHORT_SIZE;
219     LittleEndian.putShort(buf, offset, _infoShort4);
220     offset = _baseLength;
221
222     char[] letters = _name.toCharArray();
223     LittleEndian.putShort(buf, _baseLength, (short)letters.length);
224     offset += LittleEndian.SHORT_SIZE;
225     for (int x = 0; x < letters.length; x++)
226     {
227       LittleEndian.putShort(buf, offset, (short)letters[x]);
228       offset += LittleEndian.SHORT_SIZE;
229     }
230     // get past the null delimiter for the name.
231
offset += LittleEndian.SHORT_SIZE;
232
233     for (int x = 0; x < _upxs.length; x++)
234     {
235       short upxSize = (short)_upxs[x].size();
236       LittleEndian.putShort(buf, offset, upxSize);
237       offset += LittleEndian.SHORT_SIZE;
238       System.arraycopy(_upxs[x].getUPX(), 0, buf, offset, upxSize);
239       offset += upxSize + (upxSize % 2);
240     }
241
242     return buf;
243   }
244
245   public boolean equals(Object JavaDoc o)
246   {
247     StyleDescription sd = (StyleDescription)o;
248     if (sd._infoShort == _infoShort && sd._infoShort2 == _infoShort2 &&
249         sd._infoShort3 == _infoShort3 && sd._bchUpe == _bchUpe &&
250         sd._infoShort4 == _infoShort4 &&
251         _name.equals(sd._name))
252     {
253
254       if (!Arrays.equals(_upxs, sd._upxs))
255       {
256         return false;
257       }
258       return true;
259     }
260     return false;
261   }
262 }
263
Popular Tags