KickJava   Java API By Example, From Geeks To Geeks.

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


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.TableProperties;
22 import org.apache.poi.hwpf.usermodel.TableCellDescriptor;
23 import org.apache.poi.hwpf.usermodel.BorderCode;
24
25 import org.apache.poi.util.LittleEndian;
26
27 public class TableSprmUncompressor
28   extends SprmUncompressor
29 {
30   public TableSprmUncompressor()
31   {
32   }
33
34   public static TableProperties uncompressTAP(byte[] grpprl,
35                                                   int offset)
36   {
37     TableProperties newProperties = new TableProperties();
38
39     SprmIterator sprmIt = new SprmIterator(grpprl, offset);
40
41     while (sprmIt.hasNext())
42     {
43       SprmOperation sprm = (SprmOperation)sprmIt.next();
44
45       //TAPXs are actually PAPXs so we have to make sure we are only trying to
46
//uncompress the right type of sprm.
47
if (sprm.getType() == SprmOperation.TAP_TYPE)
48       {
49         unCompressTAPOperation(newProperties, sprm);
50       }
51     }
52
53     return newProperties;
54   }
55
56   /**
57    * Used to uncompress a table property. Performs an operation defined
58    * by a sprm stored in a tapx.
59    *
60    * @param newTAP The TableProperties object to perform the operation on.
61    * @param operand The operand that defines this operation.
62    * @param param The parameter for this operation.
63    * @param varParam Variable length parameter for this operation.
64    */

65   static void unCompressTAPOperation (TableProperties newTAP, SprmOperation sprm)
66   {
67     switch (sprm.getOperation())
68     {
69       case 0:
70         newTAP.setJc ((short) sprm.getOperand());
71         break;
72       case 0x01:
73       {
74         short[] rgdxaCenter = newTAP.getRgdxaCenter ();
75         short itcMac = newTAP.getItcMac ();
76         int adjust = sprm.getOperand() - (rgdxaCenter[0] + newTAP.getDxaGapHalf ());
77         for (int x = 0; x < itcMac; x++)
78         {
79           rgdxaCenter[x] += adjust;
80         }
81         break;
82       }
83       case 0x02:
84       {
85         short[] rgdxaCenter = newTAP.getRgdxaCenter ();
86         if (rgdxaCenter != null)
87         {
88           int adjust = newTAP.getDxaGapHalf () - sprm.getOperand();
89           rgdxaCenter[0] += adjust;
90         }
91         newTAP.setDxaGapHalf (sprm.getOperand());
92         break;
93       }
94       case 0x03:
95         newTAP.setFCantSplit (getFlag(sprm.getOperand()));
96         break;
97       case 0x04:
98         newTAP.setFTableHeader (getFlag (sprm.getOperand()));
99         break;
100       case 0x05:
101       {
102         byte[] buf = sprm.getGrpprl();
103         int offset = sprm.getGrpprlOffset();
104         newTAP.setBrcTop(new BorderCode(buf, offset));
105         offset += BorderCode.SIZE;
106         newTAP.setBrcLeft(new BorderCode(buf, offset));
107         offset += BorderCode.SIZE;
108         newTAP.setBrcBottom(new BorderCode(buf, offset));
109         offset += BorderCode.SIZE;
110         newTAP.setBrcRight(new BorderCode(buf, offset));
111         offset += BorderCode.SIZE;
112         newTAP.setBrcHorizontal(new BorderCode(buf, offset));
113         offset += BorderCode.SIZE;
114         newTAP.setBrcVertical(new BorderCode(buf, offset));
115         break;
116       }
117       case 0x06:
118
119         //obsolete, used in word 1.x
120
break;
121       case 0x07:
122         newTAP.setDyaRowHeight (sprm.getOperand());
123         break;
124       case 0x08:
125       {
126         byte[] grpprl = sprm.getGrpprl();
127         int offset = sprm.getGrpprlOffset();
128         short itcMac = grpprl[offset];
129         short[] rgdxaCenter = new short[itcMac + 1];
130         TableCellDescriptor[] rgtc = new TableCellDescriptor[itcMac];
131         //I use varParam[0] and newTAP._itcMac interchangably
132
newTAP.setItcMac (itcMac);
133         newTAP.setRgdxaCenter (rgdxaCenter);
134         newTAP.setRgtc (rgtc);
135
136         // get the rgdxaCenters
137
for (int x = 0; x < itcMac; x++)
138         {
139           rgdxaCenter[x] = LittleEndian.getShort (grpprl, offset + (1 + (x * 2)));
140         }
141
142         // only try to get the TC entries if they exist...
143
int endOfSprm = offset+sprm.size()-6; // -2 bytes for sprm - 2 for size short - 2 to correct offsets being 0 based
144
int startOfTCs = offset + (1 + (itcMac + 1) * 2);
145
146         boolean hasTCs = startOfTCs < endOfSprm;
147
148         for (int x = 0; x < itcMac; x++)
149         {
150           if(hasTCs) rgtc[x] = TableCellDescriptor.convertBytesToTC(grpprl,
151               offset + (1 + ( (itcMac + 1) * 2) + (x * 20)));
152           else
153             rgtc[x] = new TableCellDescriptor();
154         }
155
156         rgdxaCenter[itcMac] = LittleEndian.getShort (grpprl, offset + (1 + (itcMac * 2)));
157         break;
158       }
159       case 0x09:
160
161         /** @todo handle cell shading*/
162         break;
163       case 0x0a:
164
165         /** @todo handle word defined table styles*/
166         break;
167       case 0x20:
168 // {
169
// TableCellDescriptor[] rgtc = newTAP.getRgtc();
170
//
171
// for (int x = varParam[0]; x < varParam[1]; x++)
172
// {
173
//
174
// if ((varParam[2] & 0x08) > 0)
175
// {
176
// short[] brcRight = rgtc[x].getBrcRight ();
177
// brcRight[0] = LittleEndian.getShort (varParam, 6);
178
// brcRight[1] = LittleEndian.getShort (varParam, 8);
179
// }
180
// else if ((varParam[2] & 0x04) > 0)
181
// {
182
// short[] brcBottom = rgtc[x].getBrcBottom ();
183
// brcBottom[0] = LittleEndian.getShort (varParam, 6);
184
// brcBottom[1] = LittleEndian.getShort (varParam, 8);
185
// }
186
// else if ((varParam[2] & 0x02) > 0)
187
// {
188
// short[] brcLeft = rgtc[x].getBrcLeft ();
189
// brcLeft[0] = LittleEndian.getShort (varParam, 6);
190
// brcLeft[1] = LittleEndian.getShort (varParam, 8);
191
// }
192
// else if ((varParam[2] & 0x01) > 0)
193
// {
194
// short[] brcTop = rgtc[x].getBrcTop ();
195
// brcTop[0] = LittleEndian.getShort (varParam, 6);
196
// brcTop[1] = LittleEndian.getShort (varParam, 8);
197
// }
198
// }
199
// break;
200
// }
201
break;
202       case 0x21:
203       {
204         int param = sprm.getOperand();
205         int index = (param & 0xff000000) >> 24;
206         int count = (param & 0x00ff0000) >> 16;
207         int width = (param & 0x0000ffff);
208         int itcMac = newTAP.getItcMac();
209
210         short[] rgdxaCenter = new short[itcMac + count + 1];
211         TableCellDescriptor[] rgtc = new TableCellDescriptor[itcMac + count];
212         if (index >= itcMac)
213         {
214           index = itcMac;
215           System.arraycopy(newTAP.getRgdxaCenter(), 0, rgdxaCenter, 0,
216                            itcMac + 1);
217           System.arraycopy(newTAP.getRgtc(), 0, rgtc, 0, itcMac);
218         }
219         else
220         {
221           //copy rgdxaCenter
222
System.arraycopy(newTAP.getRgdxaCenter(), 0, rgdxaCenter, 0,
223                            index + 1);
224           System.arraycopy(newTAP.getRgdxaCenter(), index + 1, rgdxaCenter,
225                            index + count, itcMac - (index));
226           //copy rgtc
227
System.arraycopy(newTAP.getRgtc(), 0, rgtc, 0, index);
228           System.arraycopy(newTAP.getRgtc(), index, rgtc, index + count,
229                            itcMac - index);
230         }
231
232         for (int x = index; x < index + count; x++)
233         {
234           rgtc[x] = new TableCellDescriptor();
235           rgdxaCenter[x] = (short)(rgdxaCenter[x - 1] + width);
236         }
237         rgdxaCenter[index +
238           count] = (short)(rgdxaCenter[(index + count) - 1] + width);
239         break;
240       }
241       /**@todo handle table sprms from complex files*/
242       case 0x22:
243       case 0x23:
244       case 0x24:
245       case 0x25:
246       case 0x26:
247       case 0x27:
248       case 0x28:
249       case 0x29:
250       case 0x2a:
251       case 0x2b:
252       case 0x2c:
253         break;
254       default:
255         break;
256     }
257   }
258
259
260
261 }
262
Popular Tags