001: /*
002: * $Id: ElementFactory.java 2775 2007-05-23 09:12:39Z blowagie $
003: * $Name$
004: *
005: * Copyright 2007 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050: package com.lowagie.text.factories;
051:
052: import java.awt.Color;
053: import java.io.IOException;
054: import java.net.MalformedURLException;
055: import java.util.ArrayList;
056: import java.util.Properties;
057: import java.util.StringTokenizer;
058:
059: import com.lowagie.text.Anchor;
060: import com.lowagie.text.Annotation;
061: import com.lowagie.text.BadElementException;
062: import com.lowagie.text.Cell;
063: import com.lowagie.text.ChapterAutoNumber;
064: import com.lowagie.text.Chunk;
065: import com.lowagie.text.ElementTags;
066: import com.lowagie.text.ExceptionConverter;
067: import com.lowagie.text.FontFactory;
068: import com.lowagie.text.Image;
069: import com.lowagie.text.List;
070: import com.lowagie.text.ListItem;
071: import com.lowagie.text.Paragraph;
072: import com.lowagie.text.Phrase;
073: import com.lowagie.text.Rectangle;
074: import com.lowagie.text.Section;
075: import com.lowagie.text.Table;
076: import com.lowagie.text.Utilities;
077: import com.lowagie.text.html.Markup;
078:
079: /**
080: * This class is able to create Element objects based on a list of properties.
081: */
082:
083: public class ElementFactory {
084:
085: /**
086: * Creates a Chunk object based on a list of properties.
087: * @param attributes
088: * @return a Chunk
089: */
090: public static Chunk getChunk(Properties attributes) {
091: Chunk chunk = new Chunk();
092:
093: chunk.setFont(FontFactory.getFont(attributes));
094: String value;
095:
096: value = attributes.getProperty(ElementTags.ITEXT);
097: if (value != null) {
098: chunk.append(value);
099: }
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: } else {
110: String destination = attributes
111: .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(value.substring(0, value
128: .length() - 1)
129: + "f") / 100f;
130: chunk.setTextRise(p * chunk.getFont().getSize());
131: }
132: value = attributes.getProperty(ElementTags.GENERICTAG);
133: if (value != null) {
134: chunk.setGenericTag(value);
135: }
136: value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR);
137: if (value != null) {
138: chunk.setBackground(Markup.decodeColor(value));
139: }
140: return chunk;
141: }
142:
143: /**
144: * Creates a Phrase object based on a list of properties.
145: * @param attributes
146: * @return a Phrase
147: */
148: public static Phrase getPhrase(Properties attributes) {
149: Phrase phrase = new Phrase();
150: phrase.setFont(FontFactory.getFont(attributes));
151: String value;
152: value = attributes.getProperty(ElementTags.LEADING);
153: if (value != null) {
154: phrase.setLeading(Float.parseFloat(value + "f"));
155: }
156: value = attributes.getProperty(Markup.CSS_KEY_LINEHEIGHT);
157: if (value != null) {
158: phrase.setLeading(Markup.parseLength(value));
159: }
160: value = attributes.getProperty(ElementTags.ITEXT);
161: if (value != null) {
162: Chunk chunk = new Chunk(value);
163: if ((value = attributes.getProperty(ElementTags.GENERICTAG)) != null) {
164: chunk.setGenericTag(value);
165: }
166: phrase.add(chunk);
167: }
168: return phrase;
169: }
170:
171: /**
172: * Creates an Anchor object based on a list of properties.
173: * @param attributes
174: * @return an Anchor
175: */
176: public static Anchor getAnchor(Properties attributes) {
177: Anchor anchor = new Anchor(getPhrase(attributes));
178: String value;
179: value = attributes.getProperty(ElementTags.NAME);
180: if (value != null) {
181: anchor.setName(value);
182: }
183: value = (String) attributes.remove(ElementTags.REFERENCE);
184: if (value != null) {
185: anchor.setReference(value);
186: }
187: return anchor;
188: }
189:
190: /**
191: * Creates a Paragraph object based on a list of properties.
192: * @param attributes
193: * @return a Paragraph
194: */
195: public static Paragraph getParagraph(Properties attributes) {
196: Paragraph paragraph = new Paragraph(getPhrase(attributes));
197: String value;
198: value = attributes.getProperty(ElementTags.ALIGN);
199: if (value != null) {
200: paragraph.setAlignment(value);
201: }
202: value = attributes.getProperty(ElementTags.INDENTATIONLEFT);
203: if (value != null) {
204: paragraph.setIndentationLeft(Float.parseFloat(value + "f"));
205: }
206: value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);
207: if (value != null) {
208: paragraph
209: .setIndentationRight(Float.parseFloat(value + "f"));
210: }
211: return paragraph;
212: }
213:
214: /**
215: * Creates a ListItem object based on a list of properties.
216: * @param attributes
217: * @return a ListItem
218: */
219: public static ListItem getListItem(Properties attributes) {
220: ListItem item = new ListItem(getParagraph(attributes));
221: return item;
222: }
223:
224: /**
225: * Creates a List object based on a list of properties.
226: * @param attributes
227: * @return the List
228: */
229: public static List getList(Properties attributes) {
230: List list = new List();
231:
232: list.setNumbered(Utilities.checkTrueOrFalse(attributes,
233: ElementTags.NUMBERED));
234: list.setLettered(Utilities.checkTrueOrFalse(attributes,
235: ElementTags.LETTERED));
236: list.setLowercase(Utilities.checkTrueOrFalse(attributes,
237: ElementTags.LOWERCASE));
238: list.setAutoindent(Utilities.checkTrueOrFalse(attributes,
239: ElementTags.AUTO_INDENT_ITEMS));
240: list.setAlignindent(Utilities.checkTrueOrFalse(attributes,
241: ElementTags.ALIGN_INDENTATION_ITEMS));
242:
243: String value;
244:
245: value = attributes.getProperty(ElementTags.FIRST);
246: if (value != null) {
247: char character = value.charAt(0);
248: if (Character.isLetter(character)) {
249: list.setFirst(character);
250: } else {
251: list.setFirst(Integer.parseInt(value));
252: }
253: }
254:
255: value = attributes.getProperty(ElementTags.LISTSYMBOL);
256: if (value != null) {
257: list.setListSymbol(new Chunk(value, FontFactory
258: .getFont(attributes)));
259: }
260:
261: value = attributes.getProperty(ElementTags.INDENTATIONLEFT);
262: if (value != null) {
263: list.setIndentationLeft(Float.parseFloat(value + "f"));
264: }
265:
266: value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);
267: if (value != null) {
268: list.setIndentationRight(Float.parseFloat(value + "f"));
269: }
270:
271: value = attributes.getProperty(ElementTags.SYMBOLINDENT);
272: if (value != null) {
273: list.setSymbolIndent(Float.parseFloat(value));
274: }
275:
276: return list;
277: }
278:
279: /**
280: * Creates a Cell object based on a list of properties.
281: * @param attributes
282: * @return a Cell
283: */
284: public static Cell getCell(Properties attributes) {
285: Cell cell = new Cell();
286: String value;
287:
288: cell.setHorizontalAlignment(attributes
289: .getProperty(ElementTags.HORIZONTALALIGN));
290: cell.setVerticalAlignment(attributes
291: .getProperty(ElementTags.VERTICALALIGN));
292:
293: value = attributes.getProperty(ElementTags.WIDTH);
294: if (value != null) {
295: cell.setWidth(value);
296: }
297: value = attributes.getProperty(ElementTags.COLSPAN);
298: if (value != null) {
299: cell.setColspan(Integer.parseInt(value));
300: }
301: value = attributes.getProperty(ElementTags.ROWSPAN);
302: if (value != null) {
303: cell.setRowspan(Integer.parseInt(value));
304: }
305: value = attributes.getProperty(ElementTags.LEADING);
306: if (value != null) {
307: cell.setLeading(Float.parseFloat(value + "f"));
308: }
309: cell.setHeader(Utilities.checkTrueOrFalse(attributes,
310: ElementTags.HEADER));
311: if (Utilities.checkTrueOrFalse(attributes, ElementTags.NOWRAP)) {
312: cell.setMaxLines(1);
313: }
314: setRectangleProperties(cell, attributes);
315: return cell;
316: }
317:
318: /**
319: * Creates an Table object based on a list of properties.
320: * @param attributes
321: * @return a Table
322: */
323: public static Table getTable(Properties attributes) {
324: String value;
325: Table table;
326: try {
327:
328: value = attributes.getProperty(ElementTags.WIDTHS);
329: if (value != null) {
330: StringTokenizer widthTokens = new StringTokenizer(
331: value, ";");
332: ArrayList values = new ArrayList();
333: while (widthTokens.hasMoreTokens()) {
334: values.add(widthTokens.nextToken());
335: }
336: table = new Table(values.size());
337: float[] widths = new float[table.getColumns()];
338: for (int i = 0; i < values.size(); i++) {
339: value = (String) values.get(i);
340: widths[i] = Float.parseFloat(value + "f");
341: }
342: table.setWidths(widths);
343: } else {
344: value = attributes.getProperty(ElementTags.COLUMNS);
345: try {
346: table = new Table(Integer.parseInt(value));
347: } catch (Exception e) {
348: table = new Table(1);
349: }
350: }
351:
352: table.setBorder(Table.BOX);
353: table.setBorderWidth(1);
354: table.getDefaultLayout().setBorder(Table.BOX);
355:
356: value = attributes.getProperty(ElementTags.LASTHEADERROW);
357: if (value != null) {
358: table.setLastHeaderRow(Integer.parseInt(value));
359: }
360: value = attributes.getProperty(ElementTags.ALIGN);
361: if (value != null) {
362: table.setAlignment(value);
363: }
364: value = attributes.getProperty(ElementTags.CELLSPACING);
365: if (value != null) {
366: table.setSpacing(Float.parseFloat(value + "f"));
367: }
368: value = attributes.getProperty(ElementTags.CELLPADDING);
369: if (value != null) {
370: table.setPadding(Float.parseFloat(value + "f"));
371: }
372: value = attributes.getProperty(ElementTags.OFFSET);
373: if (value != null) {
374: table.setOffset(Float.parseFloat(value + "f"));
375: }
376: value = attributes.getProperty(ElementTags.WIDTH);
377: if (value != null) {
378: if (value.endsWith("%"))
379: table.setWidth(Float.parseFloat(value.substring(0,
380: value.length() - 1)
381: + "f"));
382: else {
383: table.setWidth(Float.parseFloat(value + "f"));
384: table.setLocked(true);
385: }
386: }
387: table.setTableFitsPage(Utilities.checkTrueOrFalse(
388: attributes, ElementTags.TABLEFITSPAGE));
389: table.setCellsFitPage(Utilities.checkTrueOrFalse(
390: attributes, ElementTags.CELLSFITPAGE));
391: table.setConvert2pdfptable(Utilities.checkTrueOrFalse(
392: attributes, ElementTags.CONVERT2PDFP));
393:
394: setRectangleProperties(table, attributes);
395: return table;
396: } catch (BadElementException e) {
397: throw new ExceptionConverter(e);
398: }
399: }
400:
401: /**
402: * Sets some Rectangle properties (for a Cell, Table,...).
403: */
404: private static void setRectangleProperties(Rectangle rect,
405: Properties attributes) {
406: String value;
407: value = attributes.getProperty(ElementTags.BORDERWIDTH);
408: if (value != null) {
409: rect.setBorderWidth(Float.parseFloat(value + "f"));
410: }
411: int border = 0;
412: if (Utilities.checkTrueOrFalse(attributes, ElementTags.LEFT)) {
413: border |= Rectangle.LEFT;
414: }
415: if (Utilities.checkTrueOrFalse(attributes, ElementTags.RIGHT)) {
416: border |= Rectangle.RIGHT;
417: }
418: if (Utilities.checkTrueOrFalse(attributes, ElementTags.TOP)) {
419: border |= Rectangle.TOP;
420: }
421: if (Utilities.checkTrueOrFalse(attributes, ElementTags.BOTTOM)) {
422: border |= Rectangle.BOTTOM;
423: }
424: rect.setBorder(border);
425:
426: String r = attributes.getProperty(ElementTags.RED);
427: String g = attributes.getProperty(ElementTags.GREEN);
428: String b = attributes.getProperty(ElementTags.BLUE);
429: if (r != null || g != null || b != null) {
430: int red = 0;
431: int green = 0;
432: int blue = 0;
433: if (r != null)
434: red = Integer.parseInt(r);
435: if (g != null)
436: green = Integer.parseInt(g);
437: if (b != null)
438: blue = Integer.parseInt(b);
439: rect.setBorderColor(new Color(red, green, blue));
440: } else {
441: rect.setBorderColor(Markup.decodeColor(attributes
442: .getProperty(ElementTags.BORDERCOLOR)));
443: }
444: r = (String) attributes.remove(ElementTags.BGRED);
445: g = (String) attributes.remove(ElementTags.BGGREEN);
446: b = (String) attributes.remove(ElementTags.BGBLUE);
447: value = attributes.getProperty(ElementTags.BACKGROUNDCOLOR);
448: if (r != null || g != null || b != null) {
449: int red = 0;
450: int green = 0;
451: int blue = 0;
452: if (r != null)
453: red = Integer.parseInt(r);
454: if (g != null)
455: green = Integer.parseInt(g);
456: if (b != null)
457: blue = Integer.parseInt(b);
458: rect.setBackgroundColor(new Color(red, green, blue));
459: } else if (value != null) {
460: rect.setBackgroundColor(Markup.decodeColor(value));
461: } else {
462: value = attributes.getProperty(ElementTags.GRAYFILL);
463: if (value != null) {
464: rect.setGrayFill(Float.parseFloat(value + "f"));
465: }
466: }
467: }
468:
469: /**
470: * Creates a ChapterAutoNumber object based on a list of properties.
471: * @param attributes
472: * @return a Chapter
473: */
474: public static ChapterAutoNumber getChapter(Properties attributes) {
475: ChapterAutoNumber chapter = new ChapterAutoNumber("");
476: setSectionParameters(chapter, attributes);
477: return chapter;
478: }
479:
480: /**
481: * Creates a Section object based on a list of properties.
482: * @param attributes
483: * @return a Section
484: */
485: public static Section getSection(Section parent,
486: Properties attributes) {
487: Section section = parent.addSection("");
488: setSectionParameters(section, attributes);
489: return section;
490: }
491:
492: /**
493: * Helper method to create a Chapter/Section object.
494: * @param attributes
495: */
496: private static void setSectionParameters(Section section,
497: Properties attributes) {
498: String value;
499: value = attributes.getProperty(ElementTags.NUMBERDEPTH);
500: if (value != null) {
501: section.setNumberDepth(Integer.parseInt(value));
502: }
503: value = attributes.getProperty(ElementTags.INDENT);
504: if (value != null) {
505: section.setIndentation(Float.parseFloat(value + "f"));
506: }
507: value = attributes.getProperty(ElementTags.INDENTATIONLEFT);
508: if (value != null) {
509: section.setIndentationLeft(Float.parseFloat(value + "f"));
510: }
511: value = attributes.getProperty(ElementTags.INDENTATIONRIGHT);
512: if (value != null) {
513: section.setIndentationRight(Float.parseFloat(value + "f"));
514: }
515: }
516:
517: /**
518: * Creates an Image object based on a list of properties.
519: * @param attributes
520: * @return an Image
521: */
522: public static Image getImage(Properties attributes)
523: throws BadElementException, MalformedURLException,
524: IOException {
525: String value;
526:
527: value = attributes.getProperty(ElementTags.URL);
528: if (value == null)
529: throw new MalformedURLException(
530: "The URL of the image is missing.");
531: Image image = Image.getInstance(value);
532:
533: value = attributes.getProperty(ElementTags.ALIGN);
534: int align = 0;
535: if (value != null) {
536: if (ElementTags.ALIGN_LEFT.equalsIgnoreCase(value))
537: align |= Image.LEFT;
538: else if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(value))
539: align |= Image.RIGHT;
540: else if (ElementTags.ALIGN_MIDDLE.equalsIgnoreCase(value))
541: align |= Image.MIDDLE;
542: }
543: if ("true".equalsIgnoreCase(attributes
544: .getProperty(ElementTags.UNDERLYING)))
545: align |= Image.UNDERLYING;
546: if ("true".equalsIgnoreCase(attributes
547: .getProperty(ElementTags.TEXTWRAP)))
548: align |= Image.TEXTWRAP;
549: image.setAlignment(align);
550:
551: value = attributes.getProperty(ElementTags.ALT);
552: if (value != null) {
553: image.setAlt(value);
554: }
555:
556: String x = attributes.getProperty(ElementTags.ABSOLUTEX);
557: String y = attributes.getProperty(ElementTags.ABSOLUTEY);
558: if ((x != null) && (y != null)) {
559: image.setAbsolutePosition(Float.parseFloat(x + "f"), Float
560: .parseFloat(y + "f"));
561: }
562: value = attributes.getProperty(ElementTags.PLAINWIDTH);
563: if (value != null) {
564: image.scaleAbsoluteWidth(Float.parseFloat(value + "f"));
565: }
566: value = attributes.getProperty(ElementTags.PLAINHEIGHT);
567: if (value != null) {
568: image.scaleAbsoluteHeight(Float.parseFloat(value + "f"));
569: }
570: value = attributes.getProperty(ElementTags.ROTATION);
571: if (value != null) {
572: image.setRotation(Float.parseFloat(value + "f"));
573: }
574: return image;
575: }
576:
577: /**
578: * Creates an Annotation object based on a list of properties.
579: * @param attributes
580: * @return an Annotation
581: */
582: public static Annotation getAnnotation(Properties attributes) {
583: float llx = 0, lly = 0, urx = 0, ury = 0;
584: String value;
585:
586: value = attributes.getProperty(ElementTags.LLX);
587: if (value != null) {
588: llx = Float.parseFloat(value + "f");
589: }
590: value = attributes.getProperty(ElementTags.LLY);
591: if (value != null) {
592: lly = Float.parseFloat(value + "f");
593: }
594: value = attributes.getProperty(ElementTags.URX);
595: if (value != null) {
596: urx = Float.parseFloat(value + "f");
597: }
598: value = attributes.getProperty(ElementTags.URY);
599: if (value != null) {
600: ury = Float.parseFloat(value + "f");
601: }
602:
603: String title = attributes.getProperty(ElementTags.TITLE);
604: String text = attributes.getProperty(ElementTags.CONTENT);
605: if (title != null || text != null) {
606: return new Annotation(title, text, llx, lly, urx, ury);
607: }
608: value = attributes.getProperty(ElementTags.URL);
609: if (value != null) {
610: return new Annotation(llx, lly, urx, ury, value);
611: }
612: value = attributes.getProperty(ElementTags.NAMED);
613: if (value != null) {
614: return new Annotation(llx, lly, urx, ury, Integer
615: .parseInt(value));
616: }
617: String file = attributes.getProperty(ElementTags.FILE);
618: String destination = attributes
619: .getProperty(ElementTags.DESTINATION);
620: String page = (String) attributes.remove(ElementTags.PAGE);
621: if (file != null) {
622: if (destination != null) {
623: return new Annotation(llx, lly, urx, ury, file,
624: destination);
625: }
626: if (page != null) {
627: return new Annotation(llx, lly, urx, ury, file, Integer
628: .parseInt(page));
629: }
630: }
631: if (title == null)
632: title = "";
633: if (text == null)
634: text = "";
635: return new Annotation(title, text, llx, lly, urx, ury);
636: }
637: }
|