KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > exchange > util > Entities


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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 sync4j.exchange.util;
19
20 import java.util.Hashtable JavaDoc;
21
22 /**
23  * This class supplies some methods
24  * to escape / unascape special chars according XML specifications
25  *
26  * @version $Id: Entities.java,v 1.1 2005/06/22 06:51:27 fabius Exp $
27  * @author Fabio Maggi
28  */

29 class Entities {
30
31     private static final String JavaDoc[][] BASIC_ARRAY = {
32         {"quot", "34"}, // " - double-quote
33
{"amp", "38"}, // & - ampersand
34
{"lt", "60"}, // < - less-than
35
{"gt", "62"}, // > - greater-than
36
{"apos", "39"}, // XML apostrophe
37
};
38
39     /**
40      * <p>The set of entities supported by standard XML.</p>
41      */

42     public static final Entities XML;
43
44     static {
45         XML = new Entities();
46         XML.addEntities(BASIC_ARRAY);
47     }
48
49     static interface EntityMap {
50         void add(String JavaDoc name, int value);
51
52         String JavaDoc name(int value);
53
54         int value(String JavaDoc name);
55     }
56
57     static class PrimitiveEntityMap implements EntityMap {
58         private Hashtable JavaDoc mapNameToValue = new Hashtable JavaDoc();
59         private Hashtable JavaDoc mapValueToName = new Hashtable JavaDoc();
60
61         public void add(String JavaDoc name, int value) {
62             mapNameToValue.put(name, new Integer JavaDoc(value));
63             mapValueToName.put(new Integer JavaDoc(value), name);
64         }
65
66         public String JavaDoc name(int value) {
67             return (String JavaDoc) mapValueToName.get(new Integer JavaDoc(value));
68         }
69
70         public int value(String JavaDoc name) {
71             Object JavaDoc value = mapNameToValue.get(name);
72             if (value == null) {
73                 return -1;
74             }
75             return ((Integer JavaDoc) value).intValue();
76         }
77     }
78
79     static class LookupEntityMap extends PrimitiveEntityMap {
80
81         private String JavaDoc[] lookupTable;
82         private int LOOKUP_TABLE_SIZE = 256;
83
84         public String JavaDoc name(int value) {
85
86             if (value < LOOKUP_TABLE_SIZE) {
87                 return lookupTable()[value];
88             }
89
90             return super.name(value);
91         }
92
93         private String JavaDoc[] lookupTable() {
94             if (lookupTable == null) {
95                 createLookupTable();
96             }
97             return lookupTable;
98         }
99
100         private void createLookupTable() {
101             lookupTable = new String JavaDoc[LOOKUP_TABLE_SIZE];
102             for (int i = 0, l = LOOKUP_TABLE_SIZE; i < l; ++i) {
103                 lookupTable[i] = super.name(i);
104             }
105         }
106     }
107
108     EntityMap map = new Entities.LookupEntityMap();
109
110     public void addEntities(String JavaDoc[][] entityArray) {
111         for (int i = 0; i < entityArray.length; ++i) {
112             addEntity(entityArray[i][0], Integer.parseInt(entityArray[i][1]));
113         }
114     }
115
116     public void addEntity(String JavaDoc name, int value) {
117         map.add(name, value);
118     }
119
120     public String JavaDoc entityName(int value) {
121         return map.name(value);
122     }
123
124
125     public int entityValue(String JavaDoc name) {
126         return map.value(name);
127     }
128
129     /**
130      * <p>Escapes special characters in a <code>String</code>.</p>
131      *
132      *
133      * @param str The <code>String</code> to escape.
134      * @return A escaped <code>String</code>.
135      */

136     public String JavaDoc escape(String JavaDoc str) {
137
138         char ch = ' ' ;
139
140         String JavaDoc entityName = null ;
141         StringBuffer JavaDoc buf = null ;
142
143         int intValue = 0 ;
144
145         buf = new StringBuffer JavaDoc(str.length() * 2);
146
147         for (int i = 0, l = str.length(); i < l; ++i) {
148
149             ch = str.charAt(i);
150             entityName = this.entityName(ch);
151
152             if (entityName == null) {
153
154                 if (ch > 0x7F) {
155
156                     intValue = ch;
157                     buf.append("&#");
158                     buf.append(intValue);
159                     buf.append(';');
160
161                 } else {
162                     buf.append(ch);
163                 }
164             } else {
165
166                 buf.append('&');
167                 buf.append(entityName);
168                 buf.append(';');
169
170             }
171         }
172
173         return buf.toString();
174     }
175
176     /**
177      * <p>Unescapes special characters in a <code>String</code>.</p>
178      *
179      * @param str The <code>String</code> to escape.
180      * @return A un-escaped <code>String</code>.
181      */

182     public String JavaDoc unescape(String JavaDoc str) {
183
184         StringBuffer JavaDoc buf = null ;
185         String JavaDoc entityName = null ;
186
187         char ch = ' ' ;
188         char charAt1 = ' ' ;
189
190         int entityValue = 0 ;
191
192         buf = new StringBuffer JavaDoc(str.length());
193
194         for (int i = 0, l = str.length(); i < l; ++i) {
195
196             ch = str.charAt(i);
197
198             if (ch == '&') {
199
200                 int semi = str.indexOf(';', i + 1);
201
202                 if (semi == -1) {
203                     buf.append(ch);
204                     continue;
205                 }
206
207                 entityName = str.substring(i + 1, semi);
208
209                 if (entityName.charAt(0) == '#') {
210                     charAt1 = entityName.charAt(1);
211                     if (charAt1 == 'x' || charAt1=='X') {
212                         entityValue = Integer.valueOf(entityName.substring(2), 16).intValue();
213                     } else {
214                         entityValue = Integer.parseInt(entityName.substring(1));
215                     }
216                 } else {
217                     entityValue = this.entityValue(entityName);
218                 } if (entityValue == -1) {
219                     buf.append('&');
220                     buf.append(entityName);
221                     buf.append(';');
222                 } else {
223                     buf.append((char) (entityValue));
224                 }
225
226                 i = semi;
227
228             } else {
229
230                 buf.append(ch);
231
232             }
233         }
234
235         return buf.toString();
236     }
237
238 }
Popular Tags