001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package org.radeox.util;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import org.radeox.regex.MatchResult;
030: import org.radeox.regex.Matcher;
031: import org.radeox.regex.Pattern;
032: import org.radeox.regex.Substitution;
033:
034: /*
035: * Escapes and encodes Strings for web usage @author stephan
036: *
037: * @version $Id: Encoder.java 22191 2007-03-05 19:50:30Z ian@caret.cam.ac.uk $
038: */
039:
040: public class Encoder {
041:
042: public static final char HIGHEST_CHARACTER = '~';
043:
044: public static final char[][] specialChars = new char[HIGHEST_CHARACTER + 1][];
045: static {
046: specialChars['>'] = ">".toCharArray();
047: specialChars['<'] = "<".toCharArray();
048: specialChars['&'] = "&".toCharArray();
049: specialChars['"'] = """.toCharArray();
050: specialChars['\''] = "'".toCharArray();
051: specialChars['['] = "[".toCharArray();
052: specialChars[']'] = "]".toCharArray();
053: specialChars['_'] = "_".toCharArray();
054: specialChars['-'] = "-".toCharArray();
055: specialChars['~'] = "~".toCharArray();
056: }
057: private final static Map<String, String> ESCAPED_CHARS = new HashMap<String, String>();
058: static {
059: ESCAPED_CHARS.put(">", ">");
060: ESCAPED_CHARS.put("<", "<");
061: ESCAPED_CHARS.put("&", "&");
062: ESCAPED_CHARS.put(""", "\"");
063: ESCAPED_CHARS.put("'", "'");
064: ESCAPED_CHARS.put("[", "[");
065: ESCAPED_CHARS.put("]", "]");
066: ESCAPED_CHARS.put("_", "_");
067: ESCAPED_CHARS.put("-", "-");
068: ESCAPED_CHARS.put("~", "~");
069: }
070:
071: public static String escape(String toEscape) {
072: char[] chars = toEscape.toCharArray();
073: int lastEscapedBefore = 0;
074: StringBuffer escapedString = null;
075: for (int i = 0; i < chars.length; i++) {
076: if (chars[i] <= HIGHEST_CHARACTER) {
077: char[] escapedPortion = specialChars[chars[i]];
078: if (escapedPortion != null) {
079: if (lastEscapedBefore == 0) {
080: escapedString = new StringBuffer(
081: chars.length + 5);
082: }
083: if (lastEscapedBefore < i) {
084: escapedString.append(chars, lastEscapedBefore,
085: i - lastEscapedBefore);
086: }
087: lastEscapedBefore = i + 1;
088: escapedString.append(escapedPortion);
089: }
090: }
091: }
092:
093: if (lastEscapedBefore == 0) {
094: return toEscape;
095: }
096:
097: if (lastEscapedBefore < chars.length) {
098: escapedString.append(chars, lastEscapedBefore, chars.length
099: - lastEscapedBefore);
100: }
101:
102: return escapedString.toString();
103: }
104:
105: // private final static Pattern entityPattern =
106: // Pattern.compile("&(#?[0-9a-fA-F]+);");
107:
108: public static String unescape(String str) {
109: StringBuffer result = new StringBuffer();
110:
111: org.radeox.regex.Compiler compiler = org.radeox.regex.Compiler
112: .create();
113: Pattern entityPattern = compiler.compile("&(#?[0-9a-fA-F]+);");
114:
115: Matcher matcher = Matcher.create(str, entityPattern);
116: result.append(matcher.substitute(new Substitution() {
117: public void handleMatch(StringBuffer buffer,
118: MatchResult result) {
119: buffer.append(toChar(result.group(1)));
120: }
121: }));
122: return result.toString();
123: }
124:
125: public static String toEntity(int c) {
126: if (c <= HIGHEST_CHARACTER) {
127: char[] escapedPortion = specialChars[c];
128: if (escapedPortion != null) {
129: return new String(escapedPortion);
130: }
131: }
132: return "&#" + c + ";";
133: }
134:
135: public static char toChar(String number) {
136: return (char) Integer.decode(number.substring(1)).intValue();
137: }
138: }
|