KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > sprm > ParagraphSprmUncompressor


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 package org.apache.poi.hwpf.sprm;
20
21 import org.apache.poi.hwpf.usermodel.ParagraphProperties;
22 import org.apache.poi.hwpf.usermodel.BorderCode;
23 import org.apache.poi.hwpf.usermodel.DateAndTime;
24 import org.apache.poi.hwpf.usermodel.LineSpacingDescriptor;
25 import org.apache.poi.hwpf.usermodel.ShadingDescriptor;
26 import org.apache.poi.hwpf.usermodel.DropCapSpecifier;
27 import org.apache.poi.util.LittleEndian;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 public class ParagraphSprmUncompressor
36   extends SprmUncompressor
37 {
38   public ParagraphSprmUncompressor()
39   {
40   }
41
42   public static ParagraphProperties uncompressPAP(ParagraphProperties parent,
43                                                   byte[] grpprl,
44                                                   int offset)
45   {
46     ParagraphProperties newProperties = null;
47     try
48     {
49       newProperties = (ParagraphProperties) parent.clone();
50     }
51     catch (CloneNotSupportedException JavaDoc cnse)
52     {
53       throw new RuntimeException JavaDoc("There is no way this exception should happen!!");
54     }
55     SprmIterator sprmIt = new SprmIterator(grpprl, offset);
56
57     while (sprmIt.hasNext())
58     {
59       SprmOperation sprm = (SprmOperation)sprmIt.next();
60
61       // PAPXs can contain table sprms if the paragraph marks the end of a
62
// table row
63
if (sprm.getType() == SprmOperation.PAP_TYPE)
64       {
65         unCompressPAPOperation(newProperties, sprm);
66       }
67     }
68
69     return newProperties;
70   }
71
72   /**
73    * Performs an operation on a ParagraphProperties object. Used to uncompress
74    * from a papx.
75    *
76    * @param newPAP The ParagraphProperties object to perform the operation on.
77    * @param operand The operand that defines the operation.
78    * @param param The operation's parameter.
79    * @param varParam The operation's variable length parameter.
80    * @param grpprl The original papx.
81    * @param offset The current offset in the papx.
82    * @param spra A part of the sprm that defined this operation.
83    */

84   static void unCompressPAPOperation (ParagraphProperties newPAP, SprmOperation sprm)
85   {
86     switch (sprm.getOperation())
87     {
88       case 0:
89         newPAP.setIstd (sprm.getOperand());
90         break;
91       case 0x1:
92
93         // Used only for piece table grpprl's not for PAPX
94
// int istdFirst = LittleEndian.getShort (varParam, 2);
95
// int istdLast = LittleEndian.getShort (varParam, 4);
96
// if ((newPAP.getIstd () > istdFirst) || (newPAP.getIstd () <= istdLast))
97
// {
98
// permuteIstd (newPAP, varParam, opSize);
99
// }
100
break;
101       case 0x2:
102         if (newPAP.getIstd () <= 9 || newPAP.getIstd () >= 1)
103         {
104           byte paramTmp = (byte) sprm.getOperand();
105           newPAP.setIstd (newPAP.getIstd () + paramTmp);
106           newPAP.setLvl ((byte) (newPAP.getLvl () + paramTmp));
107
108           if (((paramTmp >> 7) & 0x01) == 1)
109           {
110             newPAP.setIstd (Math.max (newPAP.getIstd (), 1));
111           }
112           else
113           {
114             newPAP.setIstd (Math.min (newPAP.getIstd (), 9));
115           }
116
117         }
118         break;
119       case 0x3:
120         newPAP.setJc ((byte) sprm.getOperand());
121         break;
122       case 0x4:
123         newPAP.setFSideBySide ((byte) sprm.getOperand());
124         break;
125       case 0x5:
126         newPAP.setFKeep ((byte) sprm.getOperand());
127         break;
128       case 0x6:
129         newPAP.setFKeepFollow ((byte) sprm.getOperand());
130         break;
131       case 0x7:
132         newPAP.setFPageBreakBefore ((byte) sprm.getOperand());
133         break;
134       case 0x8:
135         newPAP.setBrcl ((byte) sprm.getOperand());
136         break;
137       case 0x9:
138         newPAP.setBrcp ((byte) sprm.getOperand());
139         break;
140       case 0xa:
141         newPAP.setIlvl ((byte) sprm.getOperand());
142         break;
143       case 0xb:
144         newPAP.setIlfo (sprm.getOperand());
145         break;
146       case 0xc:
147         newPAP.setFNoLnn ((byte) sprm.getOperand());
148         break;
149       case 0xd:
150         /**handle tabs . variable parameter. seperate processing needed*/
151         handleTabs(newPAP, sprm);
152         break;
153       case 0xe:
154         newPAP.setDxaRight (sprm.getOperand());
155         break;
156       case 0xf:
157         newPAP.setDxaLeft (sprm.getOperand());
158         break;
159       case 0x10:
160
161         // sprmPNest is only stored in grpprls linked to a piece table.
162
newPAP.setDxaLeft (newPAP.getDxaLeft () + sprm.getOperand());
163         newPAP.setDxaLeft (Math.max (0, newPAP.getDxaLeft ()));
164         break;
165       case 0x11:
166         newPAP.setDxaLeft1 (sprm.getOperand());
167         break;
168       case 0x12:
169         newPAP.setLspd(new LineSpacingDescriptor(sprm.getGrpprl(), sprm.getGrpprlOffset()));
170         break;
171       case 0x13:
172         newPAP.setDyaBefore (sprm.getOperand());
173         break;
174       case 0x14:
175         newPAP.setDyaAfter (sprm.getOperand());
176         break;
177       case 0x15:
178         // fast saved only
179
//applySprmPChgTabs (newPAP, varParam, opSize);
180
break;
181       case 0x16:
182         newPAP.setFInTable ((byte) sprm.getOperand());
183         break;
184       case 0x17:
185         newPAP.setFTtp ((byte) sprm.getOperand());
186         break;
187       case 0x18:
188         newPAP.setDxaAbs (sprm.getOperand());
189         break;
190       case 0x19:
191         newPAP.setDyaAbs (sprm.getOperand());
192         break;
193       case 0x1a:
194         newPAP.setDxaWidth (sprm.getOperand());
195         break;
196       case 0x1b:
197         byte param = (byte)sprm.getOperand();
198         /** @todo handle paragraph postioning*/
199         byte pcVert = (byte) ((param & 0x0c) >> 2);
200         byte pcHorz = (byte) (param & 0x03);
201         if (pcVert != 3)
202         {
203           newPAP.setPcVert (pcVert);
204         }
205         if (pcHorz != 3)
206         {
207           newPAP.setPcHorz (pcHorz);
208         }
209         break;
210
211         // BrcXXX1 is older Version. Brc is used
212
case 0x1c:
213
214         //newPAP.setBrcTop1((short)param);
215
break;
216       case 0x1d:
217
218         //newPAP.setBrcLeft1((short)param);
219
break;
220       case 0x1e:
221
222         //newPAP.setBrcBottom1((short)param);
223
break;
224       case 0x1f:
225
226         //newPAP.setBrcRight1((short)param);
227
break;
228       case 0x20:
229
230         //newPAP.setBrcBetween1((short)param);
231
break;
232       case 0x21:
233
234         //newPAP.setBrcBar1((byte)param);
235
break;
236       case 0x22:
237         newPAP.setDxaFromText (sprm.getOperand());
238         break;
239       case 0x23:
240         newPAP.setWr((byte)sprm.getOperand());
241         break;
242       case 0x24:
243         newPAP.setBrcTop(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
244         break;
245       case 0x25:
246         newPAP.setBrcLeft(new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
247         break;
248       case 0x26:
249         newPAP.setBrcBottom (new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
250         break;
251       case 0x27:
252         newPAP.setBrcRight (new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
253         break;
254       case 0x28:
255         newPAP.setBrcBetween (new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
256         break;
257       case 0x29:
258         newPAP.setBrcBar (new BorderCode(sprm.getGrpprl(), sprm.getGrpprlOffset()));
259         break;
260       case 0x2a:
261         newPAP.setFNoAutoHyph ((byte) sprm.getOperand());
262         break;
263       case 0x2b:
264         newPAP.setDyaHeight (sprm.getOperand());
265         break;
266       case 0x2c:
267         newPAP.setDcs (new DropCapSpecifier((short)sprm.getOperand()));
268         break;
269       case 0x2d:
270         newPAP.setShd (new ShadingDescriptor((short)sprm.getOperand()));
271         break;
272       case 0x2e:
273         newPAP.setDyaFromText (sprm.getOperand());
274         break;
275       case 0x2f:
276         newPAP.setDxaFromText (sprm.getOperand());
277         break;
278       case 0x30:
279         newPAP.setFLocked ((byte) sprm.getOperand());
280         break;
281       case 0x31:
282         newPAP.setFWidowControl ((byte) sprm.getOperand());
283         break;
284       case 0x32:
285
286         //undocumented
287
break;
288       case 0x33:
289         newPAP.setFKinsoku ((byte) sprm.getOperand());
290         break;
291       case 0x34:
292         newPAP.setFWordWrap ((byte) sprm.getOperand());
293         break;
294       case 0x35:
295         newPAP.setFOverflowPunct ((byte) sprm.getOperand());
296         break;
297       case 0x36:
298         newPAP.setFTopLinePunct ((byte) sprm.getOperand());
299         break;
300       case 0x37:
301         newPAP.setFAutoSpaceDE ((byte) sprm.getOperand());
302         break;
303       case 0x38:
304         newPAP.setFAutoSpaceDN ((byte) sprm.getOperand());
305         break;
306       case 0x39:
307         newPAP.setWAlignFont (sprm.getOperand());
308         break;
309       case 0x3a:
310         newPAP.setFontAlign ((short) sprm.getOperand());
311         break;
312       case 0x3b:
313
314         //obsolete
315
break;
316       case 0x3e:
317       {
318         byte[] buf = new byte[sprm.size() - 3];
319         System.arraycopy(buf, 0, sprm.getGrpprl(), sprm.getGrpprlOffset(),
320                          buf.length);
321         newPAP.setAnld(buf);
322         break;
323       }
324       case 0x3f:
325         //don't really need this. spec is confusing regarding this
326
//sprm
327
try
328         {
329           byte[] varParam = sprm.getGrpprl();
330           int offset = sprm.getGrpprlOffset();
331           newPAP.setFPropRMark ((int) varParam[offset]);
332           newPAP.setIbstPropRMark ((int) LittleEndian.getShort (varParam, offset + 1));
333           newPAP.setDttmPropRMark (new DateAndTime(varParam, offset + 3));
334         }
335         catch (Exception JavaDoc e)
336         {
337           e.printStackTrace ();
338         }
339         break;
340       case 0x40:
341
342         //newPAP._lvl = param;
343
if (newPAP.getIstd () >= 1 && newPAP.getIstd () <= 9)
344         {
345           newPAP.setIlvl ((byte) sprm.getOperand());
346         }
347         break;
348       case 0x41:
349
350         // undocumented
351
break;
352       case 0x43:
353
354         //pap.fNumRMIns
355
newPAP.setFNumRMIns ((byte) sprm.getOperand());
356         break;
357       case 0x44:
358
359         //undocumented
360
break;
361       case 0x45:
362         if (sprm.getSizeCode() == 6)
363         {
364           byte[] buf = new byte[sprm.size() - 3];
365           System.arraycopy(buf, 0, sprm.getGrpprl(), sprm.getGrpprlOffset(), buf.length);
366           newPAP.setNumrm (buf);
367         }
368         else
369         {
370           /**@todo handle large PAPX from data stream*/
371         }
372         break;
373
374       case 0x47:
375         newPAP.setFUsePgsuSettings ((byte) sprm.getOperand());
376         break;
377       case 0x48:
378         newPAP.setFAdjustRight ((byte) sprm.getOperand());
379         break;
380       case 0x49:
381         newPAP.setTableLevel((byte)sprm.getOperand());
382         break;
383       case 0x4b:
384         newPAP.setEmbeddedCellMark((byte)sprm.getOperand());
385         break;
386       case 0x4c:
387         newPAP.setFTtpEmbedded((byte)sprm.getOperand());
388         break;
389       default:
390         break;
391     }
392   }
393
394   private static void handleTabs(ParagraphProperties pap, SprmOperation sprm)
395   {
396     byte[] grpprl = sprm.getGrpprl();
397     int offset = sprm.getGrpprlOffset();
398     int delSize = grpprl[offset++];
399     int[] tabPositions = pap.getRgdxaTab();
400     byte[] tabDescriptors = pap.getRgtbd();
401
402     HashMap JavaDoc tabMap = new HashMap JavaDoc();
403     for (int x = 0; x < tabPositions.length; x++)
404     {
405       tabMap.put(new Integer JavaDoc(tabPositions[x]), new Byte JavaDoc(tabDescriptors[x]));
406     }
407
408     for (int x = 0; x < delSize; x++)
409     {
410       tabMap.remove(new Integer JavaDoc(LittleEndian.getShort(grpprl, offset)));
411       offset += LittleEndian.SHORT_SIZE;
412     }
413
414     int addSize = grpprl[offset++];
415     int start = offset;
416     for (int x = 0; x < addSize; x++)
417     {
418       Integer JavaDoc key = new Integer JavaDoc(LittleEndian.getShort(grpprl, offset));
419       Byte JavaDoc val = new Byte JavaDoc(grpprl[start + ((LittleEndian.SHORT_SIZE * addSize) + x)]);
420       tabMap.put(key, val);
421       offset += LittleEndian.SHORT_SIZE;
422     }
423
424     tabPositions = new int[tabMap.size()];
425     tabDescriptors = new byte[tabPositions.length];
426     ArrayList JavaDoc list = new ArrayList JavaDoc();
427
428     Iterator JavaDoc keyIT = tabMap.keySet().iterator();
429     while (keyIT.hasNext())
430     {
431       list.add(keyIT.next());
432     }
433     Collections.sort(list);
434
435     for (int x = 0; x < tabPositions.length; x++)
436     {
437       Integer JavaDoc key = ((Integer JavaDoc)list.get(x));
438       tabPositions[x] = key.intValue();
439       tabDescriptors[x] = ((Byte JavaDoc)tabMap.get(key)).byteValue();
440     }
441
442     pap.setRgdxaTab(tabPositions);
443     pap.setRgtbd(tabDescriptors);
444   }
445
446 // private static void handleTabsAgain(ParagraphProperties pap, SprmOperation sprm)
447
// {
448
// byte[] grpprl = sprm.getGrpprl();
449
// int offset = sprm.getGrpprlOffset();
450
// int delSize = grpprl[offset++];
451
// int[] tabPositions = pap.getRgdxaTab();
452
// byte[] tabDescriptors = pap.getRgtbd();
453
//
454
// HashMap tabMap = new HashMap();
455
// for (int x = 0; x < tabPositions.length; x++)
456
// {
457
// tabMap.put(new Integer(tabPositions[x]), new Byte(tabDescriptors[x]));
458
// }
459
//
460
// for (int x = 0; x < delSize; x++)
461
// {
462
// tabMap.remove(new Integer(LittleEndian.getInt(grpprl, offset)));
463
// offset += LittleEndian.INT_SIZE;;
464
// }
465
//
466
// int addSize = grpprl[offset++];
467
// for (int x = 0; x < addSize; x++)
468
// {
469
// Integer key = new Integer(LittleEndian.getInt(grpprl, offset));
470
// Byte val = new Byte(grpprl[(LittleEndian.INT_SIZE * (addSize - x)) + x]);
471
// tabMap.put(key, val);
472
// offset += LittleEndian.INT_SIZE;
473
// }
474
//
475
// tabPositions = new int[tabMap.size()];
476
// tabDescriptors = new byte[tabPositions.length];
477
// ArrayList list = new ArrayList();
478
//
479
// Iterator keyIT = tabMap.keySet().iterator();
480
// while (keyIT.hasNext())
481
// {
482
// list.add(keyIT.next());
483
// }
484
// Collections.sort(list);
485
//
486
// for (int x = 0; x < tabPositions.length; x++)
487
// {
488
// Integer key = ((Integer)list.get(x));
489
// tabPositions[x] = key.intValue();
490
// tabDescriptors[x] = ((Byte)tabMap.get(key)).byteValue();
491
// }
492
//
493
// pap.setRgdxaTab(tabPositions);
494
// pap.setRgtbd(tabDescriptors);
495
// }
496

497 }
498
Popular Tags