1 18 package sync4j.syncclient.common; 19 20 import java.util.Hashtable ; 21 22 29 class Entities { 30 31 private static final String [][] BASIC_ARRAY = { 32 {"quot" , "34"}, {"amp" , "38"}, {"lt" , "60"}, {"gt" , "62"}, {"apos" , "39"}, }; 38 39 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 name, int value); 52 53 String name(int value); 54 55 int value(String name); 56 } 57 58 static class PrimitiveEntityMap implements EntityMap { 59 private Hashtable mapNameToValue = new Hashtable (); 60 private Hashtable mapValueToName = new Hashtable (); 61 62 public void add(String name, int value) { 63 mapNameToValue.put(name, new Integer (value)); 64 mapValueToName.put(new Integer (value), name); 65 } 66 67 public String name(int value) { 68 return (String ) mapValueToName.get(new Integer (value)); 69 } 70 71 public int value(String name) { 72 Object value = mapNameToValue.get(name); 73 if (value == null) { 74 return -1; 75 } 76 return ((Integer ) value).intValue(); 77 } 78 } 79 80 static class LookupEntityMap extends PrimitiveEntityMap { 81 82 private String [] lookupTable; 83 private int LOOKUP_TABLE_SIZE = 256; 84 85 public String 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 [] lookupTable() { 95 if (lookupTable == null) { 96 createLookupTable(); 97 } 98 return lookupTable; 99 } 100 101 private void createLookupTable() { 102 lookupTable = new String [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 [][] 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 name, int value) { 118 map.add(name, value); 119 } 120 121 public String entityName(int value) { 122 return map.name(value); 123 } 124 125 126 public int entityValue(String name) { 127 return map.value(name); 128 } 129 130 137 public String escape(String str) { 138 139 char ch = ' ' ; 140 141 String entityName = null ; 142 StringBuffer buf = null ; 143 144 int intValue = 0 ; 145 146 buf = new StringBuffer (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 183 public String unescape(String str) { 184 185 StringBuffer buf = null ; 186 String entityName = null ; 187 188 char ch = ' ' ; 189 char charAt1 = ' ' ; 190 191 int entityValue = 0 ; 192 193 buf = new StringBuffer (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 |