1 package org.apache.ws.jaxme.js.pattern; 2 3 import java.io.File ; 4 import java.io.FileReader ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.io.InputStreamReader ; 8 import java.io.Reader ; 9 import java.net.URL ; 10 import java.util.List ; 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 23 public class SourceReflector implements Reflector { 24 private final File file; 25 private final URL url; 26 27 30 public SourceReflector(File pFile) { 31 file = pFile; 32 url = null; 33 } 34 35 38 public SourceReflector(URL pURL) { 39 file = null; 40 url = pURL; 41 } 42 43 public JavaSource getJavaSource(final JavaSourceFactory pFactory) 44 throws RecognitionException, TokenStreamException, IOException { 45 List result; 46 if (file == null) { 47 InputStream istream = null; 48 try { 49 istream = url.openStream(); 50 Reader r = new InputStreamReader (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 ignore) {} } 56 } 57 } else { 58 Reader r = null; 59 try { 60 r = new FileReader (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 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 |