001: /**
002: *
003: */package org.sakaiproject.search.component.service.impl;
004:
005: import org.apache.commons.logging.Log;
006: import org.apache.commons.logging.LogFactory;
007:
008: /**
009: * @author ieb
010: *
011: */
012: public class StringUtils {
013:
014: private static final Log log = LogFactory.getLog(StringUtils.class);
015:
016: /**
017: * This is a copy of the method in FormattedText... its here because FormattedText
018: * depends on the component manager, and I need to run simple unit tests
019: * @param value
020: * @param escapeNewlines
021: * @return
022: */
023: public static String escapeHtml(String value, boolean escapeNewlines) {
024: if (value == null)
025: return "";
026:
027: try {
028: // lazily allocate the StringBuffer
029: // only if changes are actually made; otherwise
030: // just return the given string without changing it.
031: StringBuffer buf = null;
032: final int len = value.length();
033: for (int i = 0; i < len; i++) {
034: char c = value.charAt(i);
035: switch (c) {
036: case '<': {
037: if (buf == null)
038: buf = new StringBuffer(value.substring(0, i));
039: buf.append("<");
040: }
041: break;
042:
043: case '>': {
044: if (buf == null)
045: buf = new StringBuffer(value.substring(0, i));
046: buf.append(">");
047: }
048: break;
049:
050: case '&': {
051: if (buf == null)
052: buf = new StringBuffer(value.substring(0, i));
053: buf.append("&");
054: }
055: break;
056:
057: case '"': {
058: if (buf == null)
059: buf = new StringBuffer(value.substring(0, i));
060: buf.append(""");
061: }
062: break;
063: case '\n': {
064: if (escapeNewlines) {
065: if (buf == null)
066: buf = new StringBuffer(value
067: .substring(0, i));
068: buf.append("<br />\n");
069: } else {
070: if (buf != null)
071: buf.append(c);
072: }
073: }
074: break;
075: default: {
076: if (c < 128) {
077: if (buf != null)
078: buf.append(c);
079: } else {
080: // escape higher Unicode characters using an
081: // HTML numeric character entity reference like "㴸"
082: if (buf == null)
083: buf = new StringBuffer(value
084: .substring(0, i));
085: buf.append("&#");
086: buf.append(Integer.toString((int) c));
087: buf.append(";");
088: }
089: }
090: break;
091: }
092: } // for
093:
094: return (buf == null) ? value : buf.toString();
095: } catch (Exception e) {
096: log.warn("Validator.escapeHtml: ", e);
097: return value;
098: }
099:
100: } // escapeHtml
101:
102: public static final char HIGHEST_CHARACTER = '>';
103:
104: public static final char[][] specialChars = new char[HIGHEST_CHARACTER + 1][];
105: static {
106: specialChars['>'] = ">".toCharArray();
107: specialChars['<'] = "<".toCharArray();
108: specialChars['&'] = "&".toCharArray();
109: specialChars['"'] = """.toCharArray();
110: specialChars['\''] = "'".toCharArray();
111: }
112:
113: public static String xmlEscape(String toEscape) {
114: char[] chars = toEscape.toCharArray();
115: int lastEscapedBefore = 0;
116: StringBuffer escapedString = null;
117: for (int i = 0; i < chars.length; i++) {
118: if (chars[i] <= HIGHEST_CHARACTER) {
119: char[] escapedPortion = specialChars[chars[i]];
120: if (escapedPortion != null) {
121: if (lastEscapedBefore == 0) {
122: escapedString = new StringBuffer(
123: chars.length + 5);
124: }
125: if (lastEscapedBefore < i) {
126: escapedString.append(chars, lastEscapedBefore,
127: i - lastEscapedBefore);
128: }
129: lastEscapedBefore = i + 1;
130: escapedString.append(escapedPortion);
131: }
132: }
133: }
134:
135: if (lastEscapedBefore == 0) {
136: return toEscape;
137: }
138:
139: if (lastEscapedBefore < chars.length) {
140: escapedString.append(chars, lastEscapedBefore, chars.length
141: - lastEscapedBefore);
142: }
143:
144: return escapedString.toString();
145: }
146:
147: }
|