KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > html > Markup


1 /*
2  * $Id: Markup.java 2536 2007-01-19 14:17:01Z blowagie $
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.html;
52
53 import java.awt.Color JavaDoc;
54 import java.util.Properties JavaDoc;
55 import java.util.StringTokenizer JavaDoc;
56
57
58 /**
59  * A class that contains all the possible tagnames and their attributes.
60  */

61
62 public class Markup {
63     
64     // iText specific
65

66     /** the key for any tag */
67     public static final String JavaDoc ITEXT_TAG = "tag";
68
69     // HTML tags
70

71     /** the markup for the body part of a file */
72     public static final String JavaDoc HTML_TAG_BODY = "body";
73     
74     /** The DIV tag. */
75     public static final String JavaDoc HTML_TAG_DIV = "div";
76
77     /** This is a possible HTML-tag. */
78     public static final String JavaDoc HTML_TAG_LINK = "link";
79
80     /** The SPAN tag. */
81     public static final String JavaDoc HTML_TAG_SPAN = "span";
82
83     // HTML attributes
84

85     /** the height attribute. */
86     public static final String JavaDoc HTML_ATTR_HEIGHT = "height";
87
88     /** the hyperlink reference attribute. */
89     public static final String JavaDoc HTML_ATTR_HREF = "href";
90
91     /** This is a possible HTML attribute for the LINK tag. */
92     public static final String JavaDoc HTML_ATTR_REL = "rel";
93
94     /** This is used for inline css style information */
95     public static final String JavaDoc HTML_ATTR_STYLE = "style";
96
97     /** This is a possible HTML attribute for the LINK tag. */
98     public static final String JavaDoc HTML_ATTR_TYPE = "type";
99
100     /** This is a possible HTML attribute. */
101     public static final String JavaDoc HTML_ATTR_STYLESHEET = "stylesheet";
102
103     /** the width attribute. */
104     public static final String JavaDoc HTML_ATTR_WIDTH = "width";
105
106     /** attribute for specifying externally defined CSS class */
107     public static final String JavaDoc HTML_ATTR_CSS_CLASS = "class";
108
109     /** The ID attribute. */
110     public static final String JavaDoc HTML_ATTR_CSS_ID = "id";
111
112     // HTML values
113

114     /** This is a possible value for the language attribute (SCRIPT tag). */
115     public static final String JavaDoc HTML_VALUE_JAVASCRIPT = "text/javascript";
116     
117     /** This is a possible HTML attribute for the LINK tag. */
118     public static final String JavaDoc HTML_VALUE_CSS = "text/css";
119
120     // CSS keys
121

122     /** the CSS tag for background color */
123     public static final String JavaDoc CSS_KEY_BGCOLOR = "background-color";
124
125     /** the CSS tag for text color */
126     public static final String JavaDoc CSS_KEY_COLOR = "color";
127
128     /** CSS key that indicate the way something has to be displayed */
129     public static final String JavaDoc CSS_KEY_DISPLAY = "display";
130
131     /** the CSS tag for the font family */
132     public static final String JavaDoc CSS_KEY_FONTFAMILY = "font-family";
133
134     /** the CSS tag for the font size */
135     public static final String JavaDoc CSS_KEY_FONTSIZE = "font-size";
136
137     /** the CSS tag for the font style */
138     public static final String JavaDoc CSS_KEY_FONTSTYLE = "font-style";
139
140     /** the CSS tag for the font weight */
141     public static final String JavaDoc CSS_KEY_FONTWEIGHT = "font-weight";
142
143     /** the CSS tag for text decorations */
144     public static final String JavaDoc CSS_KEY_LINEHEIGHT = "line-height";
145
146     /** the CSS tag for the margin of an object */
147     public static final String JavaDoc CSS_KEY_MARGIN = "margin";
148
149     /** the CSS tag for the margin of an object */
150     public static final String JavaDoc CSS_KEY_MARGINLEFT = "margin-left";
151
152     /** the CSS tag for the margin of an object */
153     public static final String JavaDoc CSS_KEY_MARGINRIGHT = "margin-right";
154
155     /** the CSS tag for the margin of an object */
156     public static final String JavaDoc CSS_KEY_MARGINTOP = "margin-top";
157
158     /** the CSS tag for the margin of an object */
159     public static final String JavaDoc CSS_KEY_MARGINBOTTOM = "margin-bottom";
160
161     /** the CSS tag for the margin of an object */
162     public static final String JavaDoc CSS_KEY_PADDING = "padding";
163
164     /** the CSS tag for the margin of an object */
165     public static final String JavaDoc CSS_KEY_PADDINGLEFT = "padding-left";
166
167     /** the CSS tag for the margin of an object */
168     public static final String JavaDoc CSS_KEY_PADDINGRIGHT = "padding-right";
169
170     /** the CSS tag for the margin of an object */
171     public static final String JavaDoc CSS_KEY_PADDINGTOP = "padding-top";
172
173     /** the CSS tag for the margin of an object */
174     public static final String JavaDoc CSS_KEY_PADDINGBOTTOM = "padding-bottom";
175
176     /** the CSS tag for the margin of an object */
177     public static final String JavaDoc CSS_KEY_BORDERCOLOR = "border-color";
178
179     /** the CSS tag for the margin of an object */
180     public static final String JavaDoc CSS_KEY_BORDERWIDTH = "border-width";
181
182     /** the CSS tag for the margin of an object */
183     public static final String JavaDoc CSS_KEY_BORDERWIDTHLEFT = "border-left-width";
184
185     /** the CSS tag for the margin of an object */
186     public static final String JavaDoc CSS_KEY_BORDERWIDTHRIGHT = "border-right-width";
187
188     /** the CSS tag for the margin of an object */
189     public static final String JavaDoc CSS_KEY_BORDERWIDTHTOP = "border-top-width";
190
191     /** the CSS tag for the margin of an object */
192     public static final String JavaDoc CSS_KEY_BORDERWIDTHBOTTOM = "border-bottom-width";
193
194     /** the CSS tag for adding a page break when the document is printed */
195     public static final String JavaDoc CSS_KEY_PAGE_BREAK_AFTER = "page-break-after";
196
197     /** the CSS tag for adding a page break when the document is printed */
198     public static final String JavaDoc CSS_KEY_PAGE_BREAK_BEFORE = "page-break-before";
199
200     /** the CSS tag for the horizontal alignment of an object */
201     public static final String JavaDoc CSS_KEY_TEXTALIGN = "text-align";
202
203     /** the CSS tag for text decorations */
204     public static final String JavaDoc CSS_KEY_TEXTDECORATION = "text-decoration";
205
206     /** the CSS tag for text decorations */
207     public static final String JavaDoc CSS_KEY_VERTICALALIGN = "vertical-align";
208
209     /** the CSS tag for the visibility of objects */
210     public static final String JavaDoc CSS_KEY_VISIBILITY = "visibility";
211
212     // CSS values
213

214     /** value for the CSS tag for adding a page break when the document is printed */
215     public static final String JavaDoc CSS_VALUE_ALWAYS = "always";
216
217     /** A possible value for the DISPLAY key */
218     public static final String JavaDoc CSS_VALUE_BLOCK = "block";
219
220     /** a CSS value for text font weight */
221     public static final String JavaDoc CSS_VALUE_BOLD = "bold";
222
223     /** the value if you want to hide objects. */
224     public static final String JavaDoc CSS_VALUE_HIDDEN = "hidden";
225
226     /** A possible value for the DISPLAY key */
227     public static final String JavaDoc CSS_VALUE_INLINE = "inline";
228     
229     /** a CSS value for text font style */
230     public static final String JavaDoc CSS_VALUE_ITALIC = "italic";
231
232     /** a CSS value for text decoration */
233     public static final String JavaDoc CSS_VALUE_LINETHROUGH = "line-through";
234
235     /** A possible value for the DISPLAY key */
236     public static final String JavaDoc CSS_VALUE_LISTITEM = "list-item";
237     
238     /** a CSS value */
239     public static final String JavaDoc CSS_VALUE_NONE = "none";
240
241     /** a CSS value */
242     public static final String JavaDoc CSS_VALUE_NORMAL = "normal";
243
244     /** a CSS value for text font style */
245     public static final String JavaDoc CSS_VALUE_OBLIQUE = "oblique";
246
247     /** A possible value for the DISPLAY key */
248     public static final String JavaDoc CSS_VALUE_TABLE = "table";
249
250     /** A possible value for the DISPLAY key */
251     public static final String JavaDoc CSS_VALUE_TABLEROW = "table-row";
252
253     /** A possible value for the DISPLAY key */
254     public static final String JavaDoc CSS_VALUE_TABLECELL = "table-cell";
255
256     /** the CSS value for a horizontal alignment of an object */
257     public static final String JavaDoc CSS_VALUE_TEXTALIGNLEFT = "left";
258
259     /** the CSS value for a horizontal alignment of an object */
260     public static final String JavaDoc CSS_VALUE_TEXTALIGNRIGHT = "right";
261
262     /** the CSS value for a horizontal alignment of an object */
263     public static final String JavaDoc CSS_VALUE_TEXTALIGNCENTER = "center";
264
265     /** the CSS value for a horizontal alignment of an object */
266     public static final String JavaDoc CSS_VALUE_TEXTALIGNJUSTIFY = "justify";
267
268     /** a CSS value for text decoration */
269     public static final String JavaDoc CSS_VALUE_UNDERLINE = "underline";
270
271     /**
272      * Parses a length.
273      *
274      * @param string
275      * a length in the form of an optional + or -, followed by a
276      * number and a unit.
277      * @return a float
278      */

279     
280     public static float parseLength(String JavaDoc string) {
281         int pos = 0;
282         int length = string.length();
283         boolean ok = true;
284         while (ok && pos < length) {
285             switch (string.charAt(pos)) {
286             case '+':
287             case '-':
288             case '0':
289             case '1':
290             case '2':
291             case '3':
292             case '4':
293             case '5':
294             case '6':
295             case '7':
296             case '8':
297             case '9':
298             case '.':
299                 pos++;
300                 break;
301             default:
302                 ok = false;
303             }
304         }
305         if (pos == 0)
306             return 0f;
307         if (pos == length)
308             return Float.parseFloat(string + "f");
309         float f = Float.parseFloat(string.substring(0, pos) + "f");
310         string = string.substring(pos);
311         // inches
312
if (string.startsWith("in")) {
313             return f * 72f;
314         }
315         // centimeters
316
if (string.startsWith("cm")) {
317             return (f / 2.54f) * 72f;
318         }
319         // millimeters
320
if (string.startsWith("mm")) {
321             return (f / 25.4f) * 72f;
322         }
323         // picas
324
if (string.startsWith("pc")) {
325             return f * 12f;
326         }
327         // default: we assume the length was measured in points
328
return f;
329     }
330
331     /**
332      * Converts a <CODE>Color</CODE> into a HTML representation of this <CODE>
333      * Color</CODE>.
334      *
335      * @param s
336      * the <CODE>Color</CODE> that has to be converted.
337      * @return the HTML representation of this <COLOR>Color </COLOR>
338      */

339     
340     public static Color JavaDoc decodeColor(String JavaDoc s) {
341         if (s == null)
342             return null;
343         s = s.toLowerCase().trim();
344         Color JavaDoc c = (Color JavaDoc)WebColors.getRGBColor(s);
345         if (c != null)
346             return c;
347         try {
348             if (s.startsWith("#")) {
349                 if (s.length() == 4)
350                     s = "#" + s.substring(1, 2) + s.substring(1, 2)
351                         + s.substring(2, 3) + s.substring(2, 3)
352                         + s.substring(3, 4) + s.substring(3, 4);
353                 if (s.length() == 7)
354                     return new Color JavaDoc(Integer.parseInt(s.substring(1), 16));
355             }
356             else if (s.startsWith("rgb")) {
357                 StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc(s.substring(3), " \t\r\n\f(),");
358                 int[] cc = new int [3];
359                 for (int k = 0; k < 3; ++k) {
360                     if (!tk.hasMoreTokens())
361                         return null;
362                     String JavaDoc t = tk.nextToken();
363                     float n;
364                     if (t.endsWith("%")) {
365                         n = Float.parseFloat(t.substring(0, t.length() - 1));
366                         n = n * 255f / 100f;
367                     }
368                     else
369                         n = Float.parseFloat(t);
370                     int ni = (int)n;
371                     if (ni > 255)
372                         ni = 255;
373                     else if (ni < 0)
374                         ni = 0;
375                     cc[k] = ni;
376                 }
377                 return new Color JavaDoc(cc[0], cc[1], cc[2]);
378             }
379         }
380         catch (Exception JavaDoc e) {
381         }
382         return null;
383     }
384
385     /**
386      * This method parses a String with attributes and returns a Properties
387      * object.
388      *
389      * @param string
390      * a String of this form: 'key1="value1"; key2="value2";...
391      * keyN="valueN" '
392      * @return a Properties object
393      */

394     public static Properties JavaDoc parseAttributes(String JavaDoc string) {
395         Properties JavaDoc result = new Properties JavaDoc();
396         if (string == null)
397             return result;
398         StringTokenizer JavaDoc keyValuePairs = new StringTokenizer JavaDoc(string, ";");
399         StringTokenizer JavaDoc keyValuePair;
400         String JavaDoc key;
401         String JavaDoc value;
402         while (keyValuePairs.hasMoreTokens()) {
403             keyValuePair = new StringTokenizer JavaDoc(keyValuePairs.nextToken(), ":");
404             if (keyValuePair.hasMoreTokens())
405                 key = keyValuePair.nextToken().trim();
406             else
407                 continue;
408             if (keyValuePair.hasMoreTokens())
409                 value = keyValuePair.nextToken().trim();
410             else
411                 continue;
412             if (value.startsWith("\""))
413                 value = value.substring(1);
414             if (value.endsWith("\""))
415                 value = value.substring(0, value.length() - 1);
416             result.setProperty(key.toLowerCase(), value);
417         }
418         return result;
419     }
420
421     /**
422      * Removes the comments sections of a String.
423      *
424      * @param string
425      * the original String
426      * @param startComment
427      * the String that marks the start of a Comment section
428      * @param endComment
429      * the String that marks the end of a Comment section.
430      * @return the String stripped of its comment section
431      */

432     public static String JavaDoc removeComment(String JavaDoc string, String JavaDoc startComment,
433             String JavaDoc endComment) {
434         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
435         int pos = 0;
436         int end = endComment.length();
437         int start = string.indexOf(startComment, pos);
438         while (start > -1) {
439             result.append(string.substring(pos, start));
440             pos = string.indexOf(endComment, start) + end;
441             start = string.indexOf(startComment, pos);
442         }
443         result.append(string.substring(pos));
444         return result.toString();
445     }
446
447 }
Popular Tags