1 50 51 package com.lowagie.text.rtf.list; 52 53 import java.awt.Color ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.IOException ; 56 import java.io.OutputStream ; 57 import java.util.ArrayList ; 58 59 import com.lowagie.text.Chunk; 60 import com.lowagie.text.DocumentException; 61 import com.lowagie.text.Element; 62 import com.lowagie.text.Font; 63 import com.lowagie.text.List; 64 import com.lowagie.text.ListItem; 65 import com.lowagie.text.RomanList; 66 import com.lowagie.text.factories.RomanAlphabetFactory; 67 import com.lowagie.text.factories.RomanNumberFactory; 68 import com.lowagie.text.rtf.RtfBasicElement; 69 import com.lowagie.text.rtf.RtfElement; 70 import com.lowagie.text.rtf.RtfExtendedElement; 71 import com.lowagie.text.rtf.document.RtfDocument; 72 import com.lowagie.text.rtf.style.RtfFont; 73 import com.lowagie.text.rtf.style.RtfFontList; 74 import com.lowagie.text.rtf.style.RtfParagraphStyle; 75 import com.lowagie.text.rtf.text.RtfParagraph; 76 77 78 87 public class RtfList extends RtfElement implements RtfExtendedElement { 88 89 92 private static final byte[] LIST_LEVEL = "\\listlevel".getBytes(); 93 96 private static final byte[] LIST_LEVEL_TYPE = "\\levelnfc".getBytes(); 97 100 private static final byte[] LIST_LEVEL_TYPE_NEW = "\\levelnfcn".getBytes(); 101 104 private static final byte[] LIST_LEVEL_ALIGNMENT = "\\leveljc".getBytes(); 105 108 private static final byte[] LIST_LEVEL_ALIGNMENT_NEW = "\\leveljcn".getBytes(); 109 112 private static final byte[] LIST_LEVEL_START_AT = "\\levelstartat".getBytes(); 113 116 private static final byte[] LIST_LEVEL_TEXT = "\\leveltext".getBytes(); 117 120 private static final byte[] LIST_LEVEL_STYLE_NUMBERED_BEGIN = "\\\'02\\\'".getBytes(); 121 124 private static final byte[] LIST_LEVEL_STYLE_NUMBERED_END = ".;".getBytes(); 125 128 private static final byte[] LIST_LEVEL_STYLE_BULLETED_BEGIN = "\\\'01".getBytes(); 129 132 private static final byte[] LIST_LEVEL_STYLE_BULLETED_END = ";".getBytes(); 133 136 private static final byte[] LIST_LEVEL_NUMBERS_BEGIN = "\\levelnumbers".getBytes(); 137 140 private static final byte[] LIST_LEVEL_NUMBERS_NUMBERED = "\\\'01".getBytes(); 141 144 private static final byte[] LIST_LEVEL_NUMBERS_END = ";".getBytes(); 145 148 private static final byte[] LIST_LEVEL_FIRST_INDENT = "\\fi".getBytes(); 149 152 private static final byte[] LIST_LEVEL_SYMBOL_INDENT = "\\tx".getBytes(); 153 156 private static final byte[] LIST_LEVEL_NUMBER = "\\ilvl".getBytes(); 157 160 private static final byte[] TAB = "\\tab".getBytes(); 161 164 private static final byte[] LIST_TEXT = "\\listtext".getBytes(); 165 168 private static final byte[] LIST_NUMBER_END = ".".getBytes(); 169 172 private static final byte[] LIST_BULLET = "\\\'b7".getBytes(); 173 174 private static final int LIST_TYPE_BULLET = 0; 175 private static final int LIST_TYPE_NUMBERED = 1; 176 private static final int LIST_TYPE_UPPER_LETTERS = 2; 177 private static final int LIST_TYPE_LOWER_LETTERS = 3; 178 private static final int LIST_TYPE_UPPER_ROMAN = 4; 179 private static final int LIST_TYPE_LOWER_ROMAN = 5; 180 181 184 private ArrayList items; 185 188 private int listLevel = 0; 189 192 private int firstIndent = 0; 193 196 private int leftIndent = 0; 197 200 private int rightIndent = 0; 201 204 private int symbolIndent = 0; 205 208 private int listNumber = 1; 209 212 private int listType = LIST_TYPE_BULLET; 213 216 private RtfFont fontNumber; 217 220 private RtfFont fontBullet; 221 224 private int alignment = Element.ALIGN_LEFT; 225 228 private RtfList parentList = null; 229 232 private String bulletCharacter = "\u00b7"; 233 234 240 public RtfList(RtfDocument doc, List list) { 241 super(doc); 242 243 this.listNumber = document.getDocumentHeader().getListNumber(this); 244 245 this.items = new ArrayList (); 246 if(list.getSymbolIndent() > 0 && list.getIndentationLeft() > 0) { 247 this.firstIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR * -1); 248 this.leftIndent = (int) ((list.getIndentationLeft() + list.getSymbolIndent()) * RtfElement.TWIPS_FACTOR); 249 } else if(list.getSymbolIndent() > 0) { 250 this.firstIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR * -1); 251 this.leftIndent = (int) (list.getSymbolIndent() * RtfElement.TWIPS_FACTOR); 252 } else if(list.getIndentationLeft() > 0) { 253 this.firstIndent = 0; 254 this.leftIndent = (int) (list.getIndentationLeft() * RtfElement.TWIPS_FACTOR); 255 } else { 256 this.firstIndent = 0; 257 this.leftIndent = 0; 258 } 259 this.rightIndent = (int) (list.getIndentationRight() * RtfElement.TWIPS_FACTOR); 260 this.symbolIndent = (int) ((list.getSymbolIndent() + list.getIndentationLeft()) * RtfElement.TWIPS_FACTOR); 261 262 if(list instanceof RomanList) { 263 if(list.isLowercase()) { 264 this.listType = LIST_TYPE_LOWER_ROMAN; 265 } else { 266 this.listType = LIST_TYPE_UPPER_ROMAN; 267 } 268 } else if(list.isNumbered()) { 269 this.listType = LIST_TYPE_NUMBERED; 270 } else if(list.isLettered()) { 271 if(list.isLowercase()) { 272 this.listType = LIST_TYPE_LOWER_LETTERS; 273 } else { 274 this.listType = LIST_TYPE_UPPER_LETTERS; 275 } 276 } 277 278 for(int i = 0; i < list.getItems().size(); i++) { 279 try { 280 Element element = (Element) list.getItems().get(i); 281 if(element.type() == Element.CHUNK) { 282 element = new ListItem((Chunk) element); 283 } 284 if(element instanceof ListItem) { 285 this.alignment = ((ListItem) element).getAlignment(); 286 } 287 RtfBasicElement rtfElement = doc.getMapper().mapElement(element); 288 if(rtfElement instanceof RtfList) { 289 ((RtfList) rtfElement).setListNumber(listNumber); 290 ((RtfList) rtfElement).setListLevel(listLevel + 1); 291 ((RtfList) rtfElement).setParent(this); 292 } else if(rtfElement instanceof RtfListItem) { 293 ((RtfListItem) rtfElement).setParent(this); 294 ((RtfListItem) rtfElement).inheritListSettings(listNumber, listLevel + 1); 295 } 296 items.add(rtfElement); 297 } catch(DocumentException de) { 298 de.printStackTrace(); 299 } 300 } 301 302 if(this.listLevel == 0) { 303 correctIndentation(); 304 } 305 306 fontNumber = new RtfFont(document, new Font(Font.TIMES_ROMAN, 10, Font.NORMAL, new Color (0, 0, 0))); 307 if (list.getSymbol() != null && list.getSymbol().getFont() != null && !list.getSymbol().getContent().startsWith("-") && list.getSymbol().getContent().length() > 0) { 308 this.fontBullet = new RtfFont(document, list.getSymbol().getFont()); 310 this.bulletCharacter = list.getSymbol().getContent().substring(0, 1); 311 } else { 312 this.fontBullet = new RtfFont(document, new Font(Font.SYMBOL, 10, Font.NORMAL, new Color (0, 0, 0))); 313 } 314 } 315 316 320 private byte[] writeIndentations() { 321 ByteArrayOutputStream result = new ByteArrayOutputStream (); 322 try { 323 writeIndentations(result); 324 } catch(IOException ioe) { 325 ioe.printStackTrace(); 326 } 327 return result.toByteArray(); 328 } 329 330 private void writeIndentations(final OutputStream result) throws IOException 331 { 332 result.write(LIST_LEVEL_FIRST_INDENT); 333 result.write(intToByteArray(firstIndent)); 334 result.write(RtfParagraphStyle.INDENT_LEFT); 335 result.write(intToByteArray(leftIndent)); 336 result.write(RtfParagraphStyle.INDENT_RIGHT); 337 result.write(intToByteArray(rightIndent)); 338 } 339 340 346 public byte[] writeDefinition() 347 { 348 ByteArrayOutputStream result = new ByteArrayOutputStream (); 349 try { 350 writeDefinition(result); 351 } catch(IOException ioe) { 352 ioe.printStackTrace(); 353 } 354 return result.toByteArray(); 355 } 356 359 public void writeDefinition(final OutputStream result) throws IOException 360 { 361 result.write(OPEN_GROUP); 362 result.write(LIST_LEVEL); 363 result.write(LIST_LEVEL_TYPE); 364 switch(this.listType) { 365 case LIST_TYPE_BULLET : result.write(intToByteArray(23)); break; 366 case LIST_TYPE_NUMBERED : result.write(intToByteArray(0)); break; 367 case LIST_TYPE_UPPER_LETTERS : result.write(intToByteArray(3)); break; 368 case LIST_TYPE_LOWER_LETTERS : result.write(intToByteArray(4)); break; 369 case LIST_TYPE_UPPER_ROMAN : result.write(intToByteArray(1)); break; 370 case LIST_TYPE_LOWER_ROMAN : result.write(intToByteArray(2)); break; 371 } 372 result.write(LIST_LEVEL_TYPE_NEW); 373 switch(this.listType) { 374 case LIST_TYPE_BULLET : result.write(intToByteArray(23)); break; 375 case LIST_TYPE_NUMBERED : result.write(intToByteArray(0)); break; 376 case LIST_TYPE_UPPER_LETTERS : result.write(intToByteArray(3)); break; 377 case LIST_TYPE_LOWER_LETTERS : result.write(intToByteArray(4)); break; 378 case LIST_TYPE_UPPER_ROMAN : result.write(intToByteArray(1)); break; 379 case LIST_TYPE_LOWER_ROMAN : result.write(intToByteArray(2)); break; 380 } 381 result.write(LIST_LEVEL_ALIGNMENT); 382 result.write(intToByteArray(0)); 383 result.write(LIST_LEVEL_ALIGNMENT_NEW); 384 result.write(intToByteArray(0)); 385 result.write(LIST_LEVEL_START_AT); 386 result.write(intToByteArray(1)); 387 result.write(OPEN_GROUP); 388 result.write(LIST_LEVEL_TEXT); 389 if(this.listType != LIST_TYPE_BULLET) { 390 result.write(LIST_LEVEL_STYLE_NUMBERED_BEGIN); 391 if(listLevel < 10) { 392 result.write(intToByteArray(0)); 393 } 394 result.write(intToByteArray(listLevel)); 395 result.write(LIST_LEVEL_STYLE_NUMBERED_END); 396 } else { 397 result.write(LIST_LEVEL_STYLE_BULLETED_BEGIN); 398 this.document.filterSpecialChar(result, this.bulletCharacter, false, false); 399 result.write(LIST_LEVEL_STYLE_BULLETED_END); 400 } 401 result.write(CLOSE_GROUP); 402 result.write(OPEN_GROUP); 403 result.write(LIST_LEVEL_NUMBERS_BEGIN); 404 if(this.listType != LIST_TYPE_BULLET) { 405 result.write(LIST_LEVEL_NUMBERS_NUMBERED); 406 } 407 result.write(LIST_LEVEL_NUMBERS_END); 408 result.write(CLOSE_GROUP); 409 result.write(RtfFontList.FONT_NUMBER); 410 if(this.listType != LIST_TYPE_BULLET) { 411 result.write(intToByteArray(fontNumber.getFontNumber())); 412 } else { 413 result.write(intToByteArray(fontBullet.getFontNumber())); 414 } 415 writeIndentations(result); 417 result.write(LIST_LEVEL_SYMBOL_INDENT); 418 result.write(intToByteArray(this.leftIndent)); 419 result.write(CLOSE_GROUP); 420 result.write("\n".getBytes()); 421 for(int i = 0; i < items.size(); i++) { 422 RtfElement rtfElement = (RtfElement) items.get(i); 423 if(rtfElement instanceof RtfList) { 424 RtfList rl = (RtfList)rtfElement; 425 rl.writeDefinition(result); 427 break; 428 } else if(rtfElement instanceof RtfListItem) { 429 RtfListItem rli = (RtfListItem) rtfElement; 430 if(rli.writeDefinition(result)) break; 436 } 437 } 438 } 439 440 441 446 protected byte[] writeListBeginning() { 447 ByteArrayOutputStream result = new ByteArrayOutputStream (); 448 try { 449 result.write(RtfParagraph.PARAGRAPH_DEFAULTS); 450 if(this.inTable) { 451 result.write(RtfParagraph.IN_TABLE); 452 } 453 switch (this.alignment) { 454 case Element.ALIGN_LEFT: 455 result.write(RtfParagraphStyle.ALIGN_LEFT); 456 break; 457 case Element.ALIGN_RIGHT: 458 result.write(RtfParagraphStyle.ALIGN_RIGHT); 459 break; 460 case Element.ALIGN_CENTER: 461 result.write(RtfParagraphStyle.ALIGN_CENTER); 462 break; 463 case Element.ALIGN_JUSTIFIED: 464 case Element.ALIGN_JUSTIFIED_ALL: 465 result.write(RtfParagraphStyle.ALIGN_JUSTIFY); 466 break; 467 } 468 writeIndentations(result); 470 result.write(RtfFont.FONT_SIZE); 471 result.write(intToByteArray(fontNumber.getFontSize() * 2)); 472 if(this.symbolIndent > 0) { 473 result.write("\\tx".getBytes()); 474 result.write(intToByteArray(this.leftIndent)); 475 } 476 } catch(IOException ioe) { 477 ioe.printStackTrace(); 478 } 479 return result.toByteArray(); 480 } 481 482 487 protected byte[] writeListNumbers() { 488 ByteArrayOutputStream result = new ByteArrayOutputStream (); 489 try { 490 result.write(RtfListTable.LIST_NUMBER); 491 result.write(intToByteArray(listNumber)); 492 if(listLevel > 0) { 493 result.write(LIST_LEVEL_NUMBER); 494 result.write(intToByteArray(listLevel)); 495 } 496 } catch(IOException ioe) { 497 ioe.printStackTrace(); 498 } 499 return result.toByteArray(); 500 } 501 502 508 public byte[] write() 509 { 510 ByteArrayOutputStream result = new ByteArrayOutputStream (); 511 try { 512 writeContent(result); 513 } catch(IOException ioe) { 514 ioe.printStackTrace(); 515 } 516 return result.toByteArray(); 517 } 518 519 520 523 public void writeContent(final OutputStream result) throws IOException 524 { 525 result.write(OPEN_GROUP); 526 int itemNr = 0; 527 for(int i = 0; i < items.size(); i++) { 528 RtfElement rtfElement = (RtfElement) items.get(i); 529 if(rtfElement instanceof RtfListItem) { 530 itemNr++; 531 result.write(writeListBeginning()); 532 result.write(writeListNumbers()); 533 result.write(OPEN_GROUP); 534 result.write(LIST_TEXT); 535 result.write(RtfParagraph.PARAGRAPH_DEFAULTS); 536 if(this.inTable) { 537 result.write(RtfParagraph.IN_TABLE); 538 } 539 result.write(RtfFontList.FONT_NUMBER); 540 if(this.listType != LIST_TYPE_BULLET) { 541 result.write(intToByteArray(fontNumber.getFontNumber())); 542 } else { 543 result.write(intToByteArray(fontBullet.getFontNumber())); 544 } 545 writeIndentations(result); 547 result.write(DELIMITER); 548 if(this.listType != LIST_TYPE_BULLET) { 549 switch(this.listType) { 550 case LIST_TYPE_NUMBERED : result.write(intToByteArray(itemNr)); break; 551 case LIST_TYPE_UPPER_LETTERS : result.write(RomanAlphabetFactory.getUpperCaseString(itemNr).getBytes()); break; 552 case LIST_TYPE_LOWER_LETTERS : result.write(RomanAlphabetFactory.getLowerCaseString(itemNr).getBytes()); break; 553 case LIST_TYPE_UPPER_ROMAN : result.write(RomanNumberFactory.getUpperCaseString(itemNr).getBytes()); break; 554 case LIST_TYPE_LOWER_ROMAN : result.write(RomanNumberFactory.getLowerCaseString(itemNr).getBytes()); break; 555 } 556 result.write(LIST_NUMBER_END); 557 } else { 558 this.document.filterSpecialChar(result, this.bulletCharacter, true, false); 559 } 560 result.write(TAB); 561 result.write(CLOSE_GROUP); 562 rtfElement.writeContent(result); 564 if(i < (items.size() - 1) || !this.inTable || this.listLevel > 0) { 565 result.write(RtfParagraph.PARAGRAPH); 566 } 567 result.write("\n".getBytes()); 568 } else if(rtfElement instanceof RtfList) { 569 rtfElement.writeContent(result); 571 result.write("\n".getBytes()); 572 } 573 } 574 result.write(CLOSE_GROUP); 575 576 if(!this.inTable) { 577 result.write(RtfParagraph.PARAGRAPH_DEFAULTS); 578 } 579 } 580 581 586 public int getListLevel() { 587 return listLevel; 588 } 589 590 596 public void setListLevel(int listLevel) { 597 this.listLevel = listLevel; 598 if(this.listLevel != 0) { 599 document.getDocumentHeader().freeListNumber(this); 600 for(int i = 0; i < this.items.size(); i++) { 601 if(this.items.get(i) instanceof RtfList) { 602 ((RtfList) this.items.get(i)).setListNumber(this.listNumber); 603 ((RtfList) this.items.get(i)).setListLevel(this.listLevel + 1); 604 } 605 } 606 } else { 607 this.listNumber = document.getDocumentHeader().getListNumber(this); 608 } 609 } 610 611 616 protected void setParent(RtfList parent) { 617 this.parentList = parent; 618 } 619 620 625 public int getListNumber() { 626 return listNumber; 627 } 628 629 634 public void setListNumber(int listNumber) { 635 this.listNumber = listNumber; 636 } 637 638 644 public void setInTable(boolean inTable) { 645 super.setInTable(inTable); 646 for(int i = 0; i < this.items.size(); i++) { 647 ((RtfBasicElement) this.items.get(i)).setInTable(inTable); 648 } 649 } 650 651 657 public void setInHeader(boolean inHeader) { 658 super.setInHeader(inHeader); 659 for(int i = 0; i < this.items.size(); i++) { 660 ((RtfBasicElement) this.items.get(i)).setInHeader(inHeader); 661 } 662 } 663 664 668 protected void correctIndentation() { 669 if(this.parentList != null) { 670 this.leftIndent = this.leftIndent + this.parentList.getLeftIndent() + this.parentList.getFirstIndent(); 671 } 672 for(int i = 0; i < this.items.size(); i++) { 673 if(this.items.get(i) instanceof RtfList) { 674 ((RtfList) this.items.get(i)).correctIndentation(); 675 } else if(this.items.get(i) instanceof RtfListItem) { 676 ((RtfListItem) this.items.get(i)).correctIndentation(); 677 } 678 } 679 } 680 681 686 private int getLeftIndent() { 687 return this.leftIndent; 688 } 689 690 695 private int getFirstIndent() { 696 return this.firstIndent; 697 } 698 } 699 | Popular Tags |