1 50 package com.lowagie.text.factories; 51 52 import java.awt.Color ; 53 import java.io.IOException ; 54 import java.net.MalformedURLException ; 55 import java.util.ArrayList ; 56 import java.util.Properties ; 57 import java.util.StringTokenizer ; 58 59 import com.lowagie.text.Anchor; 60 import com.lowagie.text.Annotation; 61 import com.lowagie.text.BadElementException; 62 import com.lowagie.text.Cell; 63 import com.lowagie.text.ChapterAutoNumber; 64 import com.lowagie.text.Chunk; 65 import com.lowagie.text.ElementTags; 66 import com.lowagie.text.ExceptionConverter; 67 import com.lowagie.text.FontFactory; 68 import com.lowagie.text.Image; 69 import com.lowagie.text.List; 70 import com.lowagie.text.ListItem; 71 import com.lowagie.text.Paragraph; 72 import com.lowagie.text.Phrase; 73 import com.lowagie.text.Rectangle; 74 import com.lowagie.text.Section; 75 import com.lowagie.text.Table; 76 import com.lowagie.text.Utilities; 77 import com.lowagie.text.html.Markup; 78 79 82 83 public class ElementFactory { 84 85 90 public static Chunk getChunk(Properties attributes) { 91 Chunk chunk = new Chunk(); 92 93 chunk.setFont(FontFactory.getFont(attributes)); 94 String value; 95 96 value = attributes.getProperty(ElementTags.ITEXT); 97 if (value != null) { 98 chunk.append(value); 99 } 100 value = attributes.getProperty(ElementTags.LOCALGOTO); 101 if (value != null) { 102 chunk.setLocalGoto(value); 103 } 104 value = attributes.getProperty(ElementTags.REMOTEGOTO); 105 if (value != null) { 106 String page = attributes.getProperty(ElementTags.PAGE); 107 if (page != null) { 108 chunk.setRemoteGoto(value, Integer.parseInt(page)); 109 } 110 else { 111 String destination = attributes.getProperty(ElementTags.DESTINATION); 112 if (destination != null) { 113 chunk.setRemoteGoto(value, destination); 114 } 115 } 116 } 117 value = attributes.getProperty(ElementTags.LOCALDESTINATION); 118 if (value != null) { 119 chunk.setLocalDestination(value); 120 } 121 value = attributes.getProperty(ElementTags.SUBSUPSCRIPT); 122 if (value != null) { 123 chunk.setTextRise(Float.parseFloat(value + "f")); 124 } 125 value = attributes.getProperty(Markup.CSS_KEY_VERTICALALIGN); 126 if (value != null && value.endsWith("%")) { 127 float p = Float.parseFloat( 128 value.substring(0, value.length() - 1) + "f") / 100f; 129 chunk.setTextRise(p * chunk.getFont().getSize()); 130 } 131 value = attributes.getProperty(ElementTags.GENERICTAG); 132 if (value != null) { 133 chunk.setGenericTag(value); 134 } 135 value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR); 136 if (value != null) { 137 chunk.setBackground(Markup.decodeColor(value)); 138 } 139 return chunk; 140 } 141 142 147 public static Phrase getPhrase(Properties attributes) { 148 Phrase phrase = new Phrase(); 149 phrase.setFont(FontFactory.getFont(attributes)); 150 String value; 151 value = attributes.getProperty(ElementTags.LEADING); 152 if (value != null) { 153 phrase.setLeading(Float.parseFloat(value + "f")); 154 } 155 value = attributes.getProperty(Markup.CSS_KEY_LINEHEIGHT); 156 if (value != null) { 157 phrase.setLeading(Markup.parseLength(value)); 158 } 159 value = attributes.getProperty(ElementTags.ITEXT); 160 if (value != null) { 161 Chunk chunk = new Chunk(value); 162 if ((value = attributes.getProperty(ElementTags.GENERICTAG)) != null) { 163 chunk.setGenericTag(value); 164 } 165 phrase.add(chunk); 166 } 167 return phrase; 168 } 169 170 175 public static Anchor getAnchor(Properties attributes) { 176 Anchor anchor = new Anchor(getPhrase(attributes)); 177 String value; 178 value = attributes.getProperty(ElementTags.NAME); 179 if (value != null) { 180 anchor.setName(value); 181 } 182 value = (String )attributes.remove(ElementTags.REFERENCE); 183 if (value != null) { 184 anchor.setReference(value); 185 } 186 return anchor; 187 } 188 189 194 public static Paragraph getParagraph(Properties attributes) { 195 Paragraph paragraph = new Paragraph(getPhrase(attributes)); 196 String value; 197 value = attributes.getProperty(ElementTags.ALIGN); 198 if (value != null) { 199 paragraph.setAlignment(value); 200 } 201 value = attributes.getProperty(ElementTags.INDENTATIONLEFT); 202 if (value != null) { 203 paragraph.setIndentationLeft(Float.parseFloat(value + "f")); 204 } 205 value = attributes.getProperty(ElementTags.INDENTATIONRIGHT); 206 if (value != null) { 207 paragraph.setIndentationRight(Float.parseFloat(value + "f")); 208 } 209 return paragraph; 210 } 211 212 217 public static ListItem getListItem(Properties attributes) { 218 ListItem item = new ListItem(getParagraph(attributes)); 219 return item; 220 } 221 222 227 public static List getList(Properties attributes) { 228 List list = new List (); 229 230 list.setNumbered(Utilities.checkTrueOrFalse(attributes, ElementTags.NUMBERED)); 231 list.setLettered(Utilities.checkTrueOrFalse(attributes, ElementTags.LETTERED)); 232 list.setLowercase(Utilities.checkTrueOrFalse(attributes, ElementTags.LOWERCASE)); 233 list.setAutoindent(Utilities.checkTrueOrFalse(attributes, ElementTags.AUTO_INDENT_ITEMS)); 234 list.setAlignindent(Utilities.checkTrueOrFalse(attributes, ElementTags.ALIGN_INDENTATION_ITEMS)); 235 236 String value; 237 238 value = attributes.getProperty(ElementTags.FIRST); 239 if (value != null) { 240 char character = value.charAt(0); 241 if (Character.isLetter(character) ) { 242 list.setFirst(character); 243 } 244 else { 245 list.setFirst(Integer.parseInt(value)); 246 } 247 } 248 249 value= attributes.getProperty(ElementTags.LISTSYMBOL); 250 if (value != null) { 251 list.setListSymbol(new Chunk(value, FontFactory.getFont(attributes))); 252 } 253 254 value = attributes.getProperty(ElementTags.INDENTATIONLEFT); 255 if (value != null) { 256 list.setIndentationLeft(Float.parseFloat(value + "f")); 257 } 258 259 value = attributes.getProperty(ElementTags.INDENTATIONRIGHT); 260 if (value != null) { 261 list.setIndentationRight(Float.parseFloat(value + "f")); 262 } 263 264 value = attributes.getProperty(ElementTags.SYMBOLINDENT); 265 if (value != null) { 266 list.setSymbolIndent(Float.parseFloat(value)); 267 } 268 269 return list; 270 } 271 272 277 public static Cell getCell(Properties attributes) { 278 Cell cell = new Cell(); 279 String value; 280 281 cell.setHorizontalAlignment(attributes.getProperty(ElementTags.HORIZONTALALIGN)); 282 cell.setVerticalAlignment(attributes.getProperty(ElementTags.VERTICALALIGN)); 283 284 value = attributes.getProperty(ElementTags.WIDTH); 285 if (value != null) { 286 cell.setWidth(value); 287 } 288 value = attributes.getProperty(ElementTags.COLSPAN); 289 if (value != null) { 290 cell.setColspan(Integer.parseInt(value)); 291 } 292 value = attributes.getProperty(ElementTags.ROWSPAN); 293 if (value != null) { 294 cell.setRowspan(Integer.parseInt(value)); 295 } 296 value = attributes.getProperty(ElementTags.LEADING); 297 if (value != null) { 298 cell.setLeading(Float.parseFloat(value + "f")); 299 } 300 cell.setHeader(Utilities.checkTrueOrFalse(attributes, ElementTags.HEADER)); 301 if (Utilities.checkTrueOrFalse(attributes, ElementTags.NOWRAP)) { 302 cell.setMaxLines(1); 303 } 304 setRectangleProperties(cell, attributes); 305 return cell; 306 } 307 308 313 public static Table getTable(Properties attributes) { 314 String value; 315 Table table; 316 try { 317 318 value = attributes.getProperty(ElementTags.WIDTHS); 319 if (value != null) { 320 StringTokenizer widthTokens = new StringTokenizer (value, ";"); 321 ArrayList values = new ArrayList (); 322 while (widthTokens.hasMoreTokens()) { 323 values.add(widthTokens.nextToken()); 324 } 325 table = new Table(values.size()); 326 float[] widths = new float[table.getColumns()]; 327 for (int i = 0; i < values.size(); i++) { 328 value = (String )values.get(i); 329 widths[i] = Float.parseFloat(value + "f"); 330 } 331 table.setWidths(widths); 332 } 333 else { 334 value = attributes.getProperty(ElementTags.COLUMNS); 335 try { 336 table = new Table(Integer.parseInt(value)); 337 } 338 catch(Exception e) { 339 table = new Table(1); 340 } 341 } 342 343 table.setBorder(Table.BOX); 344 table.setBorderWidth(1); 345 table.getDefaultLayout().setBorder(Table.BOX); 346 347 value = attributes.getProperty(ElementTags.LASTHEADERROW); 348 if (value != null) { 349 table.setLastHeaderRow(Integer.parseInt(value)); 350 } 351 value = attributes.getProperty(ElementTags.ALIGN); 352 if (value != null) { 353 table.setAlignment(value); 354 } 355 value = attributes.getProperty(ElementTags.CELLSPACING); 356 if (value != null) { 357 table.setSpacing(Float.parseFloat(value + "f")); 358 } 359 value = attributes.getProperty(ElementTags.CELLPADDING); 360 if (value != null) { 361 table.setPadding(Float.parseFloat(value + "f")); 362 } 363 value = attributes.getProperty(ElementTags.OFFSET); 364 if (value != null) { 365 table.setOffset(Float.parseFloat(value + "f")); 366 } 367 value = attributes.getProperty(ElementTags.WIDTH); 368 if (value != null) { 369 if (value.endsWith("%")) 370 table.setWidth(Float.parseFloat(value.substring(0, value.length() - 1) + "f")); 371 else { 372 table.setWidth(Float.parseFloat(value + "f")); 373 table.setLocked(true); 374 } 375 } 376 table.setTableFitsPage(Utilities.checkTrueOrFalse(attributes, ElementTags.TABLEFITSPAGE)); 377 table.setCellsFitPage(Utilities.checkTrueOrFalse(attributes, ElementTags.CELLSFITPAGE)); 378 table.setConvert2pdfptable(Utilities.checkTrueOrFalse(attributes, ElementTags.CONVERT2PDFP)); 379 380 setRectangleProperties(table, attributes); 381 return table; 382 } catch (BadElementException e) { 383 throw new ExceptionConverter(e); 384 } 385 } 386 387 390 private static void setRectangleProperties(Rectangle rect, Properties attributes) { 391 String value; 392 value = attributes.getProperty(ElementTags.BORDERWIDTH); 393 if (value != null) { 394 rect.setBorderWidth(Float.parseFloat(value + "f")); 395 } 396 int border = 0; 397 if (Utilities.checkTrueOrFalse(attributes, ElementTags.LEFT)) { 398 border |= Rectangle.LEFT; 399 } 400 if (Utilities.checkTrueOrFalse(attributes, ElementTags.RIGHT)) { 401 border |= Rectangle.RIGHT; 402 } 403 if (Utilities.checkTrueOrFalse(attributes, ElementTags.TOP)) { 404 border |= Rectangle.TOP; 405 } 406 if (Utilities.checkTrueOrFalse(attributes, ElementTags.BOTTOM)) { 407 border |= Rectangle.BOTTOM; 408 } 409 rect.setBorder(border); 410 411 String r = attributes.getProperty(ElementTags.RED); 412 String g = attributes.getProperty(ElementTags.GREEN); 413 String b = attributes.getProperty(ElementTags.BLUE); 414 if (r != null || g != null || b != null) { 415 int red = 0; 416 int green = 0; 417 int blue = 0; 418 if (r != null) red = Integer.parseInt(r); 419 if (g != null) green = Integer.parseInt(g); 420 if (b != null) blue = Integer.parseInt(b); 421 rect.setBorderColor(new Color (red, green, blue)); 422 } 423 else { 424 rect.setBorderColor(Markup.decodeColor(attributes.getProperty(ElementTags.BORDERCOLOR))); 425 } 426 r = (String )attributes.remove(ElementTags.BGRED); 427 g = (String )attributes.remove(ElementTags.BGGREEN); 428 b = (String )attributes.remove(ElementTags.BGBLUE); 429 value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR); 430 if (r != null || g != null || b != null) { 431 int red = 0; 432 int green = 0; 433 int blue = 0; 434 if (r != null) red = Integer.parseInt(r); 435 if (g != null) green = Integer.parseInt(g); 436 if (b != null) blue = Integer.parseInt(b); 437 rect.setBackgroundColor(new Color (red, green, blue)); 438 } 439 else if (value != null) { 440 rect.setBackgroundColor(Markup.decodeColor(value)); 441 } 442 else { 443 value = attributes.getProperty(ElementTags.GRAYFILL); 444 if (value != null) { 445 rect.setGrayFill(Float.parseFloat(value + "f")); 446 } 447 } 448 } 449 450 455 public static ChapterAutoNumber getChapter(Properties attributes) { 456 ChapterAutoNumber chapter = new ChapterAutoNumber(""); 457 setSectionParameters(chapter, attributes); 458 return chapter; 459 } 460 461 466 public static Section getSection(Section parent, Properties attributes) { 467 Section section = parent.addSection(""); 468 setSectionParameters(section, attributes); 469 return section; 470 } 471 472 476 private static void setSectionParameters(Section section, Properties attributes) { 477 String value; 478 value = attributes.getProperty(ElementTags.NUMBERDEPTH); 479 if (value != null) { 480 section.setNumberDepth(Integer.parseInt(value)); 481 } 482 value = attributes.getProperty(ElementTags.INDENT); 483 if (value != null) { 484 section.setIndentation(Float.parseFloat(value + "f")); 485 } 486 value = attributes.getProperty(ElementTags.INDENTATIONLEFT); 487 if (value != null) { 488 section.setIndentationLeft(Float.parseFloat(value + "f")); 489 } 490 value = attributes.getProperty(ElementTags.INDENTATIONRIGHT); 491 if (value != null) { 492 section.setIndentationRight(Float.parseFloat(value + "f")); 493 } 494 } 495 496 501 public static Image getImage(Properties attributes) 502 throws BadElementException, MalformedURLException , IOException { 503 String value; 504 505 value = attributes.getProperty(ElementTags.URL); 506 if (value == null) 507 throw new MalformedURLException ("The URL of the image is missing."); 508 Image image = Image.getInstance(value); 509 510 value = attributes.getProperty(ElementTags.ALIGN); 511 int align = 0; 512 if (value != null) { 513 if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(value)) 514 align |= Image.LEFT; 515 else if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(value)) 516 align |= Image.RIGHT; 517 else if (ElementTags.ALIGN_MIDDLE.equalsIgnoreCase(value)) 518 align |= Image.MIDDLE; 519 } 520 if ("true".equalsIgnoreCase(attributes.getProperty(ElementTags.UNDERLYING))) 521 align |= Image.UNDERLYING; 522 if ("true".equalsIgnoreCase(attributes.getProperty(ElementTags.TEXTWRAP))) 523 align |= Image.TEXTWRAP; 524 image.setAlignment(align); 525 526 value = attributes.getProperty(ElementTags.ALT); 527 if (value != null) { 528 image.setAlt(value); 529 } 530 531 String x = attributes.getProperty(ElementTags.ABSOLUTEX); 532 String y = attributes.getProperty(ElementTags.ABSOLUTEY); 533 if ((x != null) && (y != null)) { 534 image.setAbsolutePosition(Float.parseFloat(x + "f"), 535 Float.parseFloat(y + "f")); 536 } 537 value = attributes.getProperty(ElementTags.PLAINWIDTH); 538 if (value != null) { 539 image.scaleAbsoluteWidth(Float.parseFloat(value + "f")); 540 } 541 value = attributes.getProperty(ElementTags.PLAINHEIGHT); 542 if (value != null) { 543 image.scaleAbsoluteHeight(Float.parseFloat(value + "f")); 544 } 545 value = attributes.getProperty(ElementTags.ROTATION); 546 if (value != null) { 547 image.setRotation(Float.parseFloat(value + "f")); 548 } 549 return image; 550 } 551 552 557 public static Annotation getAnnotation(Properties attributes) { 558 float llx = 0, lly = 0, urx = 0, ury = 0; 559 String value; 560 561 value = attributes.getProperty(ElementTags.LLX); 562 if (value != null) { 563 llx = Float.parseFloat(value + "f"); 564 } 565 value = attributes.getProperty(ElementTags.LLY); 566 if (value != null) { 567 lly = Float.parseFloat(value + "f"); 568 } 569 value = attributes.getProperty(ElementTags.URX); 570 if (value != null) { 571 urx = Float.parseFloat(value + "f"); 572 } 573 value = attributes.getProperty(ElementTags.URY); 574 if (value != null) { 575 ury = Float.parseFloat(value + "f"); 576 } 577 578 String title = attributes.getProperty(ElementTags.TITLE); 579 String text = attributes.getProperty(ElementTags.CONTENT); 580 if (title != null || text != null) { 581 return new Annotation(title, text, llx, lly, urx, ury); 582 } 583 value = attributes.getProperty(ElementTags.URL); 584 if (value != null) { 585 return new Annotation(llx, lly, urx, ury, value); 586 } 587 value = attributes.getProperty(ElementTags.NAMED); 588 if (value != null) { 589 return new Annotation(llx, lly, urx, ury, Integer.parseInt(value)); 590 } 591 String file = attributes.getProperty(ElementTags.FILE); 592 String destination = attributes.getProperty(ElementTags.DESTINATION); 593 String page = (String ) attributes.remove(ElementTags.PAGE); 594 if (file != null) { 595 if (destination != null) { 596 return new Annotation(llx, lly, urx, ury, file, destination); 597 } 598 if (page != null) { 599 return new Annotation(llx, lly, urx, ury, file, Integer.parseInt(page)); 600 } 601 } 602 if (title == null) 603 title = ""; 604 if (text == null) 605 text = ""; 606 return new Annotation(title, text, llx, lly, urx, ury); 607 } 608 } | Popular Tags |