KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: HtmlEncoder.java 2547 2007-01-26 13:46:13Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 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
55 import com.lowagie.text.Element;
56
57 /**
58  * This class converts a <CODE>String</CODE> to the HTML-format of a String.
59  * <P>
60  * To convert the <CODE>String</CODE>, each character is examined:
61  * <UL>
62  * <LI>ASCII-characters from 000 till 031 are represented as &amp;#xxx;<BR>
63  * (with xxx = the value of the character)
64  * <LI>ASCII-characters from 032 t/m 127 are represented by the character itself, except for:
65  * <UL>
66  * <LI>'\n' becomes &lt;BR&gt;\n
67  * <LI>&quot; becomes &amp;quot;
68  * <LI>&amp; becomes &amp;amp;
69  * <LI>&lt; becomes &amp;lt;
70  * <LI>&gt; becomes &amp;gt;
71  * </UL>
72  * <LI>ASCII-characters from 128 till 255 are represented as &amp;#xxx;<BR>
73  * (with xxx = the value of the character)
74  * </UL>
75  * <P>
76  * Example:
77  * <P><BLOCKQUOTE><PRE>
78  * String htmlPresentation = HtmlEncoder.encode("Marie-Th&#233;r&#232;se S&#248;rensen");
79  * </PRE></BLOCKQUOTE><P>
80  * for more info: see O'Reilly; "HTML: The Definitive Guide" (page 164)
81  *
82  * @author mario.maccarini@ugent.be
83  */

84
85 public class HtmlEncoder {
86     
87     // membervariables
88

89 /** List with the HTML translation of all the characters. */
90     private static final String JavaDoc[] htmlCode = new String JavaDoc[256];
91     
92     static {
93         for (int i = 0; i < 10; i++) {
94             htmlCode[i] = "&#00" + i + ";";
95         }
96         
97         for (int i = 10; i < 32; i++) {
98             htmlCode[i] = "&#0" + i + ";";
99         }
100         
101         for (int i = 32; i < 128; i++) {
102             htmlCode[i] = String.valueOf((char)i);
103         }
104         
105         // Special characters
106
htmlCode['\t'] = "\t";
107         htmlCode['\n'] = "<" + HtmlTags.NEWLINE + " />\n";
108         htmlCode['\"'] = "&quot;"; // double quote
109
htmlCode['&'] = "&amp;"; // ampersand
110
htmlCode['<'] = "&lt;"; // lower than
111
htmlCode['>'] = "&gt;"; // greater than
112

113         for (int i = 128; i < 256; i++) {
114             htmlCode[i] = "&#" + i + ";";
115         }
116     }
117     
118     
119     // constructors
120

121 /**
122  * This class will never be constructed.
123  * <P>
124  * HtmlEncoder only contains static methods.
125  */

126     
127     private HtmlEncoder () { }
128     
129     // methods
130

131 /**
132  * Converts a <CODE>String</CODE> to the HTML-format of this <CODE>String</CODE>.
133  *
134  * @param string The <CODE>String</CODE> to convert
135  * @return a <CODE>String</CODE>
136  */

137     
138     public static String JavaDoc encode(String JavaDoc string) {
139         int n = string.length();
140         char character;
141         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
142         // loop over all the characters of the String.
143
for (int i = 0; i < n; i++) {
144             character = string.charAt(i);
145             // the Htmlcode of these characters are added to a StringBuffer one by one
146
if (character < 256) {
147                 buffer.append(htmlCode[character]);
148             }
149             else {
150                 // Improvement posted by Joachim Eyrich
151
buffer.append("&#").append((int)character).append(';');
152             }
153         }
154         return buffer.toString();
155     }
156     
157 /**
158  * Converts a <CODE>Color</CODE> into a HTML representation of this <CODE>Color</CODE>.
159  *
160  * @param color the <CODE>Color</CODE> that has to be converted.
161  * @return the HTML representation of this <COLOR>Color</COLOR>
162  */

163     
164     public static String JavaDoc encode(Color JavaDoc color) {
165         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("#");
166         if (color.getRed() < 16) {
167             buffer.append('0');
168         }
169         buffer.append(Integer.toString(color.getRed(), 16));
170         if (color.getGreen() < 16) {
171             buffer.append('0');
172         }
173         buffer.append(Integer.toString(color.getGreen(), 16));
174         if (color.getBlue() < 16) {
175             buffer.append('0');
176         }
177         buffer.append(Integer.toString(color.getBlue(), 16));
178         return buffer.toString();
179     }
180     
181 /**
182  * Translates the alignment value.
183  *
184  * @param alignment the alignment value
185  * @return the translated value
186  */

187     
188     public static String JavaDoc getAlignment(int alignment) {
189         switch(alignment) {
190             case Element.ALIGN_LEFT:
191                 return HtmlTags.ALIGN_LEFT;
192             case Element.ALIGN_CENTER:
193                 return HtmlTags.ALIGN_CENTER;
194             case Element.ALIGN_RIGHT:
195                 return HtmlTags.ALIGN_RIGHT;
196             case Element.ALIGN_JUSTIFIED:
197             case Element.ALIGN_JUSTIFIED_ALL:
198                 return HtmlTags.ALIGN_JUSTIFIED;
199             case Element.ALIGN_TOP:
200                 return HtmlTags.ALIGN_TOP;
201             case Element.ALIGN_MIDDLE:
202                 return HtmlTags.ALIGN_MIDDLE;
203             case Element.ALIGN_BOTTOM:
204                 return HtmlTags.ALIGN_BOTTOM;
205             case Element.ALIGN_BASELINE:
206                 return HtmlTags.ALIGN_BASELINE;
207                 default:
208                     return "";
209         }
210     }
211 }
Popular Tags