KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > pattern > SourceReflector


1 package org.apache.ws.jaxme.js.pattern;
2
3 import java.io.File JavaDoc;
4 import java.io.FileReader JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.io.Reader JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.List JavaDoc;
11
12 import org.apache.ws.jaxme.js.JavaSource;
13 import org.apache.ws.jaxme.js.JavaSourceFactory;
14 import org.apache.ws.jaxme.js.util.JavaParser;
15
16 import antlr.RecognitionException;
17 import antlr.TokenStreamException;
18
19
20 /** Reflector for gathering information about a Java
21  * source file.
22  */

23 public class SourceReflector implements Reflector {
24     private final File JavaDoc file;
25     private final URL JavaDoc url;
26
27     /** Creates a new <code>SourceReflector</code>, which
28      * is going to read the Java source file <code>pFile</code>.
29      */

30     public SourceReflector(File JavaDoc pFile) {
31         file = pFile;
32         url = null;
33     }
34
35     /** Creates a new <code>SourceReflector</code>, which
36      * is going to read the Java source file from <code>pURL</code>.
37      */

38     public SourceReflector(URL JavaDoc pURL) {
39         file = null;
40         url = pURL;
41     }
42
43     public JavaSource getJavaSource(final JavaSourceFactory pFactory)
44             throws RecognitionException, TokenStreamException, IOException JavaDoc {
45         List JavaDoc result;
46         if (file == null) {
47             InputStream JavaDoc istream = null;
48             try {
49                 istream = url.openStream();
50                 Reader JavaDoc r = new InputStreamReader JavaDoc(istream);
51                 result = new JavaParser(pFactory).parse(r);
52                 istream.close();
53                 istream = null;
54             } finally {
55                 if (istream != null) { try { istream.close(); } catch (Throwable JavaDoc ignore) {} }
56             }
57         } else {
58             Reader JavaDoc r = null;
59             try {
60                 r = new FileReader JavaDoc(file);
61                 result = new JavaParser(pFactory).parse(r);
62                 r.close();
63                 r = null;
64             } finally {
65                 if (r != null) { try { r.close(); } catch (Throwable JavaDoc ignore) {} }
66             }
67         }
68         if (result.size() > 1) {
69             throw new RecognitionException("The Java source file contained multiple classes.");
70         }
71         if (result.size() > 1) {
72             throw new RecognitionException("The Java source file contained multiple classes.");
73         }
74         return (JavaSource) result.get(0);
75     }
76
77 }
78
Popular Tags