1 2 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 ; 30 import java.util.Set ; 31 import java.util.Iterator ; 32 import java.util.Collections ; 33 import java.util.ArrayList ; 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 cnse) 52 { 53 throw new RuntimeException ("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 if (sprm.getType() == SprmOperation.PAP_TYPE) 64 { 65 unCompressPAPOperation(newProperties, sprm); 66 } 67 } 68 69 return newProperties; 70 } 71 72 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 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 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 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 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 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 case 0x1c: 213 214 break; 216 case 0x1d: 217 218 break; 220 case 0x1e: 221 222 break; 224 case 0x1f: 225 226 break; 228 case 0x20: 229 230 break; 232 case 0x21: 233 234 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 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 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 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 e) 336 { 337 e.printStackTrace (); 338 } 339 break; 340 case 0x40: 341 342 if (newPAP.getIstd () >= 1 && newPAP.getIstd () <= 9) 344 { 345 newPAP.setIlvl ((byte) sprm.getOperand()); 346 } 347 break; 348 case 0x41: 349 350 break; 352 case 0x43: 353 354 newPAP.setFNumRMIns ((byte) sprm.getOperand()); 356 break; 357 case 0x44: 358 359 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 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 tabMap = new HashMap (); 403 for (int x = 0; x < tabPositions.length; x++) 404 { 405 tabMap.put(new Integer (tabPositions[x]), new Byte (tabDescriptors[x])); 406 } 407 408 for (int x = 0; x < delSize; x++) 409 { 410 tabMap.remove(new Integer (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 key = new Integer (LittleEndian.getShort(grpprl, offset)); 419 Byte val = new Byte (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 list = new ArrayList (); 427 428 Iterator 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 key = ((Integer )list.get(x)); 438 tabPositions[x] = key.intValue(); 439 tabDescriptors[x] = ((Byte )tabMap.get(key)).byteValue(); 440 } 441 442 pap.setRgdxaTab(tabPositions); 443 pap.setRgtbd(tabDescriptors); 444 } 445 446 497 } 498 | Popular Tags |