001: /*
002: * $Id: Markup.java 2536 2007-01-19 14:17:01Z blowagie $
003: * $Name$
004: *
005: * Copyright 2001, 2002 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:
051: package com.lowagie.text.html;
052:
053: import java.awt.Color;
054: import java.util.Properties;
055: import java.util.StringTokenizer;
056:
057: /**
058: * A class that contains all the possible tagnames and their attributes.
059: */
060:
061: public class Markup {
062:
063: // iText specific
064:
065: /** the key for any tag */
066: public static final String ITEXT_TAG = "tag";
067:
068: // HTML tags
069:
070: /** the markup for the body part of a file */
071: public static final String HTML_TAG_BODY = "body";
072:
073: /** The DIV tag. */
074: public static final String HTML_TAG_DIV = "div";
075:
076: /** This is a possible HTML-tag. */
077: public static final String HTML_TAG_LINK = "link";
078:
079: /** The SPAN tag. */
080: public static final String HTML_TAG_SPAN = "span";
081:
082: // HTML attributes
083:
084: /** the height attribute. */
085: public static final String HTML_ATTR_HEIGHT = "height";
086:
087: /** the hyperlink reference attribute. */
088: public static final String HTML_ATTR_HREF = "href";
089:
090: /** This is a possible HTML attribute for the LINK tag. */
091: public static final String HTML_ATTR_REL = "rel";
092:
093: /** This is used for inline css style information */
094: public static final String HTML_ATTR_STYLE = "style";
095:
096: /** This is a possible HTML attribute for the LINK tag. */
097: public static final String HTML_ATTR_TYPE = "type";
098:
099: /** This is a possible HTML attribute. */
100: public static final String HTML_ATTR_STYLESHEET = "stylesheet";
101:
102: /** the width attribute. */
103: public static final String HTML_ATTR_WIDTH = "width";
104:
105: /** attribute for specifying externally defined CSS class */
106: public static final String HTML_ATTR_CSS_CLASS = "class";
107:
108: /** The ID attribute. */
109: public static final String HTML_ATTR_CSS_ID = "id";
110:
111: // HTML values
112:
113: /** This is a possible value for the language attribute (SCRIPT tag). */
114: public static final String HTML_VALUE_JAVASCRIPT = "text/javascript";
115:
116: /** This is a possible HTML attribute for the LINK tag. */
117: public static final String HTML_VALUE_CSS = "text/css";
118:
119: // CSS keys
120:
121: /** the CSS tag for background color */
122: public static final String CSS_KEY_BGCOLOR = "background-color";
123:
124: /** the CSS tag for text color */
125: public static final String CSS_KEY_COLOR = "color";
126:
127: /** CSS key that indicate the way something has to be displayed */
128: public static final String CSS_KEY_DISPLAY = "display";
129:
130: /** the CSS tag for the font family */
131: public static final String CSS_KEY_FONTFAMILY = "font-family";
132:
133: /** the CSS tag for the font size */
134: public static final String CSS_KEY_FONTSIZE = "font-size";
135:
136: /** the CSS tag for the font style */
137: public static final String CSS_KEY_FONTSTYLE = "font-style";
138:
139: /** the CSS tag for the font weight */
140: public static final String CSS_KEY_FONTWEIGHT = "font-weight";
141:
142: /** the CSS tag for text decorations */
143: public static final String CSS_KEY_LINEHEIGHT = "line-height";
144:
145: /** the CSS tag for the margin of an object */
146: public static final String CSS_KEY_MARGIN = "margin";
147:
148: /** the CSS tag for the margin of an object */
149: public static final String CSS_KEY_MARGINLEFT = "margin-left";
150:
151: /** the CSS tag for the margin of an object */
152: public static final String CSS_KEY_MARGINRIGHT = "margin-right";
153:
154: /** the CSS tag for the margin of an object */
155: public static final String CSS_KEY_MARGINTOP = "margin-top";
156:
157: /** the CSS tag for the margin of an object */
158: public static final String CSS_KEY_MARGINBOTTOM = "margin-bottom";
159:
160: /** the CSS tag for the margin of an object */
161: public static final String CSS_KEY_PADDING = "padding";
162:
163: /** the CSS tag for the margin of an object */
164: public static final String CSS_KEY_PADDINGLEFT = "padding-left";
165:
166: /** the CSS tag for the margin of an object */
167: public static final String CSS_KEY_PADDINGRIGHT = "padding-right";
168:
169: /** the CSS tag for the margin of an object */
170: public static final String CSS_KEY_PADDINGTOP = "padding-top";
171:
172: /** the CSS tag for the margin of an object */
173: public static final String CSS_KEY_PADDINGBOTTOM = "padding-bottom";
174:
175: /** the CSS tag for the margin of an object */
176: public static final String CSS_KEY_BORDERCOLOR = "border-color";
177:
178: /** the CSS tag for the margin of an object */
179: public static final String CSS_KEY_BORDERWIDTH = "border-width";
180:
181: /** the CSS tag for the margin of an object */
182: public static final String CSS_KEY_BORDERWIDTHLEFT = "border-left-width";
183:
184: /** the CSS tag for the margin of an object */
185: public static final String CSS_KEY_BORDERWIDTHRIGHT = "border-right-width";
186:
187: /** the CSS tag for the margin of an object */
188: public static final String CSS_KEY_BORDERWIDTHTOP = "border-top-width";
189:
190: /** the CSS tag for the margin of an object */
191: public static final String CSS_KEY_BORDERWIDTHBOTTOM = "border-bottom-width";
192:
193: /** the CSS tag for adding a page break when the document is printed */
194: public static final String CSS_KEY_PAGE_BREAK_AFTER = "page-break-after";
195:
196: /** the CSS tag for adding a page break when the document is printed */
197: public static final String CSS_KEY_PAGE_BREAK_BEFORE = "page-break-before";
198:
199: /** the CSS tag for the horizontal alignment of an object */
200: public static final String CSS_KEY_TEXTALIGN = "text-align";
201:
202: /** the CSS tag for text decorations */
203: public static final String CSS_KEY_TEXTDECORATION = "text-decoration";
204:
205: /** the CSS tag for text decorations */
206: public static final String CSS_KEY_VERTICALALIGN = "vertical-align";
207:
208: /** the CSS tag for the visibility of objects */
209: public static final String CSS_KEY_VISIBILITY = "visibility";
210:
211: // CSS values
212:
213: /** value for the CSS tag for adding a page break when the document is printed */
214: public static final String CSS_VALUE_ALWAYS = "always";
215:
216: /** A possible value for the DISPLAY key */
217: public static final String CSS_VALUE_BLOCK = "block";
218:
219: /** a CSS value for text font weight */
220: public static final String CSS_VALUE_BOLD = "bold";
221:
222: /** the value if you want to hide objects. */
223: public static final String CSS_VALUE_HIDDEN = "hidden";
224:
225: /** A possible value for the DISPLAY key */
226: public static final String CSS_VALUE_INLINE = "inline";
227:
228: /** a CSS value for text font style */
229: public static final String CSS_VALUE_ITALIC = "italic";
230:
231: /** a CSS value for text decoration */
232: public static final String CSS_VALUE_LINETHROUGH = "line-through";
233:
234: /** A possible value for the DISPLAY key */
235: public static final String CSS_VALUE_LISTITEM = "list-item";
236:
237: /** a CSS value */
238: public static final String CSS_VALUE_NONE = "none";
239:
240: /** a CSS value */
241: public static final String CSS_VALUE_NORMAL = "normal";
242:
243: /** a CSS value for text font style */
244: public static final String CSS_VALUE_OBLIQUE = "oblique";
245:
246: /** A possible value for the DISPLAY key */
247: public static final String CSS_VALUE_TABLE = "table";
248:
249: /** A possible value for the DISPLAY key */
250: public static final String CSS_VALUE_TABLEROW = "table-row";
251:
252: /** A possible value for the DISPLAY key */
253: public static final String CSS_VALUE_TABLECELL = "table-cell";
254:
255: /** the CSS value for a horizontal alignment of an object */
256: public static final String CSS_VALUE_TEXTALIGNLEFT = "left";
257:
258: /** the CSS value for a horizontal alignment of an object */
259: public static final String CSS_VALUE_TEXTALIGNRIGHT = "right";
260:
261: /** the CSS value for a horizontal alignment of an object */
262: public static final String CSS_VALUE_TEXTALIGNCENTER = "center";
263:
264: /** the CSS value for a horizontal alignment of an object */
265: public static final String CSS_VALUE_TEXTALIGNJUSTIFY = "justify";
266:
267: /** a CSS value for text decoration */
268: public static final String CSS_VALUE_UNDERLINE = "underline";
269:
270: /**
271: * Parses a length.
272: *
273: * @param string
274: * a length in the form of an optional + or -, followed by a
275: * number and a unit.
276: * @return a float
277: */
278:
279: public static float parseLength(String string) {
280: int pos = 0;
281: int length = string.length();
282: boolean ok = true;
283: while (ok && pos < length) {
284: switch (string.charAt(pos)) {
285: case '+':
286: case '-':
287: case '0':
288: case '1':
289: case '2':
290: case '3':
291: case '4':
292: case '5':
293: case '6':
294: case '7':
295: case '8':
296: case '9':
297: case '.':
298: pos++;
299: break;
300: default:
301: ok = false;
302: }
303: }
304: if (pos == 0)
305: return 0f;
306: if (pos == length)
307: return Float.parseFloat(string + "f");
308: float f = Float.parseFloat(string.substring(0, pos) + "f");
309: string = string.substring(pos);
310: // inches
311: if (string.startsWith("in")) {
312: return f * 72f;
313: }
314: // centimeters
315: if (string.startsWith("cm")) {
316: return (f / 2.54f) * 72f;
317: }
318: // millimeters
319: if (string.startsWith("mm")) {
320: return (f / 25.4f) * 72f;
321: }
322: // picas
323: if (string.startsWith("pc")) {
324: return f * 12f;
325: }
326: // default: we assume the length was measured in points
327: return f;
328: }
329:
330: /**
331: * Converts a <CODE>Color</CODE> into a HTML representation of this <CODE>
332: * Color</CODE>.
333: *
334: * @param s
335: * the <CODE>Color</CODE> that has to be converted.
336: * @return the HTML representation of this <COLOR>Color </COLOR>
337: */
338:
339: public static Color decodeColor(String s) {
340: if (s == null)
341: return null;
342: s = s.toLowerCase().trim();
343: Color c = (Color) WebColors.getRGBColor(s);
344: if (c != null)
345: return c;
346: try {
347: if (s.startsWith("#")) {
348: if (s.length() == 4)
349: s = "#" + s.substring(1, 2) + s.substring(1, 2)
350: + s.substring(2, 3) + s.substring(2, 3)
351: + s.substring(3, 4) + s.substring(3, 4);
352: if (s.length() == 7)
353: return new Color(Integer.parseInt(s.substring(1),
354: 16));
355: } else if (s.startsWith("rgb")) {
356: StringTokenizer tk = new StringTokenizer(
357: 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 t = tk.nextToken();
363: float n;
364: if (t.endsWith("%")) {
365: n = Float.parseFloat(t.substring(0,
366: t.length() - 1));
367: n = n * 255f / 100f;
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(cc[0], cc[1], cc[2]);
378: }
379: } catch (Exception e) {
380: }
381: return null;
382: }
383:
384: /**
385: * This method parses a String with attributes and returns a Properties
386: * object.
387: *
388: * @param string
389: * a String of this form: 'key1="value1"; key2="value2";...
390: * keyN="valueN" '
391: * @return a Properties object
392: */
393: public static Properties parseAttributes(String string) {
394: Properties result = new Properties();
395: if (string == null)
396: return result;
397: StringTokenizer keyValuePairs = new StringTokenizer(string, ";");
398: StringTokenizer keyValuePair;
399: String key;
400: String value;
401: while (keyValuePairs.hasMoreTokens()) {
402: keyValuePair = new StringTokenizer(keyValuePairs
403: .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 removeComment(String string,
433: String startComment, String endComment) {
434: StringBuffer result = new StringBuffer();
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: }
|