View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.lang.java;
    5
    6   import java.io.Reader;
    7   import java.util.Map;
    8
    9   import net.sourceforge.pmd.lang.AbstractParser;
    10  import net.sourceforge.pmd.lang.ParserOptions;
    11  import net.sourceforge.pmd.lang.TokenManager;
    12  import net.sourceforge.pmd.lang.ast.AbstractTokenManager;
    13  import net.sourceforge.pmd.lang.ast.JavaCharStream;
    14  import net.sourceforge.pmd.lang.ast.Node;
    15  import net.sourceforge.pmd.lang.java.ast.JavaParser;
    16  import net.sourceforge.pmd.lang.java.ast.ParseException;
    17
    18  /**
    19   * This is a generic Java specific implementation of the Parser interface. It
    20   * creates a JavaParser instance, and sets the exclude marker. It also exposes
    21   * the exclude map from the JavaParser instance.
    22   *
    23   * @see AbstractParser
    24   * @see JavaParser
    25   */
    26  public abstract class AbstractJavaParser extends AbstractParser {
    27      private JavaParser parser;
    28
    29      public AbstractJavaParser(ParserOptions parserOptions) {
    30  	super(parserOptions);
    31      }
    32
    33      @Override
    34      public TokenManager createTokenManager(Reader source) {
    35  	return new JavaTokenManager(source);
    36      }
    37
    38      /**
    39       * Subclass should override this method to modify the JavaParser as needed.
    40       */
    41      protected JavaParser createJavaParser(Reader source) throws ParseException {
    42  	parser = new JavaParser(new JavaCharStream(source));
    43  	String suppressMarker = getParserOptions().getSuppressMarker();
    44  	if (suppressMarker != null) {
    45  	    parser.setSuppressMarker(suppressMarker);
    46  	}
    47  	return parser;
    48      }
    49
    50      public boolean canParse() {
    51  	return true;
    52      }
    53
    54      public Node parse(String fileName, Reader source) throws ParseException {
    55  	AbstractTokenManager.setFileName(fileName);
    56  	return createJavaParser(source).CompilationUnit();
    57      }
    58
    59      public Map<Integer, String> getSuppressMap() {
    60  	return parser.getSuppressMap();
    61      }
    62  }