KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > util > HtmlCodec


1 /*
2  * $$Id: HtmlCodec.java,v 1.3 2005/06/07 12:32:27 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.jresearch.gossip.util;
26
27 import java.io.UnsupportedEncodingException JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29 import java.security.AccessController JavaDoc;
30
31 import sun.security.action.GetPropertyAction;
32
33 /**
34  * DOCUMENT ME!
35  *
36  * @author $author$
37  * @version $Revision: 1.3 $
38  */

39 public class HtmlCodec {
40     /**
41      * It is the default array of symbols to be encoded into the html form.
42      */

43     public static char[] defSpecSymbols = new char[] { '"', '\'', '\\', ',',
44             '<', '>', '(', ')', '.' };
45
46     /**
47      * This method is a wrapper for the <code>encode(String text , char[]
48      * specSymbols)</code>
49      * method. It uses the defSpecialSymbols array as special symbols.
50      *
51      * @param text
52      * It is the string to be encoded using the default array of
53      * special symbols.
54      *
55      * @return the encoded representation of the "text" parameter
56      */

57     public static String JavaDoc encode(final String JavaDoc text) {
58         return encode(text, defSpecSymbols);
59     }
60
61     /**
62      * This method is a wrapper for the <code>encode(String text , char[]
63      * specSymbols)</code>
64      * method. It allows to specify special symbols as elements of the String
65      * object ("String specSymbols" parameter).
66      *
67      * @param text
68      * It is the string to be encoded using special symbols from the
69      * "specSymbols" parameter.
70      * @param specSymbols
71      * It is the set of special symbols to be encoded within the
72      * "text" parameter.
73      *
74      * @return the encoded representation of the "text" parameter
75      */

76     public static String JavaDoc encode(final String JavaDoc text, final String JavaDoc specSymbols) {
77         final char[] cSpecSymbols;
78
79         if ((specSymbols == null) || (specSymbols.length() == 0)) {
80             cSpecSymbols = defSpecSymbols;
81         } else {
82             cSpecSymbols = specSymbols.toCharArray();
83         }
84
85         return encode(text, cSpecSymbols);
86     }
87
88     /**
89      * It is the main encoding method. It encodes all special symbols in the
90      * "text" parameter, using the specSymbols parameter as an array of special
91      * symbols to be changed. AlSpecial symbols are translated to the "&#xxx;"
92      * code, where the "xxx" part is the decimal code of the correspondent
93      * special symbol.
94      *
95      * @param text
96      * It is the string to be encoded using special symbols from the
97      * "specSymbols" array.
98      * @param specSymbols
99      * It is the array of special symbols to be encoded within the
100      * "text" parameter.
101      *
102      * @return the encoded representation of the "text" parameter
103      */

104     public static String JavaDoc encode(final String JavaDoc text, char[] specSymbols) {
105         if (text == null) {
106             return null;
107         }
108
109         if ((specSymbols == null) || (specSymbols.length == 0)) {
110             specSymbols = defSpecSymbols;
111         }
112
113         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(text.length());
114         char[] cText = text.toCharArray();
115
116         for (int i = 0; i < cText.length; i++) {
117             char cTextChar = cText[i]; // Speed up acess speed
118
boolean isSpecial = false;
119
120             for (int ss = 0; ss < specSymbols.length; ss++) {
121                 if (specSymbols[ss] == cTextChar) {
122                     isSpecial = true;
123                     buffer.append('&');
124                     buffer.append('#');
125                     buffer.append((int) cTextChar);
126                     buffer.append(';');
127
128                     break; // Exit the for cycle
129
}
130             }
131
132             if (!isSpecial) {
133                 buffer.append(cTextChar);
134             }
135         }
136
137         return buffer.toString();
138     }
139
140     /**
141      * DOCUMENT ME!
142      *
143      * @param string
144      * DOCUMENT ME!
145      *
146      * @return DOCUMENT ME!
147      */

148     public static String JavaDoc trim(String JavaDoc string) {
149         return string.replaceAll("&nbsp;", " ").replaceAll("&#xA0;", " ")
150                 .trim();
151     }
152
153     /**
154      * It is a wrapper method for converting URL strings into the
155      * "x-www-form-urlencoded" MIME format. This method is placed here to
156      * standardize html encoding only, to access all functionality via one
157      * class.
158      *
159      * @param url
160      * It is the string to be encoded using the standard
161      * <code>{@link java.net.URLEncoder}</code> class.
162      *
163      * @return DOCUMENT ME!
164      */

165     public static String JavaDoc encodeURL(final String JavaDoc url) {
166         try {
167             return URLEncoder.encode(url, (String JavaDoc) AccessController
168                     .doPrivileged(new GetPropertyAction("file.encoding")));
169         } catch (UnsupportedEncodingException JavaDoc e) {
170             e.printStackTrace();
171
172             return url;
173         }
174     }
175 }
176
Popular Tags