KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > util > EntityDecoder


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.util;
19
20 import java.util.StringTokenizer JavaDoc;
21
22 public class EntityDecoder {
23     private EntityDecoder () {
24         // not instanciable
25
}
26     
27     public final static String JavaDoc htmlEntities[] = { "€", "", "‚",
28             "ƒ", "„", "…", "†", "‡", "ˆ",
29             "‰", "Š", "‹", "Œ", "", "Ž", "",
30             "", "‘", "’", "“", "”", "•",
31             "–", "—", "˜", "™", "š", "›",
32             "œ", "", "ž", "Ÿ", " ", "¡", "¢",
33             "£", "¤", "¥", "¦", "§", "¨",
34             "©", "ª", "«", "¬", "­", "®", "¯",
35             "°", "±", "²", "³", "´", "µ",
36             "¶", "·", "¸", "¹", "º", "»",
37             "¼", "½", "¾", "¿", "À",
38             "Á", "Â", "Ã", "Ä", "Å", "Æ",
39             "Ç", "È", "É", "Ê", "Ë",
40             "Ì", "Í", "Î", "Ï", "Ð", "Ñ",
41             "Ò", "Ó", "Ô", "Õ", "Ö", "×",
42             "Ø", "Ù", "Ú", "Û", "Ü",
43             "Ý", "Þ", "ß", "à", "á",
44             "â", "ã", "ä", "å", "æ", "ç",
45             "è", "é", "ê", "ë", "ì",
46             "í", "î", "ï", "ð", "ñ", "ò",
47             "ó", "ô", "õ", "ö", "÷",
48             "ø", "ù", "ú", "û", "ü",
49             "ý", "þ", "ÿ"};
50     
51     public final static String JavaDoc entities[] = { "F6", "E4", "FC", "D6", "C4",
52             "DC", "DF", "3F", "5C", "2C", "3A", "3B", "23", "2B", "7E", "21",
53             "22", "A7", "24", "25", "26", "28", "29", "3D", "3C", "3E", "7B",
54             "5B", "5D", "7D", "2F", "E2", "EA", "EE", "F4", "FB", "C2", "CA",
55             "CE", "D4", "DB", "E1", "E9", "ED", "F3", "FA", "C1", "C9", "CD",
56             "D3", "DA", "E0", "E8", "EC", "F2", "F9", "C1", "C9", "CD", "D3",
57             "DA", "B0", "B3", "B2", "80", "7C", "5E", "60", "B4", "27", "20",
58             "40", "98", "2A"};
59     
60     public final static String JavaDoc charsHtml[] = { "", "", "", "", "", "",
61             "", "?", "\\", ",", ":", ";", "#", "+", "˜", "!", "\"",
62             "§", "$", "%", "&", "(", ")", "=", "<", ">", "{",
63             "[", "]", "}", "/", "â", "ê", "î", "ô",
64             "û", "Â", "Ê", "Î", "Ô", "Û",
65             "á", "é", "í", "ó", "ú",
66             "Á", "É", "Í", "Ó", "Ú",
67             "à", "è", "ì", "ò", "Ù",
68             "À", "È", "Ì", "Ò", "Ù",
69             "°", "³", "²", "€", "|", "ˆ", "`",
70             "´", "'", " ", "@", "~", "*"};
71
72     public final static String JavaDoc chars[] = { "", "", "", "", "", "", "",
73             "?", "\\", ",", ":", ";", "#", "+", "~", "!", "\"", "", "$", "%",
74             "&", "(", ")", "=", "<", ">", "{", "[", "]", "}", "/", "", "",
75             "", "", "", "", "", "", "", "", "", "", "", "", "",
76             "", "", "", "", "", "", "", "", "", "", "", "", "",
77             "", "", "", "", "", "", "|", "^", "`", "", "'", " ", "@",
78             "~", "*"};
79
80     public static String JavaDoc entityToChar (String JavaDoc raw) {
81         return (entityTo (raw, chars));
82     }
83     
84     public static String JavaDoc entityToHtml (String JavaDoc raw) {
85         return (charToHtml(entityTo (raw, chars)));
86     }
87
88     public static String JavaDoc htmlToChar (String JavaDoc raw) {
89         return convert (raw, charsHtml, chars);
90     }
91
92     public static String JavaDoc charToHtml (String JavaDoc raw) {
93         if (raw == null)
94             return null;
95         char[] chars = raw.toCharArray();
96         StringBuffer JavaDoc encoded = new StringBuffer JavaDoc();
97         for (int i = 0; i < chars.length; i++) {
98             char c = chars[i];
99             if (c == '<')
100                 encoded.append("&lt;");
101             else if (c == '>')
102                 encoded.append("&gt;");
103             else if (c < 128)
104                 encoded.append(c);
105             else if (c < 256)
106                 encoded.append(htmlEntities[c-128]);
107             else {
108                 encoded.append("&#");
109                 encoded.append((int) c);
110                 encoded.append(";");
111             }
112         }
113         return encoded.toString();
114     }
115
116    public static String JavaDoc entityTo (String JavaDoc raw, String JavaDoc[] tc) {
117       StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
118       boolean entity = false;
119       raw = raw.replace ('+', ' ');
120       String JavaDoc tokens = tc == charsHtml ? "%<>" : "%";
121       for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (raw, tokens, true); st.hasMoreTokens (); ) {
122          String JavaDoc token = st.nextToken ();
123          if (entity) {
124             boolean replaced = false;
125             for (int i = 0; i < entities.length; i++) {
126                if (token.startsWith (entities[i])) {
127                   sb.append (tc[i]);
128                   sb.append (token.substring (2));
129                   replaced = true;
130                   break;
131                }
132             }
133             if (!replaced)
134                sb.append (token);
135
136             entity = false;
137          } else if (token.equals ("%")) {
138             entity = true;
139             continue;
140          } else if (token.equals ("<")) {
141              sb.append ("&lt;");
142          } else if (token.equals (">")) {
143              sb.append ("&gt;");
144          } else {
145             sb.append (token);
146          }
147       }
148       return (sb.toString ());
149    }
150    
151    public static String JavaDoc convert (String JavaDoc raw, String JavaDoc[] from, String JavaDoc[] to) {
152        String JavaDoc result = raw;
153        for (int i = 0 ; i < from.length; i++) {
154            int idx = result.indexOf(from[i]);
155            if (idx < 0)
156                continue;
157            StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
158            while (idx > -1) {
159                sb.append (result.substring(0,idx));
160                sb.append (to[i]);
161                result = result.substring(idx + from[i].length());
162                idx = result.indexOf(from[i]);
163            }
164            sb.append (result);
165            result = sb.toString();
166        }
167        return result;
168    }
169 }
Popular Tags