2 * This file is part of "SnipSnap Radeox Rendering Engine".
4 * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
7 * Please visit http://radeox.org/ for updates and contact.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 package org.plog4u.wiki.filter;
28 import org.radeox.regex.Pattern;
29 import org.radeox.regex.Matcher;
30 import org.radeox.regex.Substitution;
31 import org.radeox.regex.MatchResult;
33 import java.util.HashMap;
35 import java.util.StringTokenizer;
38 * Escapes and encodes Strings for web usage
41 * @version $Id: Encoder.java,v 1.1 2005-02-19 00:19:41 axelcl Exp $
44 public class Encoder {
45 private final static String DELIMITER = "&\"'<>";
46 private final static Map ESCAPED_CHARS = new HashMap();
47 // private final static Pattern entityPattern = Pattern.compile("&(#?[0-9a-fA-F]+);");
50 // ESCAPED_CHARS.put("&", toEntity('&'));
51 // ESCAPED_CHARS.put("\"", toEntity('"'));
52 // ESCAPED_CHARS.put("'", toEntity('\''));
53 // ESCAPED_CHARS.put(">", toEntity('>'));
54 // ESCAPED_CHARS.put("<", toEntity('<'));
58 ESCAPED_CHARS.put("&", "&");
59 ESCAPED_CHARS.put("\"", """);
60 ESCAPED_CHARS.put("'", toEntity('\''));
61 ESCAPED_CHARS.put(">", ">");
62 ESCAPED_CHARS.put("<", "<");
65 * Encoder special characters that may occur in a HTML so it can be displayed
67 * @param str the original string
68 * @return the escaped string
70 public static String escape(String str) {
71 if (str==null || str.equals("")) {
74 StringBuffer result = new StringBuffer();
75 StringTokenizer tokenizer = new StringTokenizer(str, DELIMITER, true);
76 while(tokenizer.hasMoreTokens()) {
77 String currentToken = tokenizer.nextToken();
78 if(ESCAPED_CHARS.containsKey(currentToken)) {
79 result.append(ESCAPED_CHARS.get(currentToken));
81 result.append(currentToken);
84 return result.toString();
87 public static String unescape(String str) {
88 StringBuffer result = new StringBuffer();
90 org.radeox.regex.Compiler compiler = org.radeox.regex.Compiler.create();
91 Pattern entityPattern = compiler.compile("&(#?[0-9a-fA-F]+);");
93 Matcher matcher = Matcher.create(str, entityPattern);
94 result.append(matcher.substitute(new Substitution() {
95 public void handleMatch(StringBuffer buffer, MatchResult result) {
96 buffer.append(toChar(result.group(1)));
99 return result.toString();
102 public static String toEntity(int c) {
103 return "&#" + c + ";";
106 public static char toChar(String number) {
107 return (char) Integer.decode(number.substring(1)).intValue();