KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > factories > ElementFactory


1 /*
2  * $Id: ElementFactory.java 2775 2007-05-23 09:12:39Z blowagie $
3  * $Name$
4  *
5  * Copyright 2007 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50 package com.lowagie.text.factories;
51
52 import java.awt.Color JavaDoc;
53 import java.io.IOException JavaDoc;
54 import java.net.MalformedURLException JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.Properties JavaDoc;
57 import java.util.StringTokenizer JavaDoc;
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 /**
80  * This class is able to create Element objects based on a list of properties.
81  */

82
83 public class ElementFactory {
84
85     /**
86      * Creates a Chunk object based on a list of properties.
87      * @param attributes
88      * @return a Chunk
89      */

90     public static Chunk getChunk(Properties JavaDoc attributes) {
91         Chunk chunk = new Chunk();
92         
93         chunk.setFont(FontFactory.getFont(attributes));
94         String JavaDoc 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 JavaDoc page = attributes.getProperty(ElementTags.PAGE);
107             if (page != null) {
108                 chunk.setRemoteGoto(value, Integer.parseInt(page));
109             }
110             else {
111                 String JavaDoc 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     /**
143      * Creates a Phrase object based on a list of properties.
144      * @param attributes
145      * @return a Phrase
146      */

147     public static Phrase getPhrase(Properties JavaDoc attributes) {
148         Phrase phrase = new Phrase();
149         phrase.setFont(FontFactory.getFont(attributes));
150         String JavaDoc 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     /**
171      * Creates an Anchor object based on a list of properties.
172      * @param attributes
173      * @return an Anchor
174      */

175     public static Anchor getAnchor(Properties JavaDoc attributes) {
176         Anchor anchor = new Anchor(getPhrase(attributes));
177         String JavaDoc value;
178         value = attributes.getProperty(ElementTags.NAME);
179         if (value != null) {
180             anchor.setName(value);
181         }
182         value = (String JavaDoc)attributes.remove(ElementTags.REFERENCE);
183         if (value != null) {
184             anchor.setReference(value);
185         }
186         return anchor;
187     }
188
189     /**
190      * Creates a Paragraph object based on a list of properties.
191      * @param attributes
192      * @return a Paragraph
193      */

194     public static Paragraph getParagraph(Properties JavaDoc attributes) {
195         Paragraph paragraph = new Paragraph(getPhrase(attributes));
196         String JavaDoc 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     /**
213      * Creates a ListItem object based on a list of properties.
214      * @param attributes
215      * @return a ListItem
216      */

217     public static ListItem getListItem(Properties JavaDoc attributes) {
218         ListItem item = new ListItem(getParagraph(attributes));
219         return item;
220     }
221
222     /**
223      * Creates a List object based on a list of properties.
224      * @param attributes
225      * @return the List
226      */

227     public static List JavaDoc getList(Properties JavaDoc attributes) {
228         List JavaDoc list = new List JavaDoc();
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 JavaDoc 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     /**
273      * Creates a Cell object based on a list of properties.
274      * @param attributes
275      * @return a Cell
276      */

277     public static Cell getCell(Properties JavaDoc attributes) {
278         Cell cell = new Cell();
279         String JavaDoc 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     /**
309      * Creates an Table object based on a list of properties.
310      * @param attributes
311      * @return a Table
312      */

313     public static Table getTable(Properties JavaDoc attributes) {
314         String JavaDoc value;
315         Table table;
316         try {
317
318             value = attributes.getProperty(ElementTags.WIDTHS);
319             if (value != null) {
320                 StringTokenizer JavaDoc widthTokens = new StringTokenizer JavaDoc(value, ";");
321                 ArrayList JavaDoc values = new ArrayList JavaDoc();
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 JavaDoc)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 JavaDoc 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     /**
388      * Sets some Rectangle properties (for a Cell, Table,...).
389      */

390     private static void setRectangleProperties(Rectangle rect, Properties JavaDoc attributes) {
391         String JavaDoc 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 JavaDoc r = attributes.getProperty(ElementTags.RED);
412         String JavaDoc g = attributes.getProperty(ElementTags.GREEN);
413         String JavaDoc 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 JavaDoc(red, green, blue));
422         }
423         else {
424             rect.setBorderColor(Markup.decodeColor(attributes.getProperty(ElementTags.BORDERCOLOR)));
425         }
426         r = (String JavaDoc)attributes.remove(ElementTags.BGRED);
427         g = (String JavaDoc)attributes.remove(ElementTags.BGGREEN);
428         b = (String JavaDoc)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 JavaDoc(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     /**
451      * Creates a ChapterAutoNumber object based on a list of properties.
452      * @param attributes
453      * @return a Chapter
454      */

455     public static ChapterAutoNumber getChapter(Properties JavaDoc attributes) {
456         ChapterAutoNumber chapter = new ChapterAutoNumber("");
457         setSectionParameters(chapter, attributes);
458         return chapter;
459     }
460
461     /**
462      * Creates a Section object based on a list of properties.
463      * @param attributes
464      * @return a Section
465      */

466     public static Section getSection(Section parent, Properties JavaDoc attributes) {
467         Section section = parent.addSection("");
468         setSectionParameters(section, attributes);
469         return section;
470     }
471
472     /**
473      * Helper method to create a Chapter/Section object.
474      * @param attributes
475      */

476     private static void setSectionParameters(Section section, Properties JavaDoc attributes) {
477         String JavaDoc 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     /**
497      * Creates an Image object based on a list of properties.
498      * @param attributes
499      * @return an Image
500      */

501     public static Image getImage(Properties JavaDoc attributes)
502             throws BadElementException, MalformedURLException JavaDoc, IOException JavaDoc {
503         String JavaDoc value;
504         
505         value = attributes.getProperty(ElementTags.URL);
506         if (value == null)
507             throw new MalformedURLException JavaDoc("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 JavaDoc x = attributes.getProperty(ElementTags.ABSOLUTEX);
532         String JavaDoc 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     /**
553      * Creates an Annotation object based on a list of properties.
554      * @param attributes
555      * @return an Annotation
556      */

557     public static Annotation getAnnotation(Properties JavaDoc attributes) {
558         float llx = 0, lly = 0, urx = 0, ury = 0;
559         String JavaDoc 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 JavaDoc title = attributes.getProperty(ElementTags.TITLE);
579         String JavaDoc 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 JavaDoc file = attributes.getProperty(ElementTags.FILE);
592         String JavaDoc destination = attributes.getProperty(ElementTags.DESTINATION);
593         String JavaDoc page = (String JavaDoc) 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