01: /*
02: * Copyright 2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package demo.cart;
18:
19: /**
20: * HTML filter utility.
21: *
22: * @author Craig R. McClanahan
23: * @author Tim Tye
24: * @version $Revision: 1 $ $Date: 2006-11-27 16:46:47 -0800 (Mon, 27 Nov 2006) $
25: */
26:
27: public final class HTMLFilter {
28:
29: /**
30: * Filter the specified message string for characters that are sensitive
31: * in HTML. This avoids potential attacks caused by including JavaScript
32: * codes in the request URL that is often reported in error messages.
33: *
34: * @param message The message string to be filtered
35: */
36: public static String filter(String message) {
37:
38: if (message == null)
39: return (null);
40:
41: char content[] = new char[message.length()];
42: message.getChars(0, message.length(), content, 0);
43: StringBuffer result = new StringBuffer(content.length + 50);
44: for (int i = 0; i < content.length; i++) {
45: switch (content[i]) {
46: case '<':
47: result.append("<");
48: break;
49: case '>':
50: result.append(">");
51: break;
52: case '&':
53: result.append("&");
54: break;
55: case '"':
56: result.append(""");
57: break;
58: default:
59: result.append(content[i]);
60: }
61: }
62: return (result.toString());
63:
64: }
65:
66: }
|