KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > HtmlUtils


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.core.URLCodec;
23 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * This class provides a set of static methods that provide HTML utility code.
32  */

33 public class HtmlUtils
34 {
35     public static boolean containsHtml(String JavaDoc value)
36     {
37         int numChars = value.length();
38         char c;
39
40         for (int i = 0; i < numChars; i++) {
41             c = value.charAt(i);
42             switch (c) {
43                 case '<':
44                     return true;
45                 case '&':
46                     return true;
47                 case '"':
48                     return true;
49             }
50         }
51         return false;
52
53     }
54
55     /**
56      * Filter the specified value for characters that are sensitive to
57      * HTML interpreters. It will return a string with these characters replaced
58      * with HTML entities. This method calls the overloaded method with <code>markupHTMLSpaceReturn</code>
59      * set to <code>false</code>.
60      * @param value The <code>String</code> value to be filtered and returned.
61      */

62     public static void filter(String JavaDoc value, AbstractRenderAppender results)
63     {
64         filter(value, results, false);
65     }
66
67     /**
68      * Filter the specified string for characters that are sensitive to
69      * HTML interpreters, returning the string with these characters replaced
70      * by the corresponding character entities.
71      * @param value The <code>String</code> value to be filtered and returned.
72      * @param markupHTMLSpaceReturn convert space characters and return characters
73      * to &amp;nbsp; and &lt;br /&gt; marketup for html.
74      */

75     public static void filter(String JavaDoc value, AbstractRenderAppender result, boolean markupHTMLSpaceReturn)
76     {
77         // if the value is null, return
78
if (value == null)
79             return;
80
81         // convert the string
82
int numChars = value.length();
83         char c;
84         char prev = 0;
85
86         for (int i = 0; i < numChars; i++) {
87             c = value.charAt(i);
88             switch (c) {
89                 case '<':
90                     result.append("&lt;");
91                     break;
92                 case '>':
93                     result.append("&gt;");
94                     break;
95                 case '&':
96                     result.append("&amp;");
97                     break;
98                 case '"':
99                     result.append("&quot;");
100                     break;
101                 case '\'':
102                     result.append("&#39;");
103                     break;
104                 case ' ':
105                     if (markupHTMLSpaceReturn) {
106                         if (prev == ' ') {
107                             result.append("&nbsp;");
108                         }
109                         else
110                             result.append(c);
111                     }
112                     else
113                         result.append(c);
114                     break;
115                 case '\n':
116                     if (markupHTMLSpaceReturn) {
117                         result.append("<br />");
118                     }
119                     else
120                         result.append(c);
121                     break;
122                 default:
123                     result.append(c);
124             }
125             prev = c;
126         }
127     }
128
129     public static String JavaDoc escapeEscapes(String JavaDoc val)
130     {
131         assert(val != null);
132         InternalStringBuilder sb = new InternalStringBuilder(val.length());
133         for (int i = 0; i < val.length(); i++) {
134             char c = val.charAt(i);
135             if (c == '"') {
136                 sb.append("&quot;");
137                 continue;
138             }
139             if (c == '\\') {
140                 sb.append("\\\\");
141                 continue;
142             }
143             sb.append(c);
144         }
145         return sb.toString();
146     }
147
148     public static String JavaDoc legacyEscapeEscapes(String JavaDoc val)
149     {
150         assert(val != null);
151         InternalStringBuilder sb = new InternalStringBuilder(val.length());
152         for (int i = 0; i < val.length(); i++) {
153             char c = val.charAt(i);
154             if (c == '"') {
155                 sb.append("\\\"");
156                 continue;
157             }
158             if (c == '\\') {
159                 sb.append("\\\\");
160                 continue;
161             }
162             sb.append(c);
163         }
164         return sb.toString();
165     }
166
167     /**
168      * @param url
169      * @param params
170      * @param encoding
171      * @return String
172      * @throws JspException
173      */

174     public static String JavaDoc addParams(String JavaDoc url, Map JavaDoc params, String JavaDoc encoding)
175             throws JspException JavaDoc
176     {
177         InternalStringBuilder urlBuffer = new InternalStringBuilder(url);
178
179         try {
180             // Add dynamic parameters if requested
181
if ((params != null) && (params.size() > 0)) {
182
183                 // Add the required request parameters
184
boolean question = url.indexOf('?') >= 0;
185                 Iterator JavaDoc keys = params.keySet().iterator();
186                 while (keys.hasNext()) {
187                     String JavaDoc key = (String JavaDoc) keys.next();
188                     Object JavaDoc value = params.get(key);
189                     if (value == null) {
190                         if (!question) {
191                             urlBuffer.append('?');
192                             question = true;
193                         }
194                         else {
195                             urlBuffer.append("&");
196                         }
197                         urlBuffer.append(URLCodec.encode(key, encoding));
198                         urlBuffer.append('='); // Interpret null as "no value"
199
}
200                     else if (value instanceof String JavaDoc) {
201                         if (!question) {
202                             urlBuffer.append('?');
203                             question = true;
204                         }
205                         else {
206                             urlBuffer.append("&");
207                         }
208                         urlBuffer.append(URLCodec.encode(key, encoding));
209                         urlBuffer.append('=');
210                         urlBuffer.append(URLCodec.encode((String JavaDoc) value, encoding));
211                     }
212                     else if (value instanceof String JavaDoc[]) {
213                         String JavaDoc values[] = (String JavaDoc[]) value;
214                         for (int i = 0; i < values.length; i++) {
215                             if (!question) {
216                                 urlBuffer.append('?');
217                                 question = true;
218                             }
219                             else {
220                                 urlBuffer.append("&");
221                             }
222                             urlBuffer.append(URLCodec.encode(key, encoding));
223                             urlBuffer.append('=');
224                             urlBuffer.append(URLCodec.encode(values[i], encoding));
225                         }
226                     }
227                     else /* Convert other objects to a string */ {
228                         if (!question) {
229                             urlBuffer.append('?');
230                             question = true;
231                         }
232                         else {
233                             urlBuffer.append("&");
234                         }
235                         urlBuffer.append(URLCodec.encode(key, encoding));
236                         urlBuffer.append('=');
237                         urlBuffer.append(URLCodec.encode(value.toString(),
238                                 encoding));
239                     }
240                 }
241             }
242         }
243         catch (UnsupportedEncodingException JavaDoc uee) {
244             uee.printStackTrace();
245             throw new JspException JavaDoc("Unsupported Encoding" + encoding, uee);
246         }
247
248         return urlBuffer.toString();
249     }
250
251     /**
252      * This method will determine if the value passed in contains an entity.
253      * @return boolean
254      */

255     public static boolean containsEntity(String JavaDoc value)
256     {
257         assert (value != null) : "Parameter 'value' must not be null";
258
259         int pos = value.indexOf('&');
260         if (pos == -1)
261             return false;
262
263         int end = value.indexOf(';');
264         if (end != -1 && pos < end) {
265             // extract the entity and then verify it is
266
// a valid unicode identifier.
267
String JavaDoc entity = value.substring(pos + 1, end);
268             if (entity.length() == 0)
269                 return false;
270             char[] chars = entity.toCharArray();
271
272             // verify the start is an indentifier start
273
// and the rest is a part.
274
if (!Character.isUnicodeIdentifierStart(chars[0])) {
275                 if (chars[0] == '#' && chars.length > 1) {
276                     for (int i = 1; i < chars.length; i++) {
277                         if (!Character.isDigit(chars[i]))
278                             return false;
279                     }
280                     return true;
281                 }
282                 return false;
283             }
284             for (int i = 1; i < chars.length; i++) {
285                 if (!Character.isUnicodeIdentifierPart(chars[i]))
286                     return false;
287             }
288             // good indentifier
289
return true;
290         }
291         return false;
292     }
293 }
294
Popular Tags