1 19 20 package org.netbeans.lib.ddl.util; 21 22 import java.io.*; 23 import java.text.ParseException ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.Vector ; 28 29 import java.text.MessageFormat ; 30 import org.openide.util.NbBundle; 31 32 37 public class PListReader { 38 39 protected StreamTokenizer tokenizer = null; 40 41 public static Map read(String file) 42 throws FileNotFoundException, ParseException , IOException 43 { 44 PListReader reader = new PListReader(file); 45 return reader.getData(); 46 } 47 48 52 public PListReader(String file) 53 throws FileNotFoundException, ParseException , IOException 54 { 55 BufferedReader buffreader = new BufferedReader(new FileReader(file)); 56 tokenizer = createTokenizer(buffreader); 57 } 58 59 63 public PListReader(File file) 64 throws FileNotFoundException, ParseException , IOException 65 { 66 BufferedReader buffreader = new BufferedReader(new FileReader(file)); 67 tokenizer = createTokenizer(buffreader); 68 } 69 70 74 public PListReader(InputStream stream) 75 throws FileNotFoundException, ParseException , IOException 76 { 77 BufferedReader buffreader = new BufferedReader(new InputStreamReader(stream)); 78 tokenizer = createTokenizer(buffreader); 79 } 80 81 86 private StreamTokenizer createTokenizer(BufferedReader buffreader) 87 { 88 StreamTokenizer tok = new StreamTokenizer(buffreader); 89 tok.slashStarComments(true); 90 tok.slashSlashComments(true); 91 tok.wordChars(95, 95); 92 tok.wordChars(126, 126); 93 tok.wordChars(33, 33); 94 tok.wordChars(96, 96); 95 tok.wordChars(39, 39); 96 tok.wordChars(35, 35); 97 tok.wordChars(36, 36); 98 tok.wordChars(37, 37); 99 tok.wordChars(94, 94); 100 tok.wordChars(38, 38); 101 tok.wordChars(42, 42); 102 tok.wordChars(45, 45); 103 tok.wordChars(43, 43); 104 tok.wordChars(124, 124); 105 tok.wordChars(63, 63); 106 tok.wordChars(46, 46); 107 tok.quoteChar(34); 108 return tok; 109 } 110 111 116 private Object read(StreamTokenizer tokenizer) 117 throws FileNotFoundException, ParseException , IOException 118 { 119 DictionaryNode node = new DictionaryNode(tokenizer); 120 return node.getBindings(); 121 } 122 123 128 public HashMap getData() 129 throws FileNotFoundException, ParseException , IOException 130 { 131 return (HashMap )read(tokenizer); 132 } 133 134 137 public StreamTokenizer getTokenizer() 138 { 139 return tokenizer; 140 } 141 142 143 abstract class Node 144 { 145 150 public void parseChar(StreamTokenizer tokenizer, int charcode) 151 throws IOException, ParseException 152 { 153 tokenizer.nextToken(); 154 if (tokenizer.ttype != charcode) { 155 char[] charr = new char[1]; 156 charr[0] = (char)charcode; 157 throw new ParseException ( 159 MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Expected"), new String [] {new String (charr), tokenizer.toString()}), 161 tokenizer.lineno()); 162 } 163 } 164 165 170 public Object parseNumber(StreamTokenizer tokenizer) 171 throws IOException 172 { 173 String s5 = Double.toString(tokenizer.nval); 174 tokenizer.nextToken(); 175 if(tokenizer.ttype == -2) { 176 while(tokenizer.ttype == -2) { 177 s5 = s5 + Double.toString(tokenizer.nval); 178 tokenizer.nextToken(); 179 } 180 181 tokenizer.pushBack(); 182 return s5; 183 } 184 185 tokenizer.pushBack(); 186 double d = Math.rint(tokenizer.nval); 187 if(d == tokenizer.nval) return new Integer ((int)d); 188 return new Double (tokenizer.nval); 189 } 190 } 191 192 193 197 class DictionaryNode extends Node 198 { 199 200 HashMap bindings; 201 202 203 public DictionaryNode(StreamTokenizer tokenizer) 204 throws ParseException , IOException 205 { 206 bindings = new HashMap (); 207 parse(tokenizer); 208 } 209 210 214 public void parse(StreamTokenizer tokenizer) 215 throws ParseException , IOException 216 { 217 String key = null; 218 Object object = null; 219 220 try { 221 222 parseChar(tokenizer,123); 224 225 while (true) { 226 227 tokenizer.nextToken(); 229 switch(tokenizer.ttype) { 230 case StreamTokenizer.TT_WORD: 231 key = tokenizer.sval; break; 232 case 34: 233 key = tokenizer.sval; break; 234 case 125: 235 throw new EOFException(); 236 default: 237 throw new ParseException ( MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnexpectedKey"), new String [] { tokenizer.toString() } ), 240 tokenizer.lineno()); 241 } 242 243 parseChar(tokenizer,61); 245 246 tokenizer.nextToken(); 248 switch(tokenizer.ttype) { 249 case StreamTokenizer.TT_WORD: 250 object = tokenizer.sval; break; 251 case 34: 252 object = tokenizer.sval; break; 253 case 123: 254 tokenizer.pushBack(); 255 object = (Object )new DictionaryNode(tokenizer).getBindings(); 256 break; 257 case 40: 258 tokenizer.pushBack(); 259 object = (Object )new ArrayNode(tokenizer).getBindings(); 260 break; 261 case StreamTokenizer.TT_NUMBER: 262 object = parseNumber(tokenizer); 263 break; 264 default: 265 throw new ParseException ( MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ExpectedObject"), new String [] { tokenizer.toString() } ), 267 tokenizer.lineno()); 268 } 269 270 parseChar(tokenizer,59); 272 bindings.put(key,object); 273 } 274 275 } catch (EOFException e) { 276 } 277 } 278 279 280 public Object get(String s) 281 { 282 return bindings.get(s); 283 } 284 285 286 public Set getKeys() 287 { 288 return bindings.keySet(); 289 } 290 291 292 public HashMap getBindings() 293 { 294 return bindings; 295 } 296 } 297 298 299 303 class ArrayNode extends Node 304 { 305 306 Vector bindings; 307 308 309 public ArrayNode(StreamTokenizer tokenizer) 310 throws ParseException , IOException 311 { 312 bindings = new Vector (); 313 parse(tokenizer); 314 } 315 316 320 public void parse(StreamTokenizer tokenizer) 321 throws ParseException , IOException 322 { 323 Object object = null; 324 325 try { 326 327 parseChar(tokenizer,40); 329 330 while (true) { 331 332 tokenizer.nextToken(); 334 switch(tokenizer.ttype) { 335 case StreamTokenizer.TT_WORD: 336 object = tokenizer.sval; break; 337 case 34: 338 object = tokenizer.sval; break; 339 case 41: 340 throw new EOFException(); 341 case 123: 342 tokenizer.pushBack(); 343 object = (Object )new DictionaryNode(tokenizer).getBindings(); 344 break; 345 case 40: 346 tokenizer.pushBack(); 347 object = new ArrayNode(tokenizer).getBindings(); 348 break; 349 case StreamTokenizer.TT_NUMBER: 350 object = parseNumber(tokenizer); 351 break; 352 default: 353 throw new ParseException ( MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ExpectedObject"), new String [] { tokenizer.toString() } ), 355 tokenizer.lineno()); 356 } 357 358 bindings.add(object); 359 360 tokenizer.nextToken(); 362 switch(tokenizer.ttype) { 363 case 41: 364 throw new EOFException(); 365 case 44: 366 break; 367 default: 368 throw new ParseException ( 369 MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Expected"), new String [] {"','", tokenizer.toString()}), tokenizer.lineno()); 372 } 373 } 374 375 } catch (EOFException e) { 376 } 377 } 378 379 380 public Vector getBindings() 381 { 382 return bindings; 383 } 384 } 385 } 386 | Popular Tags |