Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 81 |  | Methods: | 4
| NCLOC: | 56 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
JavaTokenizer.java | 100% | 100% | 100% | 100% |

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 net.sourceforge.pmd.TargetJDK1_4;
7 |  | import net.sourceforge.pmd.ast.JavaParserConstants;
8 |  | import net.sourceforge.pmd.ast.JavaParserTokenManager;
9 |  | import net.sourceforge.pmd.ast.Token;
10 |  |
11 |  | import java.io.StringReader;
12 |  | import java.util.Properties;
13 |  |
14 |  | public class JavaTokenizer implements Tokenizer {
15 |  |
16 |  | public static final String IGNORE_LITERALS = "ignore_literals";
17 |  | public static final String IGNORE_IDENTIFIERS = "ignore_identifiers";
18 |  |
19 |  | private boolean ignoreLiterals;
20 |  | private boolean ignoreIdentifiers;
21 |  |
22 | 1 | public void setProperties(Properties properties) {
23 | 1 | ignoreLiterals = Boolean.valueOf(properties.getProperty(IGNORE_LITERALS, "false")).booleanValue();
24 | 1 | ignoreIdentifiers = Boolean.valueOf(properties.getProperty(IGNORE_IDENTIFIERS, "false")).booleanValue();
25 |  | }
26 |  |
27 | 8 | public void tokenize(SourceCode tokens, Tokens tokenEntries) {
28 | 8 | StringBuffer buffer = tokens.getCodeBuffer();
29 |  |
30 |  | /*
31 |  | I'm doing a sort of State pattern thing here where
32 |  | this goes into "discarding" mode when it hits an import or package
33 |  | keyword and goes back into "accumulate mode" when it hits a semicolon.
34 |  | This could probably be turned into some objects.
35 |  | */
36 |  |
37 |  | // TODO - allow for JDK 1.5 selection
38 | 8 | JavaParserTokenManager tokenMgr = new TargetJDK1_4().createJavaParserTokenManager(new StringReader(buffer.toString()));
39 | 8 | Token currentToken = tokenMgr.getNextToken();
40 | 8 | boolean inDiscardingState = false;
41 | 8 | while (currentToken.image.length() > 0) {
42 | 186 | if (currentToken.kind == JavaParserConstants.IMPORT || currentToken.kind == JavaParserConstants.PACKAGE) {
43 | 2 | inDiscardingState = true;
44 | 2 | currentToken = tokenMgr.getNextToken();
45 | 2 | continue;
46 |  | }
47 |  |
48 | 184 | if (inDiscardingState && currentToken.kind == JavaParserConstants.SEMICOLON) {
49 | 2 | inDiscardingState = false;
50 |  | }
51 |  |
52 | 184 | if (inDiscardingState) {
53 | 10 | currentToken = tokenMgr.getNextToken();
54 | 10 | continue;
55 |  | }
56 |  |
57 | 174 | if (currentToken.kind != JavaParserConstants.SEMICOLON) {
58 | 162 | String image = currentToken.image;
59 | 162 | if (ignoreLiterals && (currentToken.kind == JavaParserConstants.STRING_LITERAL || currentToken.kind == JavaParserConstants.CHARACTER_LITERAL
60 |  | || currentToken.kind == JavaParserConstants.DECIMAL_LITERAL || currentToken.kind == JavaParserConstants.FLOATING_POINT_LITERAL)) {
61 | 3 | image = String.valueOf(currentToken.kind);
62 |  | }
63 | 162 | if (ignoreIdentifiers && currentToken.kind == JavaParserConstants.IDENTIFIER) {
64 | 12 | image = String.valueOf(currentToken.kind);
65 |  | }
66 | 162 | tokenEntries.add(new TokenEntry(image, tokens.getFileName(), currentToken.beginLine));
67 |  | }
68 |  |
69 | 174 | currentToken = tokenMgr.getNextToken();
70 |  | }
71 | 8 | tokenEntries.add(TokenEntry.getEOF());
72 |  | }
73 |  |
74 | 1 | public void setIgnoreLiterals(boolean ignore) {
75 | 1 | this.ignoreLiterals = ignore;
76 |  | }
77 |  |
78 | 1 | public void setIgnoreIdentifiers(boolean ignore) {
79 | 1 | this.ignoreIdentifiers = ignore;
80 |  | }
81 |  | }