KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > ddl > util > PListReader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.ddl.util;
21
22 import java.io.*;
23 import java.text.ParseException JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import java.text.MessageFormat JavaDoc;
30 import org.openide.util.NbBundle;
31
32 /** Reader for "plist" format. This format uses {} brackets to enclose dictionary
33 * data (Map) and () braces for array data (Collection). Returns Map with data.
34 *
35 * @author Slavek Psenicka
36 */

37 public class PListReader {
38
39     protected StreamTokenizer tokenizer = null;
40
41     public static Map JavaDoc read(String JavaDoc file)
42     throws FileNotFoundException, ParseException JavaDoc, IOException
43     {
44         PListReader reader = new PListReader(file);
45         return reader.getData();
46     }
47
48     /** Constructor
49     * Initializes reader with contents of file
50     * @param file File to read
51     */

52     public PListReader(String JavaDoc file)
53     throws FileNotFoundException, ParseException JavaDoc, IOException
54     {
55         BufferedReader buffreader = new BufferedReader(new FileReader(file));
56         tokenizer = createTokenizer(buffreader);
57     }
58
59     /** Constructor
60     * Initializes reader with contents of file
61     * @param file File to read
62     */

63     public PListReader(File file)
64     throws FileNotFoundException, ParseException JavaDoc, IOException
65     {
66         BufferedReader buffreader = new BufferedReader(new FileReader(file));
67         tokenizer = createTokenizer(buffreader);
68     }
69
70     /** Constructor
71     * Initializes reader with stream
72     * @param stream Stream to read
73     */

74     public PListReader(InputStream stream)
75     throws FileNotFoundException, ParseException JavaDoc, IOException
76     {
77         BufferedReader buffreader = new BufferedReader(new InputStreamReader(stream));
78         tokenizer = createTokenizer(buffreader);
79     }
80
81     /** Prepares tokenizer for this format.
82     * Enables both comment styles.
83     * @param buffreader Reader
84     * @return Newly created tokenizer
85     */

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     /** Reads data from tokenizer.
112     * Parses given data tokenizer and produces data.
113     * @param tokenizer Used tokenizer
114     * @return Parsed data structure.
115     */

116     private Object JavaDoc read(StreamTokenizer tokenizer)
117     throws FileNotFoundException, ParseException JavaDoc, IOException
118     {
119         DictionaryNode node = new DictionaryNode(tokenizer);
120         return node.getBindings();
121     }
122
123     /** Reads data from tokenizer.
124     * Parses given data tokenizer and produces data.
125     * @param tokenizer Used tokenizer
126     * @return Parsed data structure.
127     */

128     public HashMap JavaDoc getData()
129     throws FileNotFoundException, ParseException JavaDoc, IOException
130     {
131         return (HashMap JavaDoc)read(tokenizer);
132     }
133
134     /** Returns prepared tokenizer.
135     * @return Prepared tokenizer.
136     */

137     public StreamTokenizer getTokenizer()
138     {
139         return tokenizer;
140     }
141
142     /** Inner superclass for nodes */
143     abstract class Node
144     {
145         /** Parses expected character from stream.
146         * Throws ParseException if expected character was not found.
147         * @param tokenizer Used tokenizer
148         * @param charcode Expected character code
149         */

150         public void parseChar(StreamTokenizer tokenizer, int charcode)
151         throws IOException, ParseException JavaDoc
152         {
153             tokenizer.nextToken();
154             if (tokenizer.ttype != charcode) {
155                 char[] charr = new char[1];
156                 charr[0] = (char)charcode;
157                 //throw new ParseException("expected '"+new String(charr)+"', found: "+tokenizer.toString(), tokenizer.lineno());
158
throw new ParseException JavaDoc(
159                     MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Expected"), // NOI18N
160
new String JavaDoc[] {new String JavaDoc(charr), tokenizer.toString()}),
161                     tokenizer.lineno());
162             }
163         }
164
165         /** Parses expected character from stream.
166         * Throws ParseException if expected character was not found.
167         * @param tokenizer Used tokenizer
168         * @param charcode Expected character code
169         */

170         public Object JavaDoc parseNumber(StreamTokenizer tokenizer)
171         throws IOException
172         {
173             String JavaDoc 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 JavaDoc((int)d);
188             return new Double JavaDoc(tokenizer.nval);
189         }
190     }
191
192
193     /**
194     * Inner class for dictionary node.
195     * @author Slavek Psenicka
196     */

197     class DictionaryNode extends Node
198     {
199         /** Read values */
200         HashMap JavaDoc bindings;
201
202         /* Constructor */
203         public DictionaryNode(StreamTokenizer tokenizer)
204         throws ParseException JavaDoc, IOException
205         {
206             bindings = new HashMap JavaDoc();
207             parse(tokenizer);
208         }
209
210         /* Method for reading data from tokenizer
211         * Prepares structure into bindings map.
212         * @param tokenizer Used tokenizer
213         */

214         public void parse(StreamTokenizer tokenizer)
215         throws ParseException JavaDoc, IOException
216         {
217             String JavaDoc key = null;
218             Object JavaDoc object = null;
219
220             try {
221
222                 // left bracket
223
parseChar(tokenizer,123);
224
225                 while (true) {
226
227                     // key or right bracket
228
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("unexpected key, found: "+tokenizer.toString(), tokenizer.lineno());
238
throw new ParseException JavaDoc( MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnexpectedKey"), // NOI18N
239
new String JavaDoc[] { tokenizer.toString() } ),
240                                                 tokenizer.lineno());
241                     }
242
243                     // =
244
parseChar(tokenizer,61);
245
246                     // object
247
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 JavaDoc)new DictionaryNode(tokenizer).getBindings();
256                         break;
257                     case 40:
258                         tokenizer.pushBack();
259                         object = (Object JavaDoc)new ArrayNode(tokenizer).getBindings();
260                         break;
261                     case StreamTokenizer.TT_NUMBER:
262                         object = parseNumber(tokenizer);
263                         break;
264                     default:
265                         throw new ParseException JavaDoc( MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ExpectedObject"), // NOI18N
266
new String JavaDoc[] { tokenizer.toString() } ),
267                                                                         tokenizer.lineno());
268                     }
269
270                     // ;
271
parseChar(tokenizer,59);
272                     bindings.put(key,object);
273                 }
274
275             } catch (EOFException e) {
276             }
277         }
278
279         /** Returns value for specified key */
280         public Object JavaDoc get(String JavaDoc s)
281         {
282             return bindings.get(s);
283         }
284
285         /** Returns all keys */
286         public Set JavaDoc getKeys()
287         {
288             return bindings.keySet();
289         }
290
291         /** Returns whole binding map */
292         public HashMap JavaDoc getBindings()
293         {
294             return bindings;
295         }
296     }
297
298
299     /**
300     * Inner class for array node.
301     * @author Slavek Psenicka
302     */

303     class ArrayNode extends Node
304     {
305         /* Data */
306         Vector JavaDoc bindings;
307
308         /** Constructor */
309         public ArrayNode(StreamTokenizer tokenizer)
310         throws ParseException JavaDoc, IOException
311         {
312             bindings = new Vector JavaDoc();
313             parse(tokenizer);
314         }
315
316         /* Method for reading data from tokenizer
317         * Prepares structure into bindings map.
318         * @param tokenizer Used tokenizer
319         */

320         public void parse(StreamTokenizer tokenizer)
321         throws ParseException JavaDoc, IOException
322         {
323             Object JavaDoc object = null;
324
325             try {
326
327                 // left bracket
328
parseChar(tokenizer,40);
329
330                 while (true) {
331
332                     // object
333
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 JavaDoc)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 JavaDoc( MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_ExpectedObject"), // NOI18N
354
new String JavaDoc[] { tokenizer.toString() } ),
355                                                 tokenizer.lineno());
356                     }
357
358                     bindings.add(object);
359
360                     // ,
361
tokenizer.nextToken();
362                     switch(tokenizer.ttype) {
363                     case 41:
364                         throw new EOFException();
365                     case 44:
366                         break;
367                     default:
368                         throw new ParseException JavaDoc(
369                             MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_Expected"), // NOI18N
370
new String JavaDoc[] {"','", tokenizer.toString()}), // NOI18N
371
tokenizer.lineno());
372                             }
373                 }
374
375             } catch (EOFException e) {
376             }
377         }
378
379         /** Returns while binding array */
380         public Vector JavaDoc getBindings()
381         {
382             return bindings;
383         }
384     }
385 }
386
Popular Tags