1 7 8 20 21 package java.text; 22 23 import java.lang.Character ; 24 25 33 34 class PatternEntry { 35 38 public void appendQuotedExtension(StringBuffer toAddTo) { 39 appendQuoted(extension,toAddTo); 40 } 41 42 45 public void appendQuotedChars(StringBuffer toAddTo) { 46 appendQuoted(chars,toAddTo); 47 } 48 49 54 public boolean equals(Object obj) { 55 if (obj == null) return false; 56 PatternEntry other = (PatternEntry ) obj; 57 boolean result = chars.equals(other.chars); 58 return result; 59 } 60 61 public int hashCode() { 62 return chars.hashCode(); 63 } 64 65 68 public String toString() { 69 StringBuffer result = new StringBuffer (); 70 addToBuffer(result, true, false, null); 71 return result.toString(); 72 } 73 74 77 final int getStrength() { 78 return strength; 79 } 80 81 84 final String getExtension() { 85 return extension; 86 } 87 88 91 final String getChars() { 92 return chars; 93 } 94 95 97 void addToBuffer(StringBuffer toAddTo, 98 boolean showExtension, 99 boolean showWhiteSpace, 100 PatternEntry lastEntry) 101 { 102 if (showWhiteSpace && toAddTo.length() > 0) 103 if (strength == Collator.PRIMARY || lastEntry != null) 104 toAddTo.append('\n'); 105 else 106 toAddTo.append(' '); 107 if (lastEntry != null) { 108 toAddTo.append('&'); 109 if (showWhiteSpace) 110 toAddTo.append(' '); 111 lastEntry.appendQuotedChars(toAddTo); 112 appendQuotedExtension(toAddTo); 113 if (showWhiteSpace) 114 toAddTo.append(' '); 115 } 116 switch (strength) { 117 case Collator.IDENTICAL: toAddTo.append('='); break; 118 case Collator.TERTIARY: toAddTo.append(','); break; 119 case Collator.SECONDARY: toAddTo.append(';'); break; 120 case Collator.PRIMARY: toAddTo.append('<'); break; 121 case RESET: toAddTo.append('&'); break; 122 case UNSET: toAddTo.append('?'); break; 123 } 124 if (showWhiteSpace) 125 toAddTo.append(' '); 126 appendQuoted(chars,toAddTo); 127 if (showExtension && extension.length() != 0) { 128 toAddTo.append('/'); 129 appendQuoted(extension,toAddTo); 130 } 131 } 132 133 static void appendQuoted(String chars, StringBuffer toAddTo) { 134 boolean inQuote = false; 135 char ch = chars.charAt(0); 136 if (Character.isSpaceChar(ch)) { 137 inQuote = true; 138 toAddTo.append('\''); 139 } else { 140 if (PatternEntry.isSpecialChar(ch)) { 141 inQuote = true; 142 toAddTo.append('\''); 143 } else { 144 switch (ch) { 145 case 0x0010: case '\f': case '\r': 146 case '\t': case '\n': case '@': 147 inQuote = true; 148 toAddTo.append('\''); 149 break; 150 case '\'': 151 inQuote = true; 152 toAddTo.append('\''); 153 break; 154 default: 155 if (inQuote) { 156 inQuote = false; toAddTo.append('\''); 157 } 158 break; 159 } 160 } 161 } 162 toAddTo.append(chars); 163 if (inQuote) 164 toAddTo.append('\''); 165 } 166 167 171 PatternEntry(int strength, 172 StringBuffer chars, 173 StringBuffer extension) 174 { 175 this.strength = strength; 176 this.chars = chars.toString(); 177 this.extension = (extension.length() > 0) ? extension.toString() 178 : ""; 179 } 180 181 static class Parser { 182 private String pattern; 183 private int i; 184 185 public Parser(String pattern) { 186 this.pattern = pattern; 187 this.i = 0; 188 } 189 190 public PatternEntry next() throws ParseException { 191 int newStrength = UNSET; 192 193 newChars.setLength(0); 194 newExtension.setLength(0); 195 196 boolean inChars = true; 197 boolean inQuote = false; 198 mainLoop: 199 while (i < pattern.length()) { 200 char ch = pattern.charAt(i); 201 if (inQuote) { 202 if (ch == '\'') { 203 inQuote = false; 204 } else { 205 if (newChars.length() == 0) newChars.append(ch); 206 else if (inChars) newChars.append(ch); 207 else newExtension.append(ch); 208 } 209 } else switch (ch) { 210 case '=': if (newStrength != UNSET) break mainLoop; 211 newStrength = Collator.IDENTICAL; break; 212 case ',': if (newStrength != UNSET) break mainLoop; 213 newStrength = Collator.TERTIARY; break; 214 case ';': if (newStrength != UNSET) break mainLoop; 215 newStrength = Collator.SECONDARY; break; 216 case '<': if (newStrength != UNSET) break mainLoop; 217 newStrength = Collator.PRIMARY; break; 218 case '&': if (newStrength != UNSET) break mainLoop; 219 newStrength = RESET; break; 220 case '\t': 221 case '\n': 222 case '\f': 223 case '\r': 224 case ' ': break; case '/': inChars = false; break; 226 case '\'': 227 inQuote = true; 228 ch = pattern.charAt(++i); 229 if (newChars.length() == 0) newChars.append(ch); 230 else if (inChars) newChars.append(ch); 231 else newExtension.append(ch); 232 break; 233 default: 234 if (newStrength == UNSET) { 235 throw new ParseException 236 ("missing char (=,;<&) : " + 237 pattern.substring(i, 238 (i+10 < pattern.length()) ? 239 i+10 : pattern.length()), 240 i); 241 } 242 if (PatternEntry.isSpecialChar(ch) && (inQuote == false)) 243 throw new ParseException 244 ("Unquoted punctuation character : " + Integer.toString(ch, 16), i); 245 if (inChars) { 246 newChars.append(ch); 247 } else { 248 newExtension.append(ch); 249 } 250 break; 251 } 252 i++; 253 } 254 if (newStrength == UNSET) 255 return null; 256 if (newChars.length() == 0) { 257 throw new ParseException 258 ("missing chars (=,;<&): " + 259 pattern.substring(i, 260 (i+10 < pattern.length()) ? 261 i+10 : pattern.length()), 262 i); 263 } 264 265 return new PatternEntry (newStrength, newChars, newExtension); 266 } 267 268 private StringBuffer newChars = new StringBuffer (); 270 private StringBuffer newExtension = new StringBuffer (); 271 272 } 273 274 static boolean isSpecialChar(char ch) { 275 return ((ch == '\u0020') || 276 ((ch <= '\u002F') && (ch >= '\u0022')) || 277 ((ch <= '\u003F') && (ch >= '\u003A')) || 278 ((ch <= '\u0060') && (ch >= '\u005B')) || 279 ((ch <= '\u007E') && (ch >= '\u007B'))); 280 } 281 282 283 static final int RESET = -2; 284 static final int UNSET = -1; 285 286 int strength = UNSET; 287 String chars = ""; 288 String extension = ""; 289 } 290 | Popular Tags |