View Javadoc

1   /*
2    * $Id: HtmlEncoder.java,v 1.1 2003/10/02 13:27:23 oconnor_m Exp $
3    * $Name: JAG_v5_0_rc1 $
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  package com.lowagie.text.html;
51  
52  import java.awt.Color;
53  
54  /***
55   * This class converts a <CODE>String</CODE> to the HTML-format of a String.
56   * <P>
57   * To convert the <CODE>String</CODE>, each character is examined:
58   * <UL>
59   * <LI>ASCII-characters from 000 till 031 are represented as &amp;#xxx;<BR>
60   *     (with xxx = the value of the character)
61   * <LI>ASCII-characters from 032 t/m 127 are represented by the character itself, except for:
62   *     <UL>
63   *     <LI>'\n'becomes &lt;BR&gt;\n
64   *     <LI>&quot; becomes &amp;quot;
65   *     <LI>&amp; becomes &amp;amp;
66   *     <LI>&lt; becomes &amp;lt;
67   *     <LI>&gt; becomes &amp;gt;
68   *     </UL>
69   * <LI>ASCII-characters from 128 till 255 are represented as &amp;#xxx;<BR>
70   *     (with xxx = the value of the character)
71   * </UL>
72   * <P>
73   * Example:
74   * <P><BLOCKQUOTE><PRE>
75   *    String htmlPresentation = HtmlEncoder.encode("Marie-Th&#233;r&#232;se S&#248;rensen");
76   * </PRE></BLOCKQUOTE><P>
77   * for more info: see O'Reilly; "HTML: The Definitive Guide" (page 164)
78   *
79   * @author  mario.maccarini@rug.ac.be
80   */
81  public class HtmlEncoder {
82  
83     // membervariables
84  
85     /*** List with the HTML translation of all the characters. */
86     private static final String[] htmlCode = new String[256];
87  
88     static {
89        for (int i = 0; i < 10; i++) {
90           htmlCode[i] = "&#00" + i + ";";
91        }
92  
93        for (int i = 10; i < 32; i++) {
94           htmlCode[i] = "&#0" + i + ";";
95        }
96  
97        //make all spaces visible
98        htmlCode[32] = "&nbsp;";
99  
100 
101       for (int i = 33; i < 128; i++) {
102          htmlCode[i] = String.valueOf((char) i);
103       }
104 
105       // Special characters
106       htmlCode['\t'] = "\t";
107       htmlCode['\n'] = "<br />\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 
130    // methods
131 
132    /***
133     * Converts a <CODE>String</CODE> to the HTML-format of this <CODE>String</CODE>.
134     *
135     * @param string The <CODE>String</CODE> to convert
136     * @return a <CODE>String</CODE>
137     */
138 
139    public static String encode(String string) {
140       int n = string.length();
141       char character;
142       StringBuffer buffer = new StringBuffer();
143       // loop over all the characters of the String.
144       for (int i = 0; i < n; i++) {
145          character = string.charAt(i);
146          // the Htmlcode of these characters are added to a StringBuffer one by one
147          if (character < 256) {
148             buffer.append(htmlCode[character]);
149          } else {
150             // Improvement posted by Joachim Eyrich
151             buffer.append("&#").append((int) character).append(";");
152          }
153       }
154       return buffer.toString().trim();
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 encode(Color color) {
165       StringBuffer buffer = new StringBuffer("#");
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 }