KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > 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.syncclient.common;
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  * @author Fabio Maggi @ Funambol
27  * @version $Id: Entities.java,v 1.3 2005/01/19 10:58:31 fabius Exp $
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
50     static interface EntityMap {
51         void add(String JavaDoc name, int value);
52
53         String JavaDoc name(int value);
54
55         int value(String JavaDoc name);
56     }
57
58     static class PrimitiveEntityMap implements EntityMap {
59         private Hashtable JavaDoc mapNameToValue = new Hashtable JavaDoc();
60         private Hashtable JavaDoc mapValueToName = new Hashtable JavaDoc();
61
62         public void add(String JavaDoc name, int value) {
63             mapNameToValue.put(name, new Integer JavaDoc(value));
64             mapValueToName.put(new Integer JavaDoc(value), name);
65         }
66
67         public String JavaDoc name(int value) {
68             return (String JavaDoc) mapValueToName.get(new Integer JavaDoc(value));
69         }
70
71         public int value(String JavaDoc name) {
72             Object JavaDoc value = mapNameToValue.get(name);
73             if (value == null) {
74                 return -1;
75             }
76             return ((Integer JavaDoc) value).intValue();
77         }
78     }
79
80     static class LookupEntityMap extends PrimitiveEntityMap {
81
82         private String JavaDoc[] lookupTable;
83         private int LOOKUP_TABLE_SIZE = 256;
84
85         public String JavaDoc name(int value) {
86
87             if (value < LOOKUP_TABLE_SIZE) {
88                 return lookupTable()[value];
89             }
90
91             return super.name(value);
92         }
93
94         private String JavaDoc[] lookupTable() {
95             if (lookupTable == null) {
96                 createLookupTable();
97             }
98             return lookupTable;
99         }
100
101         private void createLookupTable() {
102             lookupTable = new String JavaDoc[LOOKUP_TABLE_SIZE];
103             for (int i = 0, l = LOOKUP_TABLE_SIZE; i < l; ++i) {
104                 lookupTable[i] = super.name(i);
105             }
106         }
107     }
108
109     EntityMap map = new Entities.LookupEntityMap();
110
111     public void addEntities(String JavaDoc[][] entityArray) {
112         for (int i = 0; i < entityArray.length; ++i) {
113             addEntity(entityArray[i][0], Integer.parseInt(entityArray[i][1]));
114         }
115     }
116
117     public void addEntity(String JavaDoc name, int value) {
118         map.add(name, value);
119     }
120
121     public String JavaDoc entityName(int value) {
122         return map.name(value);
123     }
124
125
126     public int entityValue(String JavaDoc name) {
127         return map.value(name);
128     }
129
130     /**
131      * <p>Escapes special characters in a <code>String</code>.</p>
132      *
133      *
134      * @param str The <code>String</code> to escape.
135      * @return A escaped <code>String</code>.
136      */

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

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