001: package net.sourceforge.jrefactory.factory;
002:
003: import java.io.Reader;
004: import java.util.EmptyStackException;
005: import net.sourceforge.jrefactory.ast.SimpleNode;
006: import net.sourceforge.jrefactory.parser.JavaParser;
007: import net.sourceforge.jrefactory.parser.ParseException;
008:
009: /**
010: * Generates new parsers for a java file
011: *
012: *@author Chris Seguin
013: *@author <a href="JRefactory@ladyshot.demon.co.uk">Mike Atkinson</a>
014: *@version $Id: ParserFactory.java,v 1.3 2003/11/18 18:46:13 mikeatkinson Exp $
015: *@created June 6, 1999
016: */
017: public abstract class ParserFactory {
018: private SimpleNode root = null;
019:
020: private static JavaParser parser = null;
021:
022: /**
023: * Return the AST
024: *
025: *@param interactive do we want to receive a response in the form of
026: * a dialog box when a parse exception is encountered
027: *@return the simple node which represents the root
028: */
029: public SimpleNode getAbstractSyntaxTree(boolean interactive,
030: ExceptionPrinter printer) {
031: // Check to see if it is here yet
032: if (root == null) {
033: synchronized (ParserFactory.class) {
034: // Look it up
035: JavaParser parser = getParser();
036: if (parser == null) {
037: return null;
038: }
039:
040: // Get the parse tree
041: try {
042: root = parse(parser);
043: } catch (ParseException pe) {
044: System.out
045: .println("ParserFactory Version 0.1: Encountered errors during parse: "
046: + getKey());
047: printer.printException(pe, interactive);
048:
049: return null;
050: } catch (EmptyStackException ese) {
051: System.out
052: .println("ParserFactory Version 0.1: Encountered errors during parse: "
053: + getKey());
054: printer.printException(ese, false);
055: root = null;
056: }
057: }
058: }
059:
060: return root; // Return the tree
061: }
062:
063: protected SimpleNode parse(JavaParser parser) throws ParseException {
064: return parser.CompilationUnit();
065: }
066:
067: /**
068: * Return the input stream
069: *
070: *@return the input stream
071: */
072: protected abstract Reader getReader();
073:
074: /**
075: * Create the parser
076: *
077: *@return the java parser
078: */
079: protected JavaParser getParser() {
080: Reader in = getReader();
081: if (in == null) {
082: return null;
083: }
084:
085: // Create the parser
086: if (parser == null) {
087: ParserFactory.parser = new JavaParser(in);
088: } else {
089: ParserFactory.parser.ReInit(in);
090: }
091:
092: return ParserFactory.parser;
093: }
094:
095: /**
096: * A method to return some key identifying the file that is being parsed
097: *
098: *@return the identifier
099: */
100: protected abstract String getKey();
101: }
|