1 17 18 19 20 package org.apache.lenya.xml; 21 22 23 26 public class Normalize { 27 32 public static void main(String [] args) { 33 System.out.println(Normalize.normalize("&")); 34 System.out.println(Normalize.denormalize("Zürich")); 35 System.out.println(Normalize.denormalize("Z&252;rich & Region̓djŤ")); 36 } 37 38 41 public static String normalize(String s) { 42 StringBuffer sb = new StringBuffer (); 43 44 for (int i = 0; i < s.length(); i++) { 45 char ch = s.charAt(i); 46 47 switch (ch) { 48 case '&': { 50 sb.append("&"); 51 52 break; 53 } 54 55 case 60: { 57 sb.append("<"); 58 59 break; 60 } 61 62 case 62: { 64 sb.append(">"); 65 66 break; 67 } 68 69 case 139: { 71 sb.append("‹"); 72 73 break; 74 } 75 76 case 155: { 78 sb.append("›"); 79 80 break; 81 } 82 83 case 160: { 85 sb.append(" "); 86 87 break; 88 } 89 90 case 171: { 92 sb.append("«"); 93 94 break; 95 } 96 97 case 183: { 99 sb.append("·"); 100 101 break; 102 } 103 104 case 187: { 106 sb.append("»"); 107 108 break; 109 } 110 111 case 196: { 113 sb.append("Ä"); 114 115 break; 116 } 117 118 case 214: { 120 sb.append("Ö"); 121 122 break; 123 } 124 125 case 220: { 127 sb.append("Ü"); 128 129 break; 130 } 131 132 case 223: { 134 sb.append("ß"); 135 136 break; 137 } 138 139 case 225: { 141 sb.append("á"); 142 143 break; 144 } 145 146 case 228: { 148 sb.append("ä"); 149 150 break; 151 } 152 153 case 232: { 155 sb.append("è"); 156 157 break; 158 } 159 160 case 233: { 162 sb.append("é"); 163 164 break; 165 } 166 167 case 234: { 169 sb.append("ê"); 170 171 break; 172 } 173 174 case 244: { 176 sb.append("ô"); 177 178 break; 179 } 180 181 case 246: { 183 sb.append("ö"); 184 185 break; 186 } 187 188 case 252: { 190 sb.append("ü"); 191 192 break; 193 } 194 195 default:sb.append(ch); 196 } 197 } 198 199 return sb.toString(); 200 } 201 202 205 public static String denormalize(String s) { 206 StringBuffer sb = new StringBuffer (); 207 208 for (int i = 0; i < s.length(); i++) { 209 char ch = s.charAt(i); 210 211 if (ch == '&') { 212 StringBuffer substring = new StringBuffer (); 213 int length = i + 6; 214 215 if (length > s.length()) { 216 length = s.length(); 217 } 218 219 for (int k = i; k < length; k++) { 220 substring.append(s.charAt(k)); 221 222 if (s.charAt(k) == ';') { 223 break; 224 } 225 } 226 227 if (substring.length() > 3) { 228 if ((substring.charAt(1) != '#') || 229 (substring.charAt(substring.length() - 1) != ';')) { 230 sb.append(ch); 231 } else { 232 int ascii = 0; 233 int power = 1; 234 235 for (int j = substring.length() - 2; j >= 2; j--) { 236 try { 237 Integer integer = new Integer ("" + substring.charAt(j)); 238 ascii = ascii + (power * integer.intValue()); 239 } catch (Exception e) { 240 ascii = -1; 241 242 break; 243 } 244 245 power = power * 10; 246 } 247 248 if (ascii >= 0) { 249 char character = (char) ascii; 250 sb.append(character); 251 i = (i + substring.length()) - 1; 252 } else { 253 sb.append(ch); 254 } 255 } 256 } else { 257 sb.append(ch); 258 } 259 } else { 260 sb.append(ch); 261 } 262 } 263 264 return sb.toString(); 265 } 266 } 267 | Popular Tags |