KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > util > HTMLFilter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25
26 package util;
27
28 /**
29  * HTML filter utility.
30  *
31  * @author Craig R. McClanahan
32  * @author Tim Tye
33  * @version $Revision: 1.2 $ $Date: 2005/12/08 01:13:54 $
34  */

35
36 public final class HTMLFilter {
37
38
39     /**
40      * Filter the specified message string for characters that are sensitive
41      * in HTML. This avoids potential attacks caused by including JavaScript
42      * codes in the request URL that is often reported in error messages.
43      *
44      * @param message The message string to be filtered
45      */

46     public static String JavaDoc filter(String JavaDoc message) {
47
48         if (message == null)
49             return (null);
50
51         char content[] = new char[message.length()];
52         message.getChars(0, message.length(), content, 0);
53         StringBuffer JavaDoc result = new StringBuffer JavaDoc(content.length + 50);
54         for (int i = 0; i < content.length; i++) {
55             switch (content[i]) {
56             case '<':
57                 result.append("&lt;");
58                 break;
59             case '>':
60                 result.append("&gt;");
61                 break;
62             case '&':
63                 result.append("&amp;");
64                 break;
65             case '"':
66                 result.append("&quot;");
67                 break;
68             default:
69                 result.append(content[i]);
70             }
71         }
72         return (result.toString());
73
74     }
75
76
77 }
78
79
Popular Tags