KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > markup > MarkupParser


1 /*
2  * $Id: MarkupParser.java,v 1.42 2005/03/15 08:17:03 blowagie Exp $
3  * $Name: $
4  *
5  * Copyright 2001, 2002 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
51 package com.lowagie.text.markup;
52
53 import java.awt.Color;
54 import java.util.Properties;
55 import java.util.StringTokenizer;
56
57 import com.lowagie.text.FontFactory;
58
59 /**
60  * This class contains several static methods that can be used to parse markup.
61  *
62  * @author blowagie
63  */

64 public class MarkupParser {
65     
66 /** Creates new MarkupParser */
67     private MarkupParser() {
68     }
69     
70 /**
71  * This method parses a String with attributes and returns a Properties object.
72  *
73  * @param string a String of this form: 'key1="value1"; key2="value2";... keyN="valueN" '
74  * @return a Properties object
75  */

76     
77     public static Properties parseAttributes(String string) {
78         Properties result = new Properties();
79         if (string == null) return result;
80         StringTokenizer keyValuePairs = new StringTokenizer(string, ";");
81         StringTokenizer keyValuePair;
82         String key;
83         String value;
84         while (keyValuePairs.hasMoreTokens()) {
85             keyValuePair = new StringTokenizer(keyValuePairs.nextToken(), ":");
86             if (keyValuePair.hasMoreTokens()) key = keyValuePair.nextToken().trim();
87             else continue;
88             if (keyValuePair.hasMoreTokens()) value = keyValuePair.nextToken().trim();
89             else continue;
90             if (value.startsWith("\"")) value = value.substring(1);
91             if (value.endsWith("\"")) value = value.substring(0, value.length() - 1);
92             result.setProperty(key, value);
93         }
94         return result;
95     }
96 /**
97  * This method parses the value of 'font' attribute and returns a Properties object.
98  *
99  * @param string a String of this form: 'style1 ... styleN size/leading font1 ... fontN'
100  * @return a Properties object
101  */

102     
103     public static Properties parseFont(String string) {
104         Properties result = new Properties();
105         if (string == null) return result;
106         int pos = 0;
107         String value;
108         string = string.trim();
109         while (string.length() > 0) {
110             pos = string.indexOf(" ", pos);
111             if (pos == -1) {
112                 value = string;
113                 string = "";
114             }
115             else {
116                 value = string.substring(0, pos);
117                 string = string.substring(pos).trim();
118             }
119             if (value.equalsIgnoreCase("bold")) {
120                 result.setProperty(MarkupTags.CSS_FONTWEIGHT, MarkupTags.CSS_BOLD);
121                 continue;
122             }
123             if (value.equalsIgnoreCase("italic")) {
124                 result.setProperty(MarkupTags.CSS_FONTSTYLE, MarkupTags.CSS_ITALIC);
125                 continue;
126             }
127             if (value.equalsIgnoreCase("oblique")) {
128                 result.setProperty(MarkupTags.CSS_FONTSTYLE, MarkupTags.CSS_OBLIQUE);
129                 continue;
130             }
131             float f;
132             if ((f = parseLength(value)) > 0) {
133                 result.setProperty(MarkupTags.CSS_FONTSIZE, String.valueOf(f) + "pt");
134                 int p = value.indexOf("/");
135                 if (p > -1 && p < value.length() - 1) {
136                     result.setProperty(MarkupTags.CSS_LINEHEIGHT, String.valueOf(value.substring(p + 1)) + "pt");
137                 }
138             }
139             if (value.endsWith(",")) {
140                 value = value.substring(0, value.length() - 1);
141                 if (FontFactory.contains(value)) {
142                     result.setProperty(MarkupTags.CSS_FONTFAMILY, value);
143                     return result;
144                 }
145             }
146             if ("".equals(string) && FontFactory.contains(value)) {
147                 result.setProperty(MarkupTags.CSS_FONTFAMILY, value);
148             }
149         }
150         return result;
151     }
152     
153 /**
154  * Parses a length.
155  *
156  * @param string a length in the form of an optional + or -, followed by a number and a unit.
157  * @return a float
158  */

159     
160     public static float parseLength(String string) {
161         int pos = 0;
162         int length = string.length();
163         boolean ok = true;
164         while (ok && pos < length) {
165             switch(string.charAt(pos)) {
166                 case '+':
167                 case '-':
168                 case '0':
169                 case '1':
170                 case '2':
171                 case '3':
172                 case '4':
173                 case '5':
174                 case '6':
175                 case '7':
176                 case '8':
177                 case '9':
178                 case '.':
179                     pos++;
180                     break;
181                     default:
182                         ok = false;
183             }
184         }
185         if (pos == 0) return 0f;
186         if (pos == length) return Float.valueOf(string + "f").floatValue();
187         float f = Float.valueOf(string.substring(0, pos) + "f").floatValue();
188         string = string.substring(pos);
189         // inches
190
if (string.startsWith("in")) {
191             return f * 72f;
192         }
193         // centimeters
194
if (string.startsWith("cm")) {
195             return (f / 2.54f) * 72f;
196         }
197         // millimeters
198
if (string.startsWith("mm")) {
199             return (f / 25.4f) * 72f;
200         }
201         // picas
202
if (string.startsWith("pc")) {
203             return f * 12f;
204         }
205         // default: we assume the length was measured in points
206
return f;
207     }
208     
209 /**
210  * Converts a <CODE>Color</CODE> into a HTML representation of this <CODE>Color</CODE>.
211  *
212  * @param color the <CODE>Color</CODE> that has to be converted.
213  * @return the HTML representation of this <COLOR>Color</COLOR>
214  */

215     
216     public static Color decodeColor(String color) {
217         int red = 0;
218         int green = 0;
219         int blue = 0;
220         try {
221             red = Integer.parseInt(color.substring(1, 3), 16);
222             green = Integer.parseInt(color.substring(3, 5), 16);
223             blue = Integer.parseInt(color.substring(5), 16);
224         }
225         catch(Exception sioobe) {
226             // empty on purpose
227
}
228         return new Color(red, green, blue);
229     }
230 }
Popular Tags