01: /**
02: * Copyright 2006 Webmedia Group Ltd.
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: **/package org.araneaframework.jsp.util;
16:
17: import org.apache.commons.lang.StringEscapeUtils;
18:
19: /**
20: * Some simple utility routines for Strings.
21: *
22: * @author Konstantin Tretyakov
23: */
24: public abstract class JspStringUtil {
25: /**
26: * Given a string and an accesskey, finds the first appearance of
27: * accessKey in that string, and surrounds it with <u>…</u>.
28: * Returns the result.
29: *
30: * If accessKey is null, is not present in the string or its length is not 1,
31: * returns the string unmodified.
32: *
33: * Assumes that given string has no HTML elements in it. Does case-insensitive matching.
34: *
35: * @param s String in which the access key is to be underlined. May be null.
36: * @param accessKey Access key to underline. May be null.
37: * @return Given string with accesskey underlined (if possible).
38: */
39: public static String underlineAccessKey(String s, String accessKey) {
40: if (s == null || accessKey == null || accessKey.length() != 1)
41: return s;
42: char lo = accessKey.toLowerCase().charAt(0);
43: char hi = accessKey.toUpperCase().charAt(0);
44: int pos = -1;
45: for (int i = 0; i < s.length(); i++) {
46: if (s.charAt(i) == lo || s.charAt(i) == hi) {
47: pos = i;
48: break;
49: }
50: }
51: if (pos == -1)
52: return s;
53: String result = s.substring(0, pos) + "<u>" + s.charAt(pos)
54: + "</u>" + s.substring(pos + 1);
55: return result;
56: }
57:
58: public static String escapeHtmlEntities(String s) {
59: return StringEscapeUtils.escapeHtml(s);
60: }
61: }
|