KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > html > simpleparser > FactoryProperties


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

47
48 package com.lowagie.text.html.simpleparser;
49
50 import java.awt.Color JavaDoc;
51 import java.util.HashMap JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.Properties JavaDoc;
54 import java.util.StringTokenizer JavaDoc;
55
56 import com.lowagie.text.Chunk;
57 import com.lowagie.text.Element;
58 import com.lowagie.text.Font;
59 import com.lowagie.text.FontFactory;
60 import com.lowagie.text.FontFactoryImp;
61 import com.lowagie.text.ListItem;
62 import com.lowagie.text.Paragraph;
63 import com.lowagie.text.html.Markup;
64 import com.lowagie.text.pdf.BaseFont;
65
66 /**
67  *
68  * @author psoares
69  */

70 public class FactoryProperties {
71     
72     private FontFactoryImp fontImp = FontFactory.getFontImp();
73     
74     /** Creates a new instance of FactoryProperties */
75     public FactoryProperties() {
76     }
77     
78     public Chunk createChunk(String JavaDoc text, ChainedProperties props) {
79         Font font = getFont(props);
80         float size = font.getSize();
81         size /= 2;
82         Chunk ck = new Chunk(text, font);
83         if (props.hasProperty("sub"))
84             ck.setTextRise(-size);
85         else if (props.hasProperty("sup"))
86             ck.setTextRise(size);
87         return ck;
88     }
89     
90     private static void setParagraphLeading(Paragraph p, String JavaDoc leading) {
91         if (leading == null) {
92             p.setLeading(0, 1.5f);
93             return;
94         }
95         try {
96             StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc(leading, " ,");
97             String JavaDoc v = tk.nextToken();
98             float v1 = Float.parseFloat(v);
99             if (!tk.hasMoreTokens()) {
100                 p.setLeading(v1, 0);
101                 return;
102             }
103             v = tk.nextToken();
104             float v2 = Float.parseFloat(v);
105             p.setLeading(v1, v2);
106         }
107         catch (Exception JavaDoc e) {
108             p.setLeading(0, 1.5f);
109         }
110     }
111
112     public static Paragraph createParagraph(HashMap JavaDoc props) {
113         Paragraph p = new Paragraph();
114         String JavaDoc value = (String JavaDoc)props.get("align");
115         if (value != null) {
116             if (value.equalsIgnoreCase("center"))
117                 p.setAlignment(Element.ALIGN_CENTER);
118             else if (value.equalsIgnoreCase("right"))
119                 p.setAlignment(Element.ALIGN_RIGHT);
120             else if (value.equalsIgnoreCase("justify"))
121                 p.setAlignment(Element.ALIGN_JUSTIFIED);
122         }
123         setParagraphLeading(p, (String JavaDoc)props.get("leading"));
124         return p;
125     }
126     
127     public static void createParagraph(Paragraph p, ChainedProperties props) {
128         String JavaDoc value = props.getProperty("align");
129         if (value != null) {
130             if (value.equalsIgnoreCase("center"))
131                 p.setAlignment(Element.ALIGN_CENTER);
132             else if (value.equalsIgnoreCase("right"))
133                 p.setAlignment(Element.ALIGN_RIGHT);
134             else if (value.equalsIgnoreCase("justify"))
135                 p.setAlignment(Element.ALIGN_JUSTIFIED);
136         }
137         setParagraphLeading(p, props.getProperty("leading"));
138         value = props.getProperty("before");
139         if (value != null) {
140             try {
141                 p.setSpacingBefore(Float.parseFloat(value));
142             }
143             catch (Exception JavaDoc e) {}
144         }
145         value = props.getProperty("after");
146         if (value != null) {
147             try {
148                 p.setSpacingAfter(Float.parseFloat(value));
149             }
150             catch (Exception JavaDoc e) {}
151         }
152         value = props.getProperty("extraparaspace");
153         if (value != null) {
154             try {
155                 p.setExtraParagraphSpace(Float.parseFloat(value));
156             }
157             catch (Exception JavaDoc e) {}
158         }
159     }
160
161     public static Paragraph createParagraph(ChainedProperties props) {
162         Paragraph p = new Paragraph();
163         createParagraph(p, props);
164         return p;
165     }
166
167     public static ListItem createListItem(ChainedProperties props) {
168         ListItem p = new ListItem();
169         createParagraph(p, props);
170         return p;
171     }
172
173     public Font getFont(ChainedProperties props) {
174         String JavaDoc face = props.getProperty("face");
175         if (face != null) {
176             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(face, ",");
177             while (tok.hasMoreTokens()) {
178                 face = tok.nextToken().trim();
179                 if (face.startsWith("\""))
180                     face = face.substring(1);
181                 if (face.endsWith("\""))
182                     face = face.substring(0, face.length() - 1);
183                 if (fontImp.isRegistered(face))
184                     break;
185             }
186         }
187         int style = 0;
188         if (props.hasProperty("i"))
189             style |= Font.ITALIC;
190         if (props.hasProperty("b"))
191             style |= Font.BOLD;
192         if (props.hasProperty("u"))
193             style |= Font.UNDERLINE;
194         String JavaDoc value = props.getProperty("size");
195         float size = 12;
196         if (value != null)
197             size = Float.parseFloat(value);
198         Color JavaDoc color = Markup.decodeColor(props.getProperty("color"));
199         String JavaDoc encoding = props.getProperty("encoding");
200         if (encoding == null)
201             encoding = BaseFont.WINANSI;
202         return fontImp.getFont(face, encoding, true, size, style, color);
203     }
204     
205     public static void insertStyle(HashMap JavaDoc h) {
206         String JavaDoc style = (String JavaDoc)h.get("style");
207         if (style == null)
208             return;
209         Properties JavaDoc prop = Markup.parseAttributes(style);
210         for (Iterator JavaDoc it = prop.keySet().iterator(); it.hasNext();) {
211             String JavaDoc key = (String JavaDoc)it.next();
212             if (key.equals(Markup.CSS_KEY_FONTFAMILY)) {
213                 h.put("face", prop.getProperty(key));
214             }
215             else if (key.equals(Markup.CSS_KEY_FONTSIZE)) {
216                 h.put("size", Float.toString(Markup.parseLength(prop.getProperty(key))) + "px");
217             }
218             else if (key.equals(Markup.CSS_KEY_FONTSTYLE)) {
219                 String JavaDoc ss = prop.getProperty(key).trim().toLowerCase();
220                 if (ss.equals("italic") || ss.equals("oblique"))
221                     h.put("i", null);
222             }
223             else if (key.equals(Markup.CSS_KEY_FONTWEIGHT)) {
224                 String JavaDoc ss = prop.getProperty(key).trim().toLowerCase();
225                 if (ss.equals("bold") || ss.equals("700") || ss.equals("800") || ss.equals("900"))
226                     h.put("b", null);
227             }
228             else if (key.equals(Markup.CSS_KEY_FONTWEIGHT)) {
229                 String JavaDoc ss = prop.getProperty(key).trim().toLowerCase();
230                 if (ss.equals("underline"))
231                     h.put("u", null);
232             }
233             else if (key.equals(Markup.CSS_KEY_COLOR)) {
234                 Color JavaDoc c = Markup.decodeColor(prop.getProperty(key));
235                 if (c != null) {
236                     int hh = c.getRGB();
237                     String JavaDoc hs = Integer.toHexString(hh);
238                     hs = "000000" + hs;
239                     hs = "#" + hs.substring(hs.length() - 6);
240                     h.put("color", hs);
241                 }
242             }
243             else if (key.equals(Markup.CSS_KEY_LINEHEIGHT)) {
244                 String JavaDoc ss = prop.getProperty(key).trim();
245                 float v = Markup.parseLength(prop.getProperty(key));
246                 if (ss.endsWith("%")) {
247                     h.put("leading", "0," + (v / 100));
248                 }
249                 else {
250                     h.put("leading", v + ",0");
251                 }
252             }
253             else if (key.equals(Markup.CSS_KEY_TEXTALIGN)) {
254                 String JavaDoc ss = prop.getProperty(key).trim().toLowerCase();
255                 h.put("align", ss);
256             }
257         }
258     }
259     
260     public FontFactoryImp getFontImp() {
261         return fontImp;
262     }
263     
264     public void setFontImp(FontFactoryImp fontImp) {
265         this.fontImp = fontImp;
266     }
267
268     public static HashMap JavaDoc followTags = new HashMap JavaDoc();
269     static {
270         followTags.put("i", "i");
271         followTags.put("b", "b");
272         followTags.put("u", "u");
273         followTags.put("sub", "sub");
274         followTags.put("sup", "sup");
275         followTags.put("em", "i");
276         followTags.put("strong", "b");
277     }
278 }
279
Popular Tags