View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cpd;
5   
6   import java.io.StringReader;
7   
8   import net.sourceforge.pmd.lang.LanguageRegistry;
9   import net.sourceforge.pmd.lang.LanguageVersionHandler;
10  import net.sourceforge.pmd.lang.TokenManager;
11  import net.sourceforge.pmd.lang.jsp.JspLanguageModule;
12  import net.sourceforge.pmd.lang.jsp.ast.Token;
13  
14  public class JSPTokenizer implements Tokenizer {
15  
16      public void tokenize(SourceCode sourceCode, Tokens tokenEntries) {
17  	StringBuilder buffer = sourceCode.getCodeBuffer();
18  	LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JspLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
19  	TokenManager tokenMgr = languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions())
20  		.getTokenManager(sourceCode.getFileName(), new StringReader(buffer.toString()));
21  	Token currentToken = (Token) tokenMgr.getNextToken();
22  	while (currentToken.image.length() > 0) {
23  	    tokenEntries.add(new TokenEntry(String.valueOf(currentToken.kind), sourceCode.getFileName(),
24  		    currentToken.beginLine));
25  	    currentToken = (Token) tokenMgr.getNextToken();
26  	}
27  	tokenEntries.add(TokenEntry.getEOF());
28      }
29  }