Improved rendering
[phpeclipse.git] / archive / org.plog4u.wiki / src / org / plog4u / wiki / filter / Encoder.java
1 /*
2  * This file is part of "SnipSnap Radeox Rendering Engine".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://radeox.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
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.
14  *
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.
19  *
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
23  * --LICENSE NOTICE--
24  */
25
26 package org.plog4u.wiki.filter;
27
28 import org.radeox.regex.Pattern;
29 import org.radeox.regex.Matcher;
30 import org.radeox.regex.Substitution;
31 import org.radeox.regex.MatchResult;
32
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.StringTokenizer;
36
37 /*
38  * Escapes and encodes Strings for web usage
39  *
40  * @author stephan
41  * @version $Id: Encoder.java,v 1.1 2005-02-19 00:19:41 axelcl Exp $
42  */
43
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]+);");
48
49 //  static {
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('<'));
55 //  }
56
57   static {
58     ESCAPED_CHARS.put("&", "&amp;");
59     ESCAPED_CHARS.put("\"", "&quot;");
60     ESCAPED_CHARS.put("'", toEntity('\''));
61     ESCAPED_CHARS.put(">", "&gt;");
62     ESCAPED_CHARS.put("<", "&lt;");
63   }
64   /**
65    * Encoder special characters that may occur in a HTML so it can be displayed
66    * safely.
67    * @param str the original string
68    * @return the escaped string
69    */
70   public static String escape(String str) {
71     if (str==null || str.equals("")) {
72       return "";
73     }
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));
80       } else {
81         result.append(currentToken);
82       }
83     }
84     return result.toString();
85   }
86
87   public static String unescape(String str) {
88     StringBuffer result = new StringBuffer();
89
90     org.radeox.regex.Compiler compiler = org.radeox.regex.Compiler.create();
91     Pattern entityPattern = compiler.compile("&(#?[0-9a-fA-F]+);");
92
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)));
97       }
98     }));
99     return result.toString();
100   }
101   
102   public static String toEntity(int c) {
103     return "&#" + c + ";";
104   }
105
106   public static char toChar(String number) {
107     return (char) Integer.decode(number.substring(1)).intValue();
108   }
109 }