* Replace some code by our own implementation * Remove unused * Reformat to conform with our conventionstags/2.14
@@ -20,24 +20,51 @@ | |||
package net.sourceforge.pmd.cpd; | |||
import net.sourceforge.pmd.util.filter.Filters; | |||
import java.io.File; | |||
import java.io.FilenameFilter; | |||
public abstract class AbstractLanguage implements Language { | |||
private final Tokenizer tokenizer; | |||
private final FilenameFilter fileFilter; | |||
private final Tokenizer tokenizer; | |||
private final FilenameFilter fileFilter; | |||
public AbstractLanguage(Tokenizer tokenizer, String... extensions) { | |||
this.tokenizer = tokenizer; | |||
fileFilter = new ExtensionsFilter(extensions); | |||
} | |||
/** | |||
* @deprecated in 2.14, seems that not used in Sonar ecosystem - we don't scan directories. | |||
*/ | |||
public FilenameFilter getFileFilter() { | |||
return fileFilter; | |||
} | |||
public Tokenizer getTokenizer() { | |||
return tokenizer; | |||
} | |||
public AbstractLanguage(Tokenizer tokenizer, String... extensions) { | |||
this.tokenizer = tokenizer; | |||
fileFilter = net.sourceforge.pmd.util.filter.Filters.toFilenameFilter(Filters.getFileExtensionOrDirectoryFilter(extensions)); | |||
} | |||
private static class ExtensionsFilter implements FilenameFilter { | |||
private final String[] extensions; | |||
public FilenameFilter getFileFilter() { | |||
return fileFilter; | |||
} | |||
public ExtensionsFilter(String... extensions) { | |||
this.extensions = new String[extensions.length]; | |||
for (int i = 0; i < extensions.length; i++) { | |||
this.extensions[i] = extensions[i].toUpperCase(); | |||
} | |||
} | |||
public Tokenizer getTokenizer() { | |||
return tokenizer; | |||
} | |||
public boolean accept(File dir, String name) { | |||
File file = new File(dir, name); | |||
if (file.isDirectory()) { | |||
return true; | |||
} | |||
String uppercaseName = name.toUpperCase(); | |||
for (String extension : extensions) { | |||
if (uppercaseName.endsWith(extension)) { | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
} | |||
} |
@@ -1,30 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/** | |||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | |||
*/ | |||
package net.sourceforge.pmd.cpd; | |||
public class AnyLanguage extends AbstractLanguage { | |||
public AnyLanguage(String extension) { | |||
super(new AnyTokenizer(), extension); | |||
} | |||
} |
@@ -1,72 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/** | |||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | |||
*/ | |||
package net.sourceforge.pmd.cpd; | |||
import java.io.BufferedReader; | |||
import java.io.CharArrayReader; | |||
import java.util.NoSuchElementException; | |||
import java.util.StringTokenizer; | |||
/** | |||
* This class does a best-guess try-anything tokenization. | |||
* | |||
* @author jheintz | |||
*/ | |||
public class AnyTokenizer implements Tokenizer { | |||
public static final String TOKENS = " \t!#$%^&*(){}-=+<>/\\`~;:"; | |||
public void tokenize(SourceCode sourceCode, Tokens tokenEntries) { | |||
StringBuffer sb = sourceCode.getCodeBuffer(); | |||
BufferedReader reader = new BufferedReader(new CharArrayReader(sb.toString().toCharArray())); | |||
try { | |||
int lineNumber = 1; | |||
String line = reader.readLine(); | |||
while (line != null) { | |||
StringTokenizer tokenizer = new StringTokenizer(line, TOKENS, true); | |||
try { | |||
String token = tokenizer.nextToken(); | |||
while (token != null) { | |||
if (!token.equals(" ") && !token.equals("\t")) { | |||
tokenEntries.add(new TokenEntry(token, sourceCode.getFileName(), lineNumber)); | |||
} | |||
token = tokenizer.nextToken(); | |||
} | |||
} catch (NoSuchElementException ex) { | |||
// done with tokens | |||
} | |||
// advance iteration variables | |||
line = reader.readLine(); | |||
lineNumber++; | |||
} | |||
} catch (Exception ex) { | |||
ex.printStackTrace(); | |||
} finally { | |||
try { | |||
reader.close(); | |||
} catch (Exception ex) { | |||
} | |||
tokenEntries.add(TokenEntry.getEOF()); | |||
} | |||
} | |||
} |
@@ -27,13 +27,13 @@ import java.io.File; | |||
public interface CPDListener { | |||
public static final int INIT = 0; | |||
public static final int HASH = 1; | |||
public static final int MATCH = 2; | |||
public static final int GROUPING = 3; | |||
public static final int DONE = 4; | |||
public static final int INIT = 0; | |||
public static final int HASH = 1; | |||
public static final int MATCH = 2; | |||
public static final int GROUPING = 3; | |||
public static final int DONE = 4; | |||
void addedFile(int fileCount, File file); | |||
void addedFile(int fileCount, File file); | |||
void phaseUpdate(int phase); | |||
void phaseUpdate(int phase); | |||
} |
@@ -26,9 +26,9 @@ package net.sourceforge.pmd.cpd; | |||
import java.io.File; | |||
public class CPDNullListener implements CPDListener { | |||
public void addedFile(int fileCount, File file) { | |||
} | |||
public void addedFile(int fileCount, File file) { | |||
} | |||
public void phaseUpdate(int phase) { | |||
} | |||
public void phaseUpdate(int phase) { | |||
} | |||
} |
@@ -1,30 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/** | |||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | |||
*/ | |||
package net.sourceforge.pmd.cpd; | |||
/** | |||
* @deprecated Use net.sourceforge.pmd.util.FileFinder instead. | |||
*/ | |||
public class FileFinder extends net.sourceforge.pmd.util.FileFinder { | |||
} |
@@ -1,67 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/** | |||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | |||
*/ | |||
package net.sourceforge.pmd.cpd; | |||
import java.io.*; | |||
/** | |||
* @author Philippe T'Seyen | |||
*/ | |||
public class FileReporter { | |||
private File reportFile; | |||
private String encoding; | |||
public FileReporter(String encoding) { | |||
this(null, encoding); | |||
} | |||
public FileReporter(File reportFile) { | |||
this(reportFile, System.getProperty("file.encoding")); | |||
} | |||
public FileReporter(File reportFile, String encoding) { | |||
this.reportFile = reportFile; | |||
this.encoding = encoding; | |||
} | |||
public void report(String content) throws ReportException { | |||
try { | |||
Writer writer = null; | |||
try { | |||
OutputStream outputStream; | |||
if (reportFile == null) { | |||
outputStream = System.out; | |||
} else { | |||
outputStream = new FileOutputStream(reportFile); | |||
} | |||
writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding)); | |||
writer.write(content); | |||
} finally { | |||
if (writer != null) writer.close(); | |||
} | |||
} catch (IOException ioe) { | |||
throw new ReportException(ioe); | |||
} | |||
} | |||
} |
@@ -26,10 +26,10 @@ package net.sourceforge.pmd.cpd; | |||
import java.io.FilenameFilter; | |||
public interface Language { | |||
String fileSeparator = System.getProperty("file.separator"); | |||
public Tokenizer getTokenizer(); | |||
public FilenameFilter getFileFilter(); | |||
String fileSeparator = System.getProperty("file.separator"); | |||
public Tokenizer getTokenizer(); | |||
public FilenameFilter getFileFilter(); | |||
} |
@@ -1,34 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/** | |||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | |||
*/ | |||
package net.sourceforge.pmd.cpd; | |||
/** | |||
* @author Philippe T'Seyen | |||
*/ | |||
public class ReportException extends Exception { | |||
public ReportException(Throwable cause) { | |||
super(); | |||
} | |||
} |
@@ -32,125 +32,125 @@ public class SourceCode { | |||
public static final String EOL = System.getProperty("line.separator", "\n"); | |||
public static abstract class CodeLoader { | |||
private SoftReference<List<String>> code; | |||
public List<String> getCode() { | |||
List<String> c = null; | |||
if (code != null) { | |||
c = code.get(); | |||
} | |||
if (c != null) { | |||
return c; | |||
} | |||
this.code = new SoftReference<List<String>>(load()); | |||
return code.get(); | |||
} | |||
public static abstract class CodeLoader { | |||
private SoftReference<List<String>> code; | |||
public abstract String getFileName(); | |||
protected abstract Reader getReader() throws Exception; | |||
protected List<String> load() { | |||
LineNumberReader lnr = null; | |||
try { | |||
lnr = new LineNumberReader(getReader()); | |||
List<String> lines = new ArrayList<String>(); | |||
String currentLine; | |||
while ((currentLine = lnr.readLine()) != null) { | |||
lines.add(currentLine); | |||
} | |||
return lines; | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage()); | |||
} finally { | |||
try { | |||
if (lnr != null) | |||
lnr.close(); | |||
} catch (Exception e) { | |||
throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage()); | |||
} | |||
} | |||
} | |||
public List<String> getCode() { | |||
List<String> c = null; | |||
if (code != null) { | |||
c = code.get(); | |||
} | |||
if (c != null) { | |||
return c; | |||
} | |||
this.code = new SoftReference<List<String>>(load()); | |||
return code.get(); | |||
} | |||
public static class FileCodeLoader extends CodeLoader { | |||
private File file; | |||
private String encoding; | |||
public abstract String getFileName(); | |||
public FileCodeLoader(File file, String encoding) { | |||
this.file = file; | |||
this.encoding = encoding; | |||
} | |||
protected abstract Reader getReader() throws Exception; | |||
public Reader getReader() throws Exception { | |||
return new InputStreamReader(new FileInputStream(file), encoding); | |||
protected List<String> load() { | |||
LineNumberReader lnr = null; | |||
try { | |||
lnr = new LineNumberReader(getReader()); | |||
List<String> lines = new ArrayList<String>(); | |||
String currentLine; | |||
while ((currentLine = lnr.readLine()) != null) { | |||
lines.add(currentLine); | |||
} | |||
public String getFileName() { | |||
return this.file.getAbsolutePath(); | |||
return lines; | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage()); | |||
} finally { | |||
try { | |||
if (lnr != null) | |||
lnr.close(); | |||
} catch (Exception e) { | |||
throw new RuntimeException("Problem while reading " + getFileName() + ":" + e.getMessage()); | |||
} | |||
} | |||
} | |||
} | |||
public static class StringCodeLoader extends CodeLoader { | |||
public static final String DEFAULT_NAME = "CODE_LOADED_FROM_STRING"; | |||
public static class FileCodeLoader extends CodeLoader { | |||
private File file; | |||
private String encoding; | |||
private String source_code; | |||
public FileCodeLoader(File file, String encoding) { | |||
this.file = file; | |||
this.encoding = encoding; | |||
} | |||
private String name; | |||
public Reader getReader() throws Exception { | |||
return new InputStreamReader(new FileInputStream(file), encoding); | |||
} | |||
public StringCodeLoader(String code) { | |||
this(code, DEFAULT_NAME); | |||
} | |||
public String getFileName() { | |||
return this.file.getAbsolutePath(); | |||
} | |||
} | |||
public StringCodeLoader(String code, String name) { | |||
this.source_code = code; | |||
this.name = name; | |||
} | |||
public static class StringCodeLoader extends CodeLoader { | |||
public static final String DEFAULT_NAME = "CODE_LOADED_FROM_STRING"; | |||
public Reader getReader() { | |||
return new StringReader(source_code); | |||
} | |||
private String source_code; | |||
public String getFileName() { | |||
return name; | |||
} | |||
} | |||
private CodeLoader cl; | |||
private String name; | |||
public SourceCode(CodeLoader cl) { | |||
this.cl = cl; | |||
public StringCodeLoader(String code) { | |||
this(code, DEFAULT_NAME); | |||
} | |||
public List<String> getCode() { | |||
return cl.getCode(); | |||
public StringCodeLoader(String code, String name) { | |||
this.source_code = code; | |||
this.name = name; | |||
} | |||
public StringBuffer getCodeBuffer() { | |||
StringBuffer sb = new StringBuffer(); | |||
List<String> lines = cl.getCode(); | |||
for ( String line : lines ) { | |||
sb.append(line); | |||
sb.append(EOL); | |||
} | |||
return sb; | |||
public Reader getReader() { | |||
return new StringReader(source_code); | |||
} | |||
public String getSlice(int startLine, int endLine) { | |||
StringBuffer sb = new StringBuffer(); | |||
List lines = cl.getCode(); | |||
for (int i = (startLine == 0 ? startLine :startLine - 1); i < endLine && i < lines.size(); i++) { | |||
if (sb.length() != 0) { | |||
sb.append(EOL); | |||
} | |||
sb.append((String) lines.get(i)); | |||
} | |||
return sb.toString(); | |||
public String getFileName() { | |||
return name; | |||
} | |||
} | |||
public String getFileName() { | |||
return cl.getFileName(); | |||
private CodeLoader cl; | |||
public SourceCode(CodeLoader cl) { | |||
this.cl = cl; | |||
} | |||
public List<String> getCode() { | |||
return cl.getCode(); | |||
} | |||
public StringBuffer getCodeBuffer() { | |||
StringBuffer sb = new StringBuffer(); | |||
List<String> lines = cl.getCode(); | |||
for (String line : lines) { | |||
sb.append(line); | |||
sb.append(EOL); | |||
} | |||
return sb; | |||
} | |||
public String getSlice(int startLine, int endLine) { | |||
StringBuffer sb = new StringBuffer(); | |||
List lines = cl.getCode(); | |||
for (int i = (startLine == 0 ? startLine : startLine - 1); i < endLine && i < lines.size(); i++) { | |||
if (sb.length() != 0) { | |||
sb.append(EOL); | |||
} | |||
sb.append((String) lines.get(i)); | |||
} | |||
return sb.toString(); | |||
} | |||
public String getFileName() { | |||
return cl.getFileName(); | |||
} | |||
} |
@@ -28,77 +28,77 @@ import java.util.Map; | |||
public class TokenEntry implements Comparable<TokenEntry> { | |||
public static final TokenEntry EOF = new TokenEntry(); | |||
private String tokenSrcID; | |||
private int beginLine; | |||
private int index; | |||
private int identifier; | |||
private int hashCode; | |||
private final static Map<String, Integer> Tokens = new HashMap<String, Integer>(); | |||
private static int TokenCount = 0; | |||
private TokenEntry() { | |||
this.identifier = 0; | |||
this.tokenSrcID = "EOFMarker"; | |||
} | |||
public TokenEntry(String image, String tokenSrcID, int beginLine) { | |||
Integer i = Tokens.get(image); | |||
if (i == null) { | |||
i = Tokens.size() + 1; | |||
Tokens.put(image, i); | |||
} | |||
this.identifier = i.intValue(); | |||
this.tokenSrcID = tokenSrcID; | |||
this.beginLine = beginLine; | |||
this.index = TokenCount++; | |||
} | |||
public static TokenEntry getEOF() { | |||
TokenCount++; | |||
return EOF; | |||
} | |||
public static void clearImages() { | |||
Tokens.clear(); | |||
TokenCount = 0; | |||
} | |||
public String getTokenSrcID() { | |||
return tokenSrcID; | |||
} | |||
public int getBeginLine() { | |||
return beginLine; | |||
} | |||
public int getIdentifier() { | |||
return this.identifier; | |||
} | |||
public int getIndex() { | |||
return this.index; | |||
} | |||
public int hashCode() { | |||
return hashCode; | |||
} | |||
public void setHashCode(int hashCode) { | |||
this.hashCode = hashCode; | |||
public static final TokenEntry EOF = new TokenEntry(); | |||
private String tokenSrcID; | |||
private int beginLine; | |||
private int index; | |||
private int identifier; | |||
private int hashCode; | |||
private final static Map<String, Integer> Tokens = new HashMap<String, Integer>(); | |||
private static int TokenCount = 0; | |||
private TokenEntry() { | |||
this.identifier = 0; | |||
this.tokenSrcID = "EOFMarker"; | |||
} | |||
public TokenEntry(String image, String tokenSrcID, int beginLine) { | |||
Integer i = Tokens.get(image); | |||
if (i == null) { | |||
i = Tokens.size() + 1; | |||
Tokens.put(image, i); | |||
} | |||
public boolean equals(Object o) { | |||
if (!(o instanceof TokenEntry)) { | |||
return false; | |||
} | |||
TokenEntry other = (TokenEntry) o; | |||
return other.hashCode == hashCode; | |||
} | |||
public int compareTo(TokenEntry other) { | |||
return getIndex() - other.getIndex(); | |||
this.identifier = i.intValue(); | |||
this.tokenSrcID = tokenSrcID; | |||
this.beginLine = beginLine; | |||
this.index = TokenCount++; | |||
} | |||
public static TokenEntry getEOF() { | |||
TokenCount++; | |||
return EOF; | |||
} | |||
public static void clearImages() { | |||
Tokens.clear(); | |||
TokenCount = 0; | |||
} | |||
public String getTokenSrcID() { | |||
return tokenSrcID; | |||
} | |||
public int getBeginLine() { | |||
return beginLine; | |||
} | |||
public int getIdentifier() { | |||
return this.identifier; | |||
} | |||
public int getIndex() { | |||
return this.index; | |||
} | |||
public int hashCode() { | |||
return hashCode; | |||
} | |||
public void setHashCode(int hashCode) { | |||
this.hashCode = hashCode; | |||
} | |||
public boolean equals(Object o) { | |||
if (!(o instanceof TokenEntry)) { | |||
return false; | |||
} | |||
} | |||
TokenEntry other = (TokenEntry) o; | |||
return other.hashCode == hashCode; | |||
} | |||
public int compareTo(TokenEntry other) { | |||
return getIndex() - other.getIndex(); | |||
} | |||
} |
@@ -26,5 +26,5 @@ package net.sourceforge.pmd.cpd; | |||
import java.io.IOException; | |||
public interface Tokenizer { | |||
void tokenize(SourceCode tokens, Tokens tokenEntries) throws IOException; | |||
void tokenize(SourceCode tokens, Tokens tokenEntries) throws IOException; | |||
} |
@@ -31,34 +31,34 @@ import java.util.List; | |||
public class Tokens { | |||
private List<TokenEntry> tokens = new ArrayList<TokenEntry>(); | |||
private List<TokenEntry> tokens = new ArrayList<TokenEntry>(); | |||
public void add(TokenEntry tokenEntry) { | |||
this.tokens.add(tokenEntry); | |||
} | |||
public void add(TokenEntry tokenEntry) { | |||
this.tokens.add(tokenEntry); | |||
} | |||
public Iterator<TokenEntry> iterator() { | |||
return tokens.iterator(); | |||
} | |||
public Iterator<TokenEntry> iterator() { | |||
return tokens.iterator(); | |||
} | |||
private TokenEntry get(int index) { | |||
return tokens.get(index); | |||
} | |||
private TokenEntry get(int index) { | |||
return tokens.get(index); | |||
} | |||
public int size() { | |||
return tokens.size(); | |||
} | |||
public int size() { | |||
return tokens.size(); | |||
} | |||
public int getLineCount(TokenEntry mark, Match match) { | |||
TokenEntry endTok = get(mark.getIndex() + match.getTokenCount() - 1); | |||
if (endTok == TokenEntry.EOF) { | |||
endTok = get(mark.getIndex() + match.getTokenCount() - 2); | |||
} | |||
return endTok.getBeginLine() - mark.getBeginLine() + 1; | |||
public int getLineCount(TokenEntry mark, Match match) { | |||
TokenEntry endTok = get(mark.getIndex() + match.getTokenCount() - 1); | |||
if (endTok == TokenEntry.EOF) { | |||
endTok = get(mark.getIndex() + match.getTokenCount() - 2); | |||
} | |||
return endTok.getBeginLine() - mark.getBeginLine() + 1; | |||
} | |||
public List<TokenEntry> getTokens() { | |||
return tokens; | |||
} | |||
public List<TokenEntry> getTokens() { | |||
return tokens; | |||
} | |||
} |
@@ -1,417 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/* Generated By:JavaCC: Do not edit this line. CPPParserConstants.java */ | |||
package net.sourceforge.pmd.cpd.cppast; | |||
/** | |||
* Token literal values and constants. | |||
* Generated by org.javacc.parser.OtherFilesGen#start() | |||
*/ | |||
public interface CPPParserConstants { | |||
/** End of File. */ | |||
int EOF = 0; | |||
/** RegularExpression Id. */ | |||
int LCURLYBRACE = 19; | |||
/** RegularExpression Id. */ | |||
int RCURLYBRACE = 20; | |||
/** RegularExpression Id. */ | |||
int LSQUAREBRACKET = 21; | |||
/** RegularExpression Id. */ | |||
int RSQUAREBRACKET = 22; | |||
/** RegularExpression Id. */ | |||
int LPARENTHESIS = 23; | |||
/** RegularExpression Id. */ | |||
int RPARENTHESIS = 24; | |||
/** RegularExpression Id. */ | |||
int SCOPE = 25; | |||
/** RegularExpression Id. */ | |||
int COLON = 26; | |||
/** RegularExpression Id. */ | |||
int SEMICOLON = 27; | |||
/** RegularExpression Id. */ | |||
int COMMA = 28; | |||
/** RegularExpression Id. */ | |||
int QUESTIONMARK = 29; | |||
/** RegularExpression Id. */ | |||
int ELLIPSIS = 30; | |||
/** RegularExpression Id. */ | |||
int ASSIGNEQUAL = 31; | |||
/** RegularExpression Id. */ | |||
int TIMESEQUAL = 32; | |||
/** RegularExpression Id. */ | |||
int DIVIDEEQUAL = 33; | |||
/** RegularExpression Id. */ | |||
int MODEQUAL = 34; | |||
/** RegularExpression Id. */ | |||
int PLUSEQUAL = 35; | |||
/** RegularExpression Id. */ | |||
int MINUSEQUAL = 36; | |||
/** RegularExpression Id. */ | |||
int SHIFTLEFTEQUAL = 37; | |||
/** RegularExpression Id. */ | |||
int SHIFTRIGHTEQUAL = 38; | |||
/** RegularExpression Id. */ | |||
int BITWISEANDEQUAL = 39; | |||
/** RegularExpression Id. */ | |||
int BITWISEXOREQUAL = 40; | |||
/** RegularExpression Id. */ | |||
int BITWISEOREQUAL = 41; | |||
/** RegularExpression Id. */ | |||
int OR = 42; | |||
/** RegularExpression Id. */ | |||
int AND = 43; | |||
/** RegularExpression Id. */ | |||
int BITWISEOR = 44; | |||
/** RegularExpression Id. */ | |||
int BITWISEXOR = 45; | |||
/** RegularExpression Id. */ | |||
int AMPERSAND = 46; | |||
/** RegularExpression Id. */ | |||
int EQUAL = 47; | |||
/** RegularExpression Id. */ | |||
int NOTEQUAL = 48; | |||
/** RegularExpression Id. */ | |||
int LESSTHAN = 49; | |||
/** RegularExpression Id. */ | |||
int GREATERTHAN = 50; | |||
/** RegularExpression Id. */ | |||
int LESSTHANOREQUALTO = 51; | |||
/** RegularExpression Id. */ | |||
int GREATERTHANOREQUALTO = 52; | |||
/** RegularExpression Id. */ | |||
int SHIFTLEFT = 53; | |||
/** RegularExpression Id. */ | |||
int SHIFTRIGHT = 54; | |||
/** RegularExpression Id. */ | |||
int PLUS = 55; | |||
/** RegularExpression Id. */ | |||
int MINUS = 56; | |||
/** RegularExpression Id. */ | |||
int STAR = 57; | |||
/** RegularExpression Id. */ | |||
int DIVIDE = 58; | |||
/** RegularExpression Id. */ | |||
int MOD = 59; | |||
/** RegularExpression Id. */ | |||
int PLUSPLUS = 60; | |||
/** RegularExpression Id. */ | |||
int MINUSMINUS = 61; | |||
/** RegularExpression Id. */ | |||
int TILDE = 62; | |||
/** RegularExpression Id. */ | |||
int NOT = 63; | |||
/** RegularExpression Id. */ | |||
int DOT = 64; | |||
/** RegularExpression Id. */ | |||
int POINTERTO = 65; | |||
/** RegularExpression Id. */ | |||
int DOTSTAR = 66; | |||
/** RegularExpression Id. */ | |||
int ARROWSTAR = 67; | |||
/** RegularExpression Id. */ | |||
int AUTO = 68; | |||
/** RegularExpression Id. */ | |||
int BREAK = 69; | |||
/** RegularExpression Id. */ | |||
int CASE = 70; | |||
/** RegularExpression Id. */ | |||
int CATCH = 71; | |||
/** RegularExpression Id. */ | |||
int CHAR = 72; | |||
/** RegularExpression Id. */ | |||
int CONST = 73; | |||
/** RegularExpression Id. */ | |||
int CONTINUE = 74; | |||
/** RegularExpression Id. */ | |||
int _DEFAULT = 75; | |||
/** RegularExpression Id. */ | |||
int DELETE = 76; | |||
/** RegularExpression Id. */ | |||
int DO = 77; | |||
/** RegularExpression Id. */ | |||
int DOUBLE = 78; | |||
/** RegularExpression Id. */ | |||
int ELSE = 79; | |||
/** RegularExpression Id. */ | |||
int ENUM = 80; | |||
/** RegularExpression Id. */ | |||
int EXTERN = 81; | |||
/** RegularExpression Id. */ | |||
int FLOAT = 82; | |||
/** RegularExpression Id. */ | |||
int FOR = 83; | |||
/** RegularExpression Id. */ | |||
int FRIEND = 84; | |||
/** RegularExpression Id. */ | |||
int GOTO = 85; | |||
/** RegularExpression Id. */ | |||
int IF = 86; | |||
/** RegularExpression Id. */ | |||
int INLINE = 87; | |||
/** RegularExpression Id. */ | |||
int INT = 88; | |||
/** RegularExpression Id. */ | |||
int LONG = 89; | |||
/** RegularExpression Id. */ | |||
int NEW = 90; | |||
/** RegularExpression Id. */ | |||
int PRIVATE = 91; | |||
/** RegularExpression Id. */ | |||
int PROTECTED = 92; | |||
/** RegularExpression Id. */ | |||
int PUBLIC = 93; | |||
/** RegularExpression Id. */ | |||
int REDECLARED = 94; | |||
/** RegularExpression Id. */ | |||
int REGISTER = 95; | |||
/** RegularExpression Id. */ | |||
int RETURN = 96; | |||
/** RegularExpression Id. */ | |||
int SHORT = 97; | |||
/** RegularExpression Id. */ | |||
int SIGNED = 98; | |||
/** RegularExpression Id. */ | |||
int SIZEOF = 99; | |||
/** RegularExpression Id. */ | |||
int STATIC = 100; | |||
/** RegularExpression Id. */ | |||
int STRUCT = 101; | |||
/** RegularExpression Id. */ | |||
int CLASS = 102; | |||
/** RegularExpression Id. */ | |||
int SWITCH = 103; | |||
/** RegularExpression Id. */ | |||
int TEMPLATE = 104; | |||
/** RegularExpression Id. */ | |||
int THIS = 105; | |||
/** RegularExpression Id. */ | |||
int TRY = 106; | |||
/** RegularExpression Id. */ | |||
int TYPEDEF = 107; | |||
/** RegularExpression Id. */ | |||
int UNION = 108; | |||
/** RegularExpression Id. */ | |||
int UNSIGNED = 109; | |||
/** RegularExpression Id. */ | |||
int VIRTUAL = 110; | |||
/** RegularExpression Id. */ | |||
int VOID = 111; | |||
/** RegularExpression Id. */ | |||
int VOLATILE = 112; | |||
/** RegularExpression Id. */ | |||
int WHILE = 113; | |||
/** RegularExpression Id. */ | |||
int OPERATOR = 114; | |||
/** RegularExpression Id. */ | |||
int TRUETOK = 115; | |||
/** RegularExpression Id. */ | |||
int FALSETOK = 116; | |||
/** RegularExpression Id. */ | |||
int THROW = 117; | |||
/** RegularExpression Id. */ | |||
int OCTALINT = 118; | |||
/** RegularExpression Id. */ | |||
int OCTALLONG = 119; | |||
/** RegularExpression Id. */ | |||
int UNSIGNED_OCTALINT = 120; | |||
/** RegularExpression Id. */ | |||
int UNSIGNED_OCTALLONG = 121; | |||
/** RegularExpression Id. */ | |||
int DECIMALINT = 122; | |||
/** RegularExpression Id. */ | |||
int DECIMALLONG = 123; | |||
/** RegularExpression Id. */ | |||
int UNSIGNED_DECIMALINT = 124; | |||
/** RegularExpression Id. */ | |||
int UNSIGNED_DECIMALLONG = 125; | |||
/** RegularExpression Id. */ | |||
int HEXADECIMALINT = 126; | |||
/** RegularExpression Id. */ | |||
int HEXADECIMALLONG = 127; | |||
/** RegularExpression Id. */ | |||
int UNSIGNED_HEXADECIMALINT = 128; | |||
/** RegularExpression Id. */ | |||
int UNSIGNED_HEXADECIMALLONG = 129; | |||
/** RegularExpression Id. */ | |||
int FLOATONE = 130; | |||
/** RegularExpression Id. */ | |||
int FLOATTWO = 131; | |||
/** RegularExpression Id. */ | |||
int CHARACTER = 132; | |||
/** RegularExpression Id. */ | |||
int STRING = 133; | |||
/** RegularExpression Id. */ | |||
int ID = 135; | |||
/** Lexical state. */ | |||
int DEFAULT = 0; | |||
/** Lexical state. */ | |||
int IN_LINE_COMMENT = 1; | |||
/** Lexical state. */ | |||
int IN_COMMENT = 2; | |||
/** Lexical state. */ | |||
int IN_PREPROCESSOR_OUTPUT_COMMENT = 3; | |||
/** Lexical state. */ | |||
int PREPROCESSOR_OUTPUT = 4; | |||
/** Literal token values. */ | |||
String[] tokenImage = { | |||
"<EOF>", | |||
"\" \"", | |||
"\"\\f\"", | |||
"\"\\t\"", | |||
"\"\\r\\n\"", | |||
"\"\\n\"", | |||
"\"//\"", | |||
"\"/*\"", | |||
"\"#\"", | |||
"\"\\n\"", | |||
"<token of kind 10>", | |||
"\"*/\"", | |||
"<token of kind 12>", | |||
"\"*/\"", | |||
"\"\\n\"", | |||
"\"/*\"", | |||
"\"\\\\\\n\"", | |||
"\"\\\\\\r\\n\"", | |||
"<token of kind 18>", | |||
"\"{\"", | |||
"\"}\"", | |||
"\"[\"", | |||
"\"]\"", | |||
"\"(\"", | |||
"\")\"", | |||
"\"::\"", | |||
"\":\"", | |||
"\";\"", | |||
"\",\"", | |||
"\"?\"", | |||
"\"...\"", | |||
"\"=\"", | |||
"\"*=\"", | |||
"\"/=\"", | |||
"\"%=\"", | |||
"\"+=\"", | |||
"\"-=\"", | |||
"\"<<=\"", | |||
"\">>=\"", | |||
"\"&=\"", | |||
"\"^=\"", | |||
"\"|=\"", | |||
"\"||\"", | |||
"\"&&\"", | |||
"\"|\"", | |||
"\"^\"", | |||
"\"&\"", | |||
"\"==\"", | |||
"\"!=\"", | |||
"\"<\"", | |||
"\">\"", | |||
"\"<=\"", | |||
"\">=\"", | |||
"\"<<\"", | |||
"\">>\"", | |||
"\"+\"", | |||
"\"-\"", | |||
"\"*\"", | |||
"\"/\"", | |||
"\"%\"", | |||
"\"++\"", | |||
"\"--\"", | |||
"\"~\"", | |||
"\"!\"", | |||
"\".\"", | |||
"\"->\"", | |||
"\".*\"", | |||
"\"->*\"", | |||
"\"auto\"", | |||
"\"break\"", | |||
"\"case\"", | |||
"\"catch\"", | |||
"\"char\"", | |||
"\"const\"", | |||
"\"continue\"", | |||
"\"default\"", | |||
"\"delete\"", | |||
"\"do\"", | |||
"\"double\"", | |||
"\"else\"", | |||
"\"enum\"", | |||
"\"extern\"", | |||
"\"float\"", | |||
"\"for\"", | |||
"\"friend\"", | |||
"\"goto\"", | |||
"\"if\"", | |||
"\"inline\"", | |||
"\"int\"", | |||
"\"long\"", | |||
"\"new\"", | |||
"\"private\"", | |||
"\"protected\"", | |||
"\"public\"", | |||
"\"redeclared\"", | |||
"\"register\"", | |||
"\"return\"", | |||
"\"short\"", | |||
"\"signed\"", | |||
"\"sizeof\"", | |||
"\"static\"", | |||
"\"struct\"", | |||
"\"class\"", | |||
"\"switch\"", | |||
"\"template\"", | |||
"\"this\"", | |||
"\"try\"", | |||
"\"typedef\"", | |||
"\"union\"", | |||
"\"unsigned\"", | |||
"\"virtual\"", | |||
"\"void\"", | |||
"\"volatile\"", | |||
"\"while\"", | |||
"\"operator\"", | |||
"\"true\"", | |||
"\"false\"", | |||
"\"throw\"", | |||
"<OCTALINT>", | |||
"<OCTALLONG>", | |||
"<UNSIGNED_OCTALINT>", | |||
"<UNSIGNED_OCTALLONG>", | |||
"<DECIMALINT>", | |||
"<DECIMALLONG>", | |||
"<UNSIGNED_DECIMALINT>", | |||
"<UNSIGNED_DECIMALLONG>", | |||
"<HEXADECIMALINT>", | |||
"<HEXADECIMALLONG>", | |||
"<UNSIGNED_HEXADECIMALINT>", | |||
"<UNSIGNED_HEXADECIMALLONG>", | |||
"<FLOATONE>", | |||
"<FLOATTWO>", | |||
"<CHARACTER>", | |||
"<STRING>", | |||
"\"finally\"", | |||
"<ID>", | |||
}; | |||
} |
@@ -1,218 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 4.1 */ | |||
/* JavaCCOptions:KEEP_LINE_COL=null */ | |||
package net.sourceforge.pmd.cpd.cppast; | |||
/** | |||
* This exception is thrown when parse errors are encountered. | |||
* You can explicitly create objects of this exception type by | |||
* calling the method generateParseException in the generated | |||
* parser. | |||
* | |||
* You can modify this class to customize your error reporting | |||
* mechanisms so long as you retain the public fields. | |||
*/ | |||
public class ParseException extends RuntimeException { | |||
/** | |||
* This constructor is used by the method "generateParseException" | |||
* in the generated parser. Calling this constructor generates | |||
* a new object of this type with the fields "currentToken", | |||
* "expectedTokenSequences", and "tokenImage" set. The boolean | |||
* flag "specialConstructor" is also set to true to indicate that | |||
* this constructor was used to create this object. | |||
* This constructor calls its super class with the empty string | |||
* to force the "toString" method of parent class "Throwable" to | |||
* print the error message in the form: | |||
* ParseException: <result of getMessage> | |||
*/ | |||
public ParseException(Token currentTokenVal, | |||
int[][] expectedTokenSequencesVal, | |||
String[] tokenImageVal | |||
) | |||
{ | |||
super(""); | |||
specialConstructor = true; | |||
currentToken = currentTokenVal; | |||
expectedTokenSequences = expectedTokenSequencesVal; | |||
tokenImage = tokenImageVal; | |||
} | |||
/** | |||
* The following constructors are for use by you for whatever | |||
* purpose you can think of. Constructing the exception in this | |||
* manner makes the exception behave in the normal way - i.e., as | |||
* documented in the class "Throwable". The fields "errorToken", | |||
* "expectedTokenSequences", and "tokenImage" do not contain | |||
* relevant information. The JavaCC generated code does not use | |||
* these constructors. | |||
*/ | |||
public ParseException() { | |||
super(); | |||
specialConstructor = false; | |||
} | |||
/** Constructor with message. */ | |||
public ParseException(String message) { | |||
super(message); | |||
specialConstructor = false; | |||
} | |||
/** | |||
* This variable determines which constructor was used to create | |||
* this object and thereby affects the semantics of the | |||
* "getMessage" method (see below). | |||
*/ | |||
protected boolean specialConstructor; | |||
/** | |||
* This is the last token that has been consumed successfully. If | |||
* this object has been created due to a parse error, the token | |||
* followng this token will (therefore) be the first error token. | |||
*/ | |||
public Token currentToken; | |||
/** | |||
* Each entry in this array is an array of integers. Each array | |||
* of integers represents a sequence of tokens (by their ordinal | |||
* values) that is expected at this point of the parse. | |||
*/ | |||
public int[][] expectedTokenSequences; | |||
/** | |||
* This is a reference to the "tokenImage" array of the generated | |||
* parser within which the parse error occurred. This array is | |||
* defined in the generated ...Constants interface. | |||
*/ | |||
public String[] tokenImage; | |||
/** | |||
* This method has the standard behavior when this object has been | |||
* created using the standard constructors. Otherwise, it uses | |||
* "currentToken" and "expectedTokenSequences" to generate a parse | |||
* error message and returns it. If this object has been created | |||
* due to a parse error, and you do not catch it (it gets thrown | |||
* from the parser), then this method is called during the printing | |||
* of the final stack trace, and hence the correct error message | |||
* gets displayed. | |||
*/ | |||
public String getMessage() { | |||
if (!specialConstructor) { | |||
return super.getMessage(); | |||
} | |||
StringBuffer expected = new StringBuffer(); | |||
int maxSize = 0; | |||
for (int i = 0; i < expectedTokenSequences.length; i++) { | |||
if (maxSize < expectedTokenSequences[i].length) { | |||
maxSize = expectedTokenSequences[i].length; | |||
} | |||
for (int j = 0; j < expectedTokenSequences[i].length; j++) { | |||
expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' '); | |||
} | |||
if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { | |||
expected.append("..."); | |||
} | |||
expected.append(eol).append(" "); | |||
} | |||
String retval = "Encountered \""; | |||
Token tok = currentToken.next; | |||
for (int i = 0; i < maxSize; i++) { | |||
if (i != 0) retval += " "; | |||
if (tok.kind == 0) { | |||
retval += tokenImage[0]; | |||
break; | |||
} | |||
retval += " " + tokenImage[tok.kind]; | |||
retval += " \""; | |||
retval += add_escapes(tok.image); | |||
retval += " \""; | |||
tok = tok.next; | |||
} | |||
retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; | |||
retval += "." + eol; | |||
if (expectedTokenSequences.length == 1) { | |||
retval += "Was expecting:" + eol + " "; | |||
} else { | |||
retval += "Was expecting one of:" + eol + " "; | |||
} | |||
retval += expected.toString(); | |||
return retval; | |||
} | |||
/** | |||
* The end of line string for this machine. | |||
*/ | |||
protected String eol = System.getProperty("line.separator", "\n"); | |||
/** | |||
* Used to convert raw characters to their escaped version | |||
* when these raw version cannot be used as part of an ASCII | |||
* string literal. | |||
*/ | |||
protected String add_escapes(String str) { | |||
StringBuffer retval = new StringBuffer(); | |||
char ch; | |||
for (int i = 0; i < str.length(); i++) { | |||
switch (str.charAt(i)) | |||
{ | |||
case 0 : | |||
continue; | |||
case '\b': | |||
retval.append("\\b"); | |||
continue; | |||
case '\t': | |||
retval.append("\\t"); | |||
continue; | |||
case '\n': | |||
retval.append("\\n"); | |||
continue; | |||
case '\f': | |||
retval.append("\\f"); | |||
continue; | |||
case '\r': | |||
retval.append("\\r"); | |||
continue; | |||
case '\"': | |||
retval.append("\\\""); | |||
continue; | |||
case '\'': | |||
retval.append("\\\'"); | |||
continue; | |||
case '\\': | |||
retval.append("\\\\"); | |||
continue; | |||
default: | |||
if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { | |||
String s = "0000" + Integer.toString(ch, 16); | |||
retval.append("\\u" + s.substring(s.length() - 4, s.length())); | |||
} else { | |||
retval.append(ch); | |||
} | |||
continue; | |||
} | |||
} | |||
return retval.toString(); | |||
} | |||
} | |||
/* JavaCC - OriginalChecksum=8b42f597f21215eb130252440c369111 (do not edit this line) */ |
@@ -1,496 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.1 */ | |||
/* JavaCCOptions:STATIC=true */ | |||
package net.sourceforge.pmd.cpd.cppast; | |||
/** | |||
* An implementation of interface CharStream, where the stream is assumed to | |||
* contain only ASCII characters (without unicode processing). | |||
*/ | |||
public class SimpleCharStream | |||
{ | |||
/** Whether parser is static. */ | |||
public static final boolean staticFlag = true; | |||
static int bufsize; | |||
static int available; | |||
static int tokenBegin; | |||
/** Position in buffer. */ | |||
static public int bufpos = -1; | |||
static protected int bufline[]; | |||
static protected int bufcolumn[]; | |||
static protected int column = 0; | |||
static protected int line = 1; | |||
static protected boolean prevCharIsCR = false; | |||
static protected boolean prevCharIsLF = false; | |||
static protected java.io.Reader inputStream; | |||
static protected char[] buffer; | |||
static protected int maxNextCharInd = 0; | |||
static protected int inBuf = 0; | |||
static protected int tabSize = 8; | |||
static protected void setTabSize(int i) { tabSize = i; } | |||
static protected int getTabSize(int i) { return tabSize; } | |||
static protected void ExpandBuff(boolean wrapAround) | |||
{ | |||
char[] newbuffer = new char[bufsize + 2048]; | |||
int newbufline[] = new int[bufsize + 2048]; | |||
int newbufcolumn[] = new int[bufsize + 2048]; | |||
try | |||
{ | |||
if (wrapAround) | |||
{ | |||
System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); | |||
System.arraycopy(buffer, 0, newbuffer, | |||
bufsize - tokenBegin, bufpos); | |||
buffer = newbuffer; | |||
System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); | |||
System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos); | |||
bufline = newbufline; | |||
System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); | |||
System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos); | |||
bufcolumn = newbufcolumn; | |||
maxNextCharInd = (bufpos += (bufsize - tokenBegin)); | |||
} | |||
else | |||
{ | |||
System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin); | |||
buffer = newbuffer; | |||
System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin); | |||
bufline = newbufline; | |||
System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin); | |||
bufcolumn = newbufcolumn; | |||
maxNextCharInd = (bufpos -= tokenBegin); | |||
} | |||
} | |||
catch (Throwable t) | |||
{ | |||
throw new Error(t.getMessage()); | |||
} | |||
bufsize += 2048; | |||
available = bufsize; | |||
tokenBegin = 0; | |||
} | |||
static protected void FillBuff() throws java.io.IOException | |||
{ | |||
if (maxNextCharInd == available) | |||
{ | |||
if (available == bufsize) | |||
{ | |||
if (tokenBegin > 2048) | |||
{ | |||
bufpos = maxNextCharInd = 0; | |||
available = tokenBegin; | |||
} | |||
else if (tokenBegin < 0) | |||
bufpos = maxNextCharInd = 0; | |||
else | |||
ExpandBuff(false); | |||
} | |||
else if (available > tokenBegin) | |||
available = bufsize; | |||
else if ((tokenBegin - available) < 2048) | |||
ExpandBuff(true); | |||
else | |||
available = tokenBegin; | |||
} | |||
int i; | |||
try { | |||
if ((i = inputStream.read(buffer, maxNextCharInd, | |||
available - maxNextCharInd)) == -1) | |||
{ | |||
inputStream.close(); | |||
throw new java.io.IOException(); | |||
} | |||
else | |||
maxNextCharInd += i; | |||
return; | |||
} | |||
catch(java.io.IOException e) { | |||
--bufpos; | |||
backup(0); | |||
if (tokenBegin == -1) | |||
tokenBegin = bufpos; | |||
throw e; | |||
} | |||
} | |||
/** Start. */ | |||
static public char BeginToken() throws java.io.IOException | |||
{ | |||
tokenBegin = -1; | |||
char c = readChar(); | |||
tokenBegin = bufpos; | |||
return c; | |||
} | |||
static protected void UpdateLineColumn(char c) | |||
{ | |||
column++; | |||
if (prevCharIsLF) | |||
{ | |||
prevCharIsLF = false; | |||
line += (column = 1); | |||
} | |||
else if (prevCharIsCR) | |||
{ | |||
prevCharIsCR = false; | |||
if (c == '\n') | |||
{ | |||
prevCharIsLF = true; | |||
} | |||
else | |||
line += (column = 1); | |||
} | |||
switch (c) | |||
{ | |||
case '\r' : | |||
prevCharIsCR = true; | |||
break; | |||
case '\n' : | |||
prevCharIsLF = true; | |||
break; | |||
case '\t' : | |||
column--; | |||
column += (tabSize - (column % tabSize)); | |||
break; | |||
default : | |||
break; | |||
} | |||
bufline[bufpos] = line; | |||
bufcolumn[bufpos] = column; | |||
} | |||
/** Read a character. */ | |||
static public char readChar() throws java.io.IOException | |||
{ | |||
if (inBuf > 0) | |||
{ | |||
--inBuf; | |||
if (++bufpos == bufsize) | |||
bufpos = 0; | |||
return buffer[bufpos]; | |||
} | |||
if (++bufpos >= maxNextCharInd) | |||
FillBuff(); | |||
char c = buffer[bufpos]; | |||
UpdateLineColumn(c); | |||
return c; | |||
} | |||
/** | |||
* @deprecated | |||
* @see #getEndColumn | |||
*/ | |||
static public int getColumn() { | |||
return bufcolumn[bufpos]; | |||
} | |||
/** | |||
* @deprecated | |||
* @see #getEndLine | |||
*/ | |||
static public int getLine() { | |||
return bufline[bufpos]; | |||
} | |||
/** Get token end column number. */ | |||
static public int getEndColumn() { | |||
return bufcolumn[bufpos]; | |||
} | |||
/** Get token end line number. */ | |||
static public int getEndLine() { | |||
return bufline[bufpos]; | |||
} | |||
/** Get token beginning column number. */ | |||
static public int getBeginColumn() { | |||
return bufcolumn[tokenBegin]; | |||
} | |||
/** Get token beginning line number. */ | |||
static public int getBeginLine() { | |||
return bufline[tokenBegin]; | |||
} | |||
/** Backup a number of characters. */ | |||
static public void backup(int amount) { | |||
inBuf += amount; | |||
if ((bufpos -= amount) < 0) | |||
bufpos += bufsize; | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.Reader dstream, int startline, | |||
int startcolumn, int buffersize) | |||
{ | |||
if (inputStream != null) | |||
throw new Error("\n ERROR: Second call to the constructor of a static SimpleCharStream.\n" + | |||
" You must either use ReInit() or set the JavaCC option STATIC to false\n" + | |||
" during the generation of this class."); | |||
inputStream = dstream; | |||
line = startline; | |||
column = startcolumn - 1; | |||
available = bufsize = buffersize; | |||
buffer = new char[buffersize]; | |||
bufline = new int[buffersize]; | |||
bufcolumn = new int[buffersize]; | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.Reader dstream, int startline, | |||
int startcolumn) | |||
{ | |||
this(dstream, startline, startcolumn, 4096); | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.Reader dstream) | |||
{ | |||
this(dstream, 1, 1, 4096); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.Reader dstream, int startline, | |||
int startcolumn, int buffersize) | |||
{ | |||
inputStream = dstream; | |||
line = startline; | |||
column = startcolumn - 1; | |||
if (buffer == null || buffersize != buffer.length) | |||
{ | |||
available = bufsize = buffersize; | |||
buffer = new char[buffersize]; | |||
bufline = new int[buffersize]; | |||
bufcolumn = new int[buffersize]; | |||
} | |||
prevCharIsLF = prevCharIsCR = false; | |||
tokenBegin = inBuf = maxNextCharInd = 0; | |||
bufpos = -1; | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.Reader dstream, int startline, | |||
int startcolumn) | |||
{ | |||
ReInit(dstream, startline, startcolumn, 4096); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.Reader dstream) | |||
{ | |||
ReInit(dstream, 1, 1, 4096); | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, | |||
int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException | |||
{ | |||
this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.InputStream dstream, int startline, | |||
int startcolumn, int buffersize) | |||
{ | |||
this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline, | |||
int startcolumn) throws java.io.UnsupportedEncodingException | |||
{ | |||
this(dstream, encoding, startline, startcolumn, 4096); | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.InputStream dstream, int startline, | |||
int startcolumn) | |||
{ | |||
this(dstream, startline, startcolumn, 4096); | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException | |||
{ | |||
this(dstream, encoding, 1, 1, 4096); | |||
} | |||
/** Constructor. */ | |||
public SimpleCharStream(java.io.InputStream dstream) | |||
{ | |||
this(dstream, 1, 1, 4096); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.InputStream dstream, String encoding, int startline, | |||
int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException | |||
{ | |||
ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.InputStream dstream, int startline, | |||
int startcolumn, int buffersize) | |||
{ | |||
ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException | |||
{ | |||
ReInit(dstream, encoding, 1, 1, 4096); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.InputStream dstream) | |||
{ | |||
ReInit(dstream, 1, 1, 4096); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.InputStream dstream, String encoding, int startline, | |||
int startcolumn) throws java.io.UnsupportedEncodingException | |||
{ | |||
ReInit(dstream, encoding, startline, startcolumn, 4096); | |||
} | |||
/** Reinitialise. */ | |||
public void ReInit(java.io.InputStream dstream, int startline, | |||
int startcolumn) | |||
{ | |||
ReInit(dstream, startline, startcolumn, 4096); | |||
} | |||
/** Get token literal value. */ | |||
static public String GetImage() | |||
{ | |||
if (bufpos >= tokenBegin) | |||
return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); | |||
else | |||
return new String(buffer, tokenBegin, bufsize - tokenBegin) + | |||
new String(buffer, 0, bufpos + 1); | |||
} | |||
/** Get the suffix. */ | |||
static public char[] GetSuffix(int len) | |||
{ | |||
char[] ret = new char[len]; | |||
if ((bufpos + 1) >= len) | |||
System.arraycopy(buffer, bufpos - len + 1, ret, 0, len); | |||
else | |||
{ | |||
System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0, | |||
len - bufpos - 1); | |||
System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1); | |||
} | |||
return ret; | |||
} | |||
/** Reset buffer when finished. */ | |||
static public void Done() | |||
{ | |||
buffer = null; | |||
bufline = null; | |||
bufcolumn = null; | |||
} | |||
/** | |||
* Method to adjust line and column numbers for the start of a token. | |||
*/ | |||
static public void adjustBeginLineColumn(int newLine, int newCol) | |||
{ | |||
int start = tokenBegin; | |||
int len; | |||
if (bufpos >= tokenBegin) | |||
{ | |||
len = bufpos - tokenBegin + inBuf + 1; | |||
} | |||
else | |||
{ | |||
len = bufsize - tokenBegin + bufpos + 1 + inBuf; | |||
} | |||
int i = 0, j = 0, k = 0; | |||
int nextColDiff = 0, columnDiff = 0; | |||
while (i < len && | |||
bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) | |||
{ | |||
bufline[j] = newLine; | |||
nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j]; | |||
bufcolumn[j] = newCol + columnDiff; | |||
columnDiff = nextColDiff; | |||
i++; | |||
} | |||
if (i < len) | |||
{ | |||
bufline[j] = newLine++; | |||
bufcolumn[j] = newCol + columnDiff; | |||
while (i++ < len) | |||
{ | |||
if (bufline[j = start % bufsize] != bufline[++start % bufsize]) | |||
bufline[j] = newLine++; | |||
else | |||
bufline[j] = newLine; | |||
} | |||
} | |||
line = bufline[j]; | |||
column = bufcolumn[j]; | |||
} | |||
} | |||
/* JavaCC - OriginalChecksum=9bbb8cb4295bb8f7d58e31ce57dc2e0f (do not edit this line) */ |
@@ -1,144 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/* Generated By:JavaCC: Do not edit this line. Token.java Version 4.1 */ | |||
/* JavaCCOptions:TOKEN_EXTENDS=,KEEP_LINE_COL=null */ | |||
package net.sourceforge.pmd.cpd.cppast; | |||
/** | |||
* Describes the input token stream. | |||
*/ | |||
public class Token { | |||
/** | |||
* An integer that describes the kind of this token. This numbering | |||
* system is determined by JavaCCParser, and a table of these numbers is | |||
* stored in the file ...Constants.java. | |||
*/ | |||
public int kind; | |||
/** The line number of the first character of this Token. */ | |||
public int beginLine; | |||
/** The column number of the first character of this Token. */ | |||
public int beginColumn; | |||
/** The line number of the last character of this Token. */ | |||
public int endLine; | |||
/** The column number of the last character of this Token. */ | |||
public int endColumn; | |||
/** | |||
* The string image of the token. | |||
*/ | |||
public String image; | |||
/** | |||
* A reference to the next regular (non-special) token from the input | |||
* stream. If this is the last token from the input stream, or if the | |||
* token manager has not read tokens beyond this one, this field is | |||
* set to null. This is true only if this token is also a regular | |||
* token. Otherwise, see below for a description of the contents of | |||
* this field. | |||
*/ | |||
public Token next; | |||
/** | |||
* This field is used to access special tokens that occur prior to this | |||
* token, but after the immediately preceding regular (non-special) token. | |||
* If there are no such special tokens, this field is set to null. | |||
* When there are more than one such special token, this field refers | |||
* to the last of these special tokens, which in turn refers to the next | |||
* previous special token through its specialToken field, and so on | |||
* until the first special token (whose specialToken field is null). | |||
* The next fields of special tokens refer to other special tokens that | |||
* immediately follow it (without an intervening regular token). If there | |||
* is no such token, this field is null. | |||
*/ | |||
public Token specialToken; | |||
/** | |||
* An optional attribute value of the Token. | |||
* Tokens which are not used as syntactic sugar will often contain | |||
* meaningful values that will be used later on by the compiler or | |||
* interpreter. This attribute value is often different from the image. | |||
* Any subclass of Token that actually wants to return a non-null value can | |||
* override this method as appropriate. | |||
*/ | |||
public Object getValue() { | |||
return null; | |||
} | |||
/** | |||
* No-argument constructor | |||
*/ | |||
public Token() {} | |||
/** | |||
* Constructs a new token for the specified Image. | |||
*/ | |||
public Token(int kind) | |||
{ | |||
this(kind, null); | |||
} | |||
/** | |||
* Constructs a new token for the specified Image and Kind. | |||
*/ | |||
public Token(int kind, String image) | |||
{ | |||
this.kind = kind; | |||
this.image = image; | |||
} | |||
/** | |||
* Returns the image. | |||
*/ | |||
public String toString() | |||
{ | |||
return image; | |||
} | |||
/** | |||
* Returns a new Token object, by default. However, if you want, you | |||
* can create and return subclass objects based on the value of ofKind. | |||
* Simply add the cases to the switch for all those special cases. | |||
* For example, if you have a subclass of Token called IDToken that | |||
* you want to create if ofKind is ID, simply add something like : | |||
* | |||
* case MyParserConstants.ID : return new IDToken(ofKind, image); | |||
* | |||
* to the following switch statement. Then you can cast matchedToken | |||
* variable to the appropriate type and use sit in your lexical actions. | |||
*/ | |||
public static Token newToken(int ofKind, String image) | |||
{ | |||
switch(ofKind) | |||
{ | |||
default : return new Token(ofKind, image); | |||
} | |||
} | |||
public static Token newToken(int ofKind) | |||
{ | |||
return newToken(ofKind, null); | |||
} | |||
} | |||
/* JavaCC - OriginalChecksum=1917659c640ac2c65feaa32a37580421 (do not edit this line) */ |
@@ -1,160 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 4.1 */ | |||
/* JavaCCOptions: */ | |||
package net.sourceforge.pmd.cpd.cppast; | |||
/** Token Manager Error. */ | |||
public class TokenMgrError extends Error | |||
{ | |||
/* | |||
* Ordinals for various reasons why an Error of this type can be thrown. | |||
*/ | |||
/** | |||
* Lexical error occurred. | |||
*/ | |||
static final int LEXICAL_ERROR = 0; | |||
/** | |||
* An attempt was made to create a second instance of a static token manager. | |||
*/ | |||
static final int STATIC_LEXER_ERROR = 1; | |||
/** | |||
* Tried to change to an invalid lexical state. | |||
*/ | |||
static final int INVALID_LEXICAL_STATE = 2; | |||
/** | |||
* Detected (and bailed out of) an infinite loop in the token manager. | |||
*/ | |||
static final int LOOP_DETECTED = 3; | |||
/** | |||
* Indicates the reason why the exception is thrown. It will have | |||
* one of the above 4 values. | |||
*/ | |||
int errorCode; | |||
/** | |||
* Replaces unprintable characters by their escaped (or unicode escaped) | |||
* equivalents in the given string | |||
*/ | |||
protected static final String addEscapes(String str) { | |||
StringBuffer retval = new StringBuffer(); | |||
char ch; | |||
for (int i = 0; i < str.length(); i++) { | |||
switch (str.charAt(i)) | |||
{ | |||
case 0 : | |||
continue; | |||
case '\b': | |||
retval.append("\\b"); | |||
continue; | |||
case '\t': | |||
retval.append("\\t"); | |||
continue; | |||
case '\n': | |||
retval.append("\\n"); | |||
continue; | |||
case '\f': | |||
retval.append("\\f"); | |||
continue; | |||
case '\r': | |||
retval.append("\\r"); | |||
continue; | |||
case '\"': | |||
retval.append("\\\""); | |||
continue; | |||
case '\'': | |||
retval.append("\\\'"); | |||
continue; | |||
case '\\': | |||
retval.append("\\\\"); | |||
continue; | |||
default: | |||
if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { | |||
String s = "0000" + Integer.toString(ch, 16); | |||
retval.append("\\u" + s.substring(s.length() - 4, s.length())); | |||
} else { | |||
retval.append(ch); | |||
} | |||
continue; | |||
} | |||
} | |||
return retval.toString(); | |||
} | |||
/** | |||
* Returns a detailed message for the Error when it is thrown by the | |||
* token manager to indicate a lexical error. | |||
* Parameters : | |||
* EOFSeen : indicates if EOF caused the lexical error | |||
* curLexState : lexical state in which this error occurred | |||
* errorLine : line number when the error occurred | |||
* errorColumn : column number when the error occurred | |||
* errorAfter : prefix that was seen before this error occurred | |||
* curchar : the offending character | |||
* Note: You can customize the lexical error message by modifying this method. | |||
*/ | |||
protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) { | |||
return("Lexical error in file " + CPPParserTokenManager.getFileName() + " at line " + | |||
errorLine + ", column " + | |||
errorColumn + ". Encountered: " + | |||
(EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") + | |||
"after : \"" + addEscapes(errorAfter) + "\""); | |||
} | |||
/** | |||
* You can also modify the body of this method to customize your error messages. | |||
* For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not | |||
* of end-users concern, so you can return something like : | |||
* | |||
* "Internal Error : Please file a bug report .... " | |||
* | |||
* from this method for such cases in the release version of your parser. | |||
*/ | |||
public String getMessage() { | |||
return super.getMessage(); | |||
} | |||
/* | |||
* Constructors of various flavors follow. | |||
*/ | |||
/** No arg constructor. */ | |||
public TokenMgrError() { | |||
} | |||
/** Constructor with message and reason. */ | |||
public TokenMgrError(String message, int reason) { | |||
super(message); | |||
errorCode = reason; | |||
} | |||
/** Full Constructor. */ | |||
public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) { | |||
this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason); | |||
} | |||
} | |||
/* JavaCC - OriginalChecksum=7925b33c412b4bfa3a7147ae3e790276 (do not edit this line) */ |
@@ -30,33 +30,33 @@ import java.util.List; | |||
*/ | |||
public class FileFinder { | |||
private FilenameFilter filter; | |||
private static final String FILE_SEP = System.getProperty("file.separator"); | |||
private FilenameFilter filter; | |||
private static final String FILE_SEP = System.getProperty("file.separator"); | |||
public List<File> findFilesFrom(String dir, FilenameFilter filter, boolean recurse) { | |||
this.filter = filter; | |||
List<File> files = new ArrayList<File>(); | |||
scanDirectory(new File(dir), files, recurse); | |||
return files; | |||
} | |||
public List<File> findFilesFrom(String dir, FilenameFilter filter, boolean recurse) { | |||
this.filter = filter; | |||
List<File> files = new ArrayList<File>(); | |||
scanDirectory(new File(dir), files, recurse); | |||
return files; | |||
} | |||
/** | |||
* Implements a tail recursive file scanner | |||
*/ | |||
private void scanDirectory(File dir, List<File> list, boolean recurse) { | |||
String[] candidates = dir.list(filter); | |||
if (candidates == null) { | |||
return; | |||
} | |||
for (int i = 0; i < candidates.length; i++) { | |||
File tmp = new File(dir + FILE_SEP + candidates[i]); | |||
if (tmp.isDirectory()) { | |||
if (recurse) { | |||
scanDirectory(tmp, list, true); | |||
} | |||
} else { | |||
list.add(new File(dir + FILE_SEP + candidates[i])); | |||
} | |||
/** | |||
* Implements a tail recursive file scanner | |||
*/ | |||
private void scanDirectory(File dir, List<File> list, boolean recurse) { | |||
String[] candidates = dir.list(filter); | |||
if (candidates == null) { | |||
return; | |||
} | |||
for (int i = 0; i < candidates.length; i++) { | |||
File tmp = new File(dir + FILE_SEP + candidates[i]); | |||
if (tmp.isDirectory()) { | |||
if (recurse) { | |||
scanDirectory(tmp, list, true); | |||
} | |||
} else { | |||
list.add(new File(dir + FILE_SEP + candidates[i])); | |||
} | |||
} | |||
} | |||
} |
@@ -1,330 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
/** | |||
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html | |||
*/ | |||
package net.sourceforge.pmd.util; | |||
import java.util.ArrayList; | |||
import java.util.Iterator; | |||
import java.util.List; | |||
public class StringUtil { | |||
public static final String[] EMPTY_STRINGS = new String[0]; | |||
private static final boolean supportsUTF8 = System.getProperty("net.sourceforge.pmd.supportUTF8", "no").equals("yes"); | |||
private static final String[] ENTITIES; | |||
static { | |||
ENTITIES = new String[256 - 126]; | |||
for (int i = 126; i <= 255; i++) { | |||
ENTITIES[i - 126] = "&#" + i + ';'; | |||
} | |||
} | |||
public static String replaceString(String original, char oldChar, String newString) { | |||
String fixedNew = newString == null ? "" : newString; | |||
StringBuffer desc = new StringBuffer(); | |||
int index = original.indexOf(oldChar); | |||
int last = 0; | |||
while (index != -1) { | |||
desc.append(original.substring(last, index)); | |||
desc.append(fixedNew); | |||
last = index + 1; | |||
index = original.indexOf(oldChar, last); | |||
} | |||
desc.append(original.substring(last)); | |||
return desc.toString(); | |||
} | |||
public static String replaceString(String original, String oldString, String newString) { | |||
String fixedNew = newString == null ? "" : newString; | |||
StringBuffer desc = new StringBuffer(); | |||
int index = original.indexOf(oldString); | |||
int last = 0; | |||
while (index != -1) { | |||
desc.append(original.substring(last, index)); | |||
desc.append(fixedNew); | |||
last = index + oldString.length(); | |||
index = original.indexOf(oldString, last); | |||
} | |||
desc.append(original.substring(last)); | |||
return desc.toString(); | |||
} | |||
/** | |||
* Appends to a StringBuffer the String src where non-ASCII and | |||
* XML special chars are escaped. | |||
* | |||
* @param buf The destination XML stream | |||
* @param src The String to append to the stream | |||
*/ | |||
public static void appendXmlEscaped(StringBuffer buf, String src) { | |||
appendXmlEscaped(buf, src, supportsUTF8); | |||
} | |||
public static String htmlEncode(String string) { | |||
String encoded = StringUtil.replaceString(string, '&', "&"); | |||
encoded = StringUtil.replaceString(encoded, '<', "<"); | |||
return StringUtil.replaceString(encoded, '>', ">"); | |||
} | |||
// TODO - unify the method above with the one below | |||
private static void appendXmlEscaped(StringBuffer buf, String src, boolean supportUTF8) { | |||
char c; | |||
for (int i = 0; i < src.length(); i++) { | |||
c = src.charAt(i); | |||
if (c > '~') {// 126 | |||
if (!supportUTF8) { | |||
if (c <= 255) { | |||
buf.append(ENTITIES[c - 126]); | |||
} else { | |||
buf.append("&u").append(Integer.toHexString(c)).append(';'); | |||
} | |||
} else { | |||
buf.append(c); | |||
} | |||
} else if (c == '&') | |||
buf.append("&"); | |||
else if (c == '"') | |||
buf.append("""); | |||
else if (c == '<') | |||
buf.append("<"); | |||
else if (c == '>') | |||
buf.append(">"); | |||
else | |||
buf.append(c); | |||
} | |||
} | |||
/** | |||
* Parses the input source using the delimiter specified. This method is much | |||
* faster than using the StringTokenizer or String.split(char) approach and | |||
* serves as a replacement for String.split() for JDK1.3 that doesn't have it. | |||
* | |||
* FIXME - we're on JDK 1.4 now, can we replace this with String.split? | |||
* | |||
* @param source String | |||
* @param delimiter char | |||
* @return String[] | |||
*/ | |||
public static String[] substringsOf(String source, char delimiter) { | |||
if (source == null || source.length() == 0) { | |||
return EMPTY_STRINGS; | |||
} | |||
int delimiterCount = 0; | |||
int length = source.length(); | |||
char[] chars = source.toCharArray(); | |||
for (int i=0; i<length; i++) { | |||
if (chars[i] == delimiter) delimiterCount++; | |||
} | |||
if (delimiterCount == 0) return new String[] { source }; | |||
String results[] = new String[delimiterCount+1]; | |||
int i = 0; | |||
int offset = 0; | |||
while (offset <= length) { | |||
int pos = source.indexOf(delimiter, offset); | |||
if (pos < 0) pos = length; | |||
results[i++] = pos == offset ? "" : source.substring(offset, pos); | |||
offset = pos + 1; | |||
} | |||
return results; | |||
} | |||
/** | |||
* Much more efficient than StringTokenizer. | |||
* | |||
* @param str String | |||
* @param separator char | |||
* @return String[] | |||
*/ | |||
public static String[] substringsOf(String str, String separator) { | |||
if (str == null || str.length() == 0) { | |||
return EMPTY_STRINGS; | |||
} | |||
int index = str.indexOf(separator); | |||
if (index == -1) { | |||
return new String[]{str}; | |||
} | |||
List<String> list = new ArrayList<String>(); | |||
int currPos = 0; | |||
int len = separator.length(); | |||
while (index != -1) { | |||
list.add(str.substring(currPos, index)); | |||
currPos = index + len; | |||
index = str.indexOf(separator, currPos); | |||
} | |||
list.add(str.substring(currPos)); | |||
return list.toArray(new String[list.size()]); | |||
} | |||
/** | |||
* Copies the elements returned by the iterator onto the string buffer | |||
* each delimited by the separator. | |||
* | |||
* @param sb StringBuffer | |||
* @param iter Iterator | |||
* @param separator String | |||
*/ | |||
public static void asStringOn(StringBuffer sb, Iterator iter, String separator) { | |||
if (!iter.hasNext()) return; | |||
sb.append(iter.next()); | |||
while (iter.hasNext()) { | |||
sb.append(separator); | |||
sb.append(iter.next()); | |||
} | |||
} | |||
/** | |||
* Return the length of the shortest string in the array. | |||
* If any one of them is null then it returns 0. | |||
* | |||
* @param strings String[] | |||
* @return int | |||
*/ | |||
public static int lengthOfShortestIn(String[] strings) { | |||
int minLength = Integer.MAX_VALUE; | |||
for (int i=0; i<strings.length; i++) { | |||
if (strings[i] == null) return 0; | |||
minLength = Math.min(minLength, strings[i].length()); | |||
} | |||
return minLength; | |||
} | |||
/** | |||
* Determine the maximum number of common leading whitespace characters | |||
* the strings share in the same sequence. Useful for determining how | |||
* many leading characters can be removed to shift all the text in the | |||
* strings to the left without misaligning them. | |||
* | |||
* @param strings String[] | |||
* @return int | |||
*/ | |||
public static int maxCommonLeadingWhitespaceForAll(String[] strings) { | |||
int shortest = lengthOfShortestIn(strings); | |||
if (shortest == 0) return 0; | |||
char[] matches = new char[shortest]; | |||
String str; | |||
for (int m=0; m<matches.length; m++) { | |||
matches[m] = strings[0].charAt(m); | |||
if (!Character.isWhitespace(matches[m])) return m; | |||
for (int i=0; i<strings.length; i++) { | |||
str = strings[i]; | |||
if (str.charAt(m) != matches[m]) return m; | |||
} | |||
} | |||
return shortest; | |||
} | |||
/** | |||
* Trims off the leading characters off the strings up to the trimDepth | |||
* specified. Returns the same strings if trimDepth = 0 | |||
* | |||
* @param strings | |||
* @param trimDepth | |||
* @return String[] | |||
*/ | |||
public static String[] trimStartOn(String[] strings, int trimDepth) { | |||
if (trimDepth == 0) return strings; | |||
String[] results = new String[strings.length]; | |||
for (int i=0; i<strings.length; i++) { | |||
results[i] = strings[i].substring(trimDepth); | |||
} | |||
return results; | |||
} | |||
/** | |||
* Left pads a string. | |||
* @param s The String to pad | |||
* @param length The desired minimum length of the resulting padded String | |||
* @return The resulting left padded String | |||
*/ | |||
public static String lpad(String s, int length) { | |||
String res = s; | |||
if (length - s.length() > 0) { | |||
char [] arr = new char[length - s.length()]; | |||
java.util.Arrays.fill(arr, ' '); | |||
res = new StringBuffer(length).append(arr).append(s).toString(); | |||
} | |||
return res; | |||
} | |||
/** | |||
* Are the two String values the same. | |||
* The Strings can be optionally trimmed before checking. | |||
* The Strings can be optionally compared ignoring case. | |||
* The Strings can be have embedded whitespace standardized before comparing. | |||
* Two null values are treated as equal. | |||
* | |||
* @param s1 The first String. | |||
* @param s2 The second String. | |||
* @param trim Indicates if the Strings should be trimmed before comparison. | |||
* @param ignoreCase Indicates if the case of the Strings should ignored during comparison. | |||
* @param standardizeWhitespace Indicates if the embedded whitespace should be standardized before comparison. | |||
* @return <code>true</code> if the Strings are the same, <code>false</code> otherwise. | |||
*/ | |||
public static boolean isSame(String s1, String s2, boolean trim, boolean ignoreCase, boolean standardizeWhitespace) { | |||
if (s1 == s2) { | |||
return true; | |||
} else if (s1 == null || s2 == null) { | |||
return false; | |||
} else { | |||
if (trim) { | |||
s1 = s1.trim(); | |||
s2 = s2.trim(); | |||
} | |||
if (standardizeWhitespace) { | |||
// Replace all whitespace with a standard single space character. | |||
s1 = s1.replaceAll("\\s+", " "); | |||
s2 = s2.replaceAll("\\s+", " "); | |||
} | |||
return ignoreCase ? s1.equalsIgnoreCase(s2) : s1.equals(s2); | |||
} | |||
} | |||
} |
@@ -1,76 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
/** | |||
* A base class for Filters which implements behavior using a List of other | |||
* Filters. | |||
* | |||
* @param <T> | |||
* The underlying type on which the filter applies. | |||
*/ | |||
public abstract class AbstractCompoundFilter<T> implements Filter<T> { | |||
protected List<Filter<T>> filters; | |||
public AbstractCompoundFilter() { | |||
filters = new ArrayList<Filter<T>>(2); | |||
} | |||
public AbstractCompoundFilter(Filter<T>... filters) { | |||
this.filters = new ArrayList<Filter<T>>(filters.length); | |||
for (Filter<T> filter : filters) { | |||
this.filters.add(filter); | |||
} | |||
} | |||
public List<Filter<T>> getFilters() { | |||
return filters; | |||
} | |||
public void setFilters(List<Filter<T>> filters) { | |||
this.filters = filters; | |||
} | |||
public void addFilter(Filter<T> filter) { | |||
filters.add(filter); | |||
} | |||
protected abstract String getOperator(); | |||
public String toString() { | |||
StringBuilder builder = new StringBuilder(); | |||
builder.append("("); | |||
for (int i = 0; i < filters.size(); i++) { | |||
if (i > 0) { | |||
builder.append(" "); | |||
builder.append(getOperator()); | |||
builder.append(" "); | |||
} | |||
builder.append(filters.get(i)); | |||
} | |||
builder.append(")"); | |||
return builder.toString(); | |||
} | |||
} |
@@ -1,57 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
/** | |||
* A base class for Filters which implements behavior using delegation | |||
* to an underlying filter. | |||
* | |||
* @param <T> | |||
* The underlying type on which the filter applies. | |||
*/ | |||
public abstract class AbstractDelegateFilter<T> implements Filter<T> { | |||
protected Filter<T> filter; | |||
public AbstractDelegateFilter() { | |||
} | |||
public AbstractDelegateFilter(Filter<T> filter) { | |||
this.filter = filter; | |||
} | |||
public Filter<T> getFilter() { | |||
return filter; | |||
} | |||
public void setFilter(Filter<T> filter) { | |||
this.filter = filter; | |||
} | |||
// Subclass should override to do something other the simply delegate. | |||
public boolean filter(T obj) { | |||
return filter.filter(obj); | |||
} | |||
// Subclass should override to do something other the simply delegate. | |||
public String toString() { | |||
return filter.toString(); | |||
} | |||
} |
@@ -1,54 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
/** | |||
* A logical AND of a list of Filters. This implementation is short circuiting. | |||
* | |||
* @param <T> | |||
* The underlying type on which the filter applies. | |||
*/ | |||
public class AndFilter<T> extends AbstractCompoundFilter<T> { | |||
public AndFilter() { | |||
super(); | |||
} | |||
public AndFilter(Filter<T>... filters) { | |||
super(filters); | |||
} | |||
public boolean filter(T obj) { | |||
boolean match = true; | |||
for (Filter<T> filter : filters) { | |||
if (!filter.filter(obj)) { | |||
match = false; | |||
break; | |||
} | |||
} | |||
return match; | |||
} | |||
@Override | |||
protected String getOperator() { | |||
return "and"; | |||
} | |||
} |
@@ -1,37 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
import java.io.File; | |||
/** | |||
* Directory filter. | |||
*/ | |||
public class DirectoryFilter implements Filter<File> { | |||
public static final DirectoryFilter INSTANCE = new DirectoryFilter(); | |||
private DirectoryFilter() { | |||
} | |||
public boolean filter(File file) { | |||
return file.isDirectory(); | |||
} | |||
} |
@@ -1,62 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
import java.io.File; | |||
public class FileExtensionFilter implements Filter<File> { | |||
protected final String[] extensions; | |||
protected final boolean ignoreCase; | |||
/** | |||
* Matches any files with the given extensions, ignoring case | |||
*/ | |||
public FileExtensionFilter(String... extensions) { | |||
this(true, extensions); | |||
} | |||
/** | |||
* Matches any files with the given extensions, optionally ignoring case. | |||
*/ | |||
public FileExtensionFilter(boolean ignoreCase, String... extensions) { | |||
this.extensions = extensions; | |||
this.ignoreCase = ignoreCase; | |||
if (ignoreCase) { | |||
for (int i = 0; i < this.extensions.length; i++) { | |||
this.extensions[i] = this.extensions[i].toUpperCase(); | |||
} | |||
} | |||
} | |||
public boolean filter(File file) { | |||
boolean accept = extensions == null; | |||
if (!accept) { | |||
for (String extension : extensions) { | |||
String name = file.getName(); | |||
if (ignoreCase ? name.toUpperCase().endsWith(extension) : name.endsWith(extension)) { | |||
accept = true; | |||
break; | |||
} | |||
} | |||
} | |||
return accept; | |||
} | |||
} |
@@ -1,31 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
/** | |||
* A Filter interface, used for filtering arbitrary objects. | |||
* | |||
* @param <T> | |||
* The underlying type on which the filter applies. | |||
*/ | |||
public interface Filter<T> { | |||
boolean filter(T obj); | |||
} |
@@ -1,220 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
import java.io.File; | |||
import java.io.FilenameFilter; | |||
import java.util.ArrayList; | |||
import java.util.Collection; | |||
import java.util.List; | |||
/** | |||
* Utility class for working with Filters. Contains builder style methods, | |||
* apply methods, as well as mechanisms for adapting Filters and FilenameFilters. | |||
*/ | |||
public class Filters { | |||
/** | |||
* Filter a given Collection. | |||
* @param <T> Type of the Collection. | |||
* @param filter A Filter upon the Type of objects in the Collection. | |||
* @param collection The Collection to filter. | |||
* @return A List containing only those objects for which the Filter returned <code>true</code>. | |||
*/ | |||
public static <T> List<T> filter(Filter<T> filter, Collection<T> collection) { | |||
List<T> list = new ArrayList<T>(); | |||
for (T obj : collection) { | |||
if (filter.filter(obj)) { | |||
list.add(obj); | |||
} | |||
} | |||
return list; | |||
} | |||
/** | |||
* Get a File Filter for files with the given extensions, ignoring case. | |||
* @param extensions The extensions to filter. | |||
* @return A File Filter. | |||
*/ | |||
public static Filter<File> getFileExtensionFilter(String... extensions) { | |||
return new FileExtensionFilter(extensions); | |||
} | |||
/** | |||
* Get a File Filter for directories. | |||
* @return A File Filter. | |||
*/ | |||
public static Filter<File> getDirectoryFilter() { | |||
return DirectoryFilter.INSTANCE; | |||
} | |||
/** | |||
* Get a File Filter for directories or for files with the given extensions, ignoring case. | |||
* @param extensions The extensions to filter. | |||
* @return A File Filter. | |||
*/ | |||
public static Filter<File> getFileExtensionOrDirectoryFilter(String... extensions) { | |||
return new OrFilter<File>(getFileExtensionFilter(extensions), getDirectoryFilter()); | |||
} | |||
/** | |||
* Given a String Filter, expose as a File Filter. The File paths are | |||
* normalized to a standard pattern using <code>/</code> as a path separator | |||
* which can be used cross platform easily in a regular expression based | |||
* String Filter. | |||
* | |||
* @param filter A String Filter. | |||
* @return A File Filter. | |||
*/ | |||
public static Filter<File> toNormalizedFileFilter(final Filter<String> filter) { | |||
return new Filter<File>() { | |||
public boolean filter(File file) { | |||
String path = file.getPath(); | |||
path = path.replace('\\', '/'); | |||
return filter.filter(path); | |||
} | |||
public String toString() { | |||
return filter.toString(); | |||
} | |||
}; | |||
} | |||
/** | |||
* Given a String Filter, expose as a Filter on another type. The | |||
* <code>toString()</code> method is called on the objects of the other | |||
* type and delegated to the String Filter. | |||
* @param <T> The desired type. | |||
* @param filter The existing String Filter. | |||
* @return A Filter on the desired type. | |||
*/ | |||
public static <T> Filter<T> fromStringFilter(final Filter<String> filter) { | |||
return new Filter<T>() { | |||
public boolean filter(T obj) { | |||
return filter.filter(obj.toString()); | |||
} | |||
public String toString() { | |||
return filter.toString(); | |||
} | |||
}; | |||
} | |||
/** | |||
* Given a File Filter, expose as a FilenameFilter. | |||
* @param filter The File Filter. | |||
* @return A FilenameFilter. | |||
*/ | |||
public static FilenameFilter toFilenameFilter(final Filter<File> filter) { | |||
return new FilenameFilter() { | |||
public boolean accept(File dir, String name) { | |||
return filter.filter(new File(dir, name)); | |||
} | |||
public String toString() { | |||
return filter.toString(); | |||
} | |||
}; | |||
} | |||
/** | |||
* Given a FilenameFilter, expose as a File Filter. | |||
* @param filter The FilenameFilter. | |||
* @return A File Filter. | |||
*/ | |||
public static Filter<File> toFileFilter(final FilenameFilter filter) { | |||
return new Filter<File>() { | |||
public boolean filter(File file) { | |||
return filter.accept(file.getParentFile(), file.getName()); | |||
} | |||
public String toString() { | |||
return filter.toString(); | |||
} | |||
}; | |||
} | |||
/** | |||
* Construct a String Filter using set of include and exclude regular | |||
* expressions. If there are no include regular expressions provide, then | |||
* a regular expression is added which matches every String by default. | |||
* A String is included as long as it matches an include regular expression | |||
* and does not match an exclude regular expression. | |||
* <p> | |||
* In other words, exclude patterns override include patterns. | |||
* | |||
* @param includeRegexes The include regular expressions. May be <code>null</code>. | |||
* @param excludeRegexes The exclude regular expressions. May be <code>null</code>. | |||
* @return A String Filter. | |||
*/ | |||
public static Filter<String> buildRegexFilterExcludeOverInclude(List<String> includeRegexes, | |||
List<String> excludeRegexes) { | |||
OrFilter<String> includeFilter = new OrFilter<String>(); | |||
if (includeRegexes == null || includeRegexes.size() == 0) { | |||
includeFilter.addFilter(new RegexStringFilter(".*")); | |||
} else { | |||
for (String includeRegex : includeRegexes) { | |||
includeFilter.addFilter(new RegexStringFilter(includeRegex)); | |||
} | |||
} | |||
OrFilter<String> excludeFilter = new OrFilter<String>(); | |||
if (excludeRegexes != null) { | |||
for (String excludeRegex : excludeRegexes) { | |||
excludeFilter.addFilter(new RegexStringFilter(excludeRegex)); | |||
} | |||
} | |||
return new AndFilter<String>(includeFilter, new NotFilter<String>(excludeFilter)); | |||
} | |||
/** | |||
* Construct a String Filter using set of include and exclude regular | |||
* expressions. If there are no include regular expressions provide, then | |||
* a regular expression is added which matches every String by default. | |||
* A String is included as long as the case that there is an include which | |||
* matches or there is not an exclude which matches. | |||
* <p> | |||
* In other words, include patterns override exclude patterns. | |||
* | |||
* @param includeRegexes The include regular expressions. May be <code>null</code>. | |||
* @param excludeRegexes The exclude regular expressions. May be <code>null</code>. | |||
* @return A String Filter. | |||
*/ | |||
public static Filter<String> buildRegexFilterIncludeOverExclude(List<String> includeRegexes, | |||
List<String> excludeRegexes) { | |||
OrFilter<String> includeFilter = new OrFilter<String>(); | |||
if (includeRegexes != null) { | |||
for (String includeRegex : includeRegexes) { | |||
includeFilter.addFilter(new RegexStringFilter(includeRegex)); | |||
} | |||
} | |||
OrFilter<String> excludeFilter = new OrFilter<String>(); | |||
if (excludeRegexes != null) { | |||
for (String excludeRegex : excludeRegexes) { | |||
excludeFilter.addFilter(new RegexStringFilter(excludeRegex)); | |||
} | |||
} | |||
return new OrFilter<String>(includeFilter, new NotFilter<String>(excludeFilter)); | |||
} | |||
} |
@@ -1,45 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
/** | |||
* A logical NEGATION of a Filter. | |||
* | |||
* @param <T> | |||
* The underlying type on which the filter applies. | |||
*/ | |||
public class NotFilter<T> extends AbstractDelegateFilter<T> { | |||
public NotFilter() { | |||
super(); | |||
} | |||
public NotFilter(Filter<T> filter) { | |||
super(filter); | |||
} | |||
public boolean filter(T obj) { | |||
return !filter.filter(obj); | |||
} | |||
public String toString() { | |||
return "not (" + filter + ")"; | |||
} | |||
} |
@@ -1,54 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
/** | |||
* A logical OR of a list of Filters. This implementation is short circuiting. | |||
* | |||
* @param <T> | |||
* The underlying type on which the filter applies. | |||
*/ | |||
public class OrFilter<T> extends AbstractCompoundFilter<T> { | |||
public OrFilter() { | |||
super(); | |||
} | |||
public OrFilter(Filter<T>... filters) { | |||
super(filters); | |||
} | |||
public boolean filter(T obj) { | |||
boolean match = false; | |||
for (Filter<T> filter : filters) { | |||
if (filter.filter(obj)) { | |||
match = true; | |||
break; | |||
} | |||
} | |||
return match; | |||
} | |||
@Override | |||
protected String getOperator() { | |||
return "or"; | |||
} | |||
} |
@@ -1,60 +0,0 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.util.filter; | |||
import java.util.regex.Pattern; | |||
/** | |||
* A filter to which uses a regular expression to match upon Strings. | |||
*/ | |||
public class RegexStringFilter implements Filter<String> { | |||
protected String regex; | |||
protected Pattern pattern; | |||
public RegexStringFilter() { | |||
} | |||
public RegexStringFilter(String regex) { | |||
this.regex = regex; | |||
} | |||
public String getRegex() { | |||
return regex; | |||
} | |||
public void setRegex(String regex) { | |||
this.regex = regex; | |||
this.pattern = null; | |||
} | |||
public boolean filter(String obj) { | |||
if (pattern == null) { | |||
pattern = Pattern.compile(regex); | |||
} | |||
return pattern.matcher(obj).matches(); | |||
} | |||
public String toString() { | |||
return "matches " + regex; | |||
} | |||
} |
@@ -113,5 +113,4 @@ public class CPD { | |||
source.put(sourceCode.getFileName(), sourceCode); | |||
} | |||
} |
@@ -23,13 +23,13 @@ | |||
*/ | |||
package org.sonar.duplications.cpd; | |||
import net.sourceforge.pmd.cpd.TokenEntry; | |||
import java.util.Comparator; | |||
import java.util.Iterator; | |||
import java.util.Set; | |||
import java.util.TreeSet; | |||
import net.sourceforge.pmd.cpd.TokenEntry; | |||
public class Match implements Comparable<Match> { | |||
public static final String EOL = System.getProperty("line.separator", "\n"); | |||
@@ -196,4 +196,4 @@ public class Match implements Comparable<Match> { | |||
public String getLabel() { | |||
return label; | |||
} | |||
} | |||
} |
@@ -23,17 +23,10 @@ | |||
*/ | |||
package org.sonar.duplications.cpd; | |||
import java.util.ArrayList; | |||
import java.util.Collections; | |||
import java.util.HashMap; | |||
import java.util.HashSet; | |||
import java.util.Iterator; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import net.sourceforge.pmd.cpd.TokenEntry; | |||
import java.util.*; | |||
public class MatchCollector { | |||
private MatchAlgorithm ma; | |||
@@ -51,7 +44,7 @@ public class MatchCollector { | |||
for (int j = i + 1; j < marks.size(); j++) { | |||
TokenEntry mark2 = marks.get(j); | |||
int diff = mark1.getIndex() - mark2.getIndex(); | |||
if ( -diff < ma.getMinimumTileSize()) { | |||
if (-diff < ma.getMinimumTileSize()) { | |||
continue; | |||
} | |||
if (hasPreviousDupe(mark1, mark2)) { | |||
@@ -118,7 +111,7 @@ public class MatchCollector { | |||
TokenEntry cmark2 = a1.get(inner); | |||
matchCode.setFirst(cmark1.getIndex()); | |||
matchCode.setSecond(cmark2.getIndex()); | |||
if ( !matchSet.contains(matchCode)) { | |||
if (!matchSet.contains(matchCode)) { | |||
if (pruned.size() > 2) { | |||
pruned.remove(cmark2); | |||
} | |||
@@ -149,7 +142,7 @@ public class MatchCollector { | |||
if (other.getFirstMark().getIndex() + other.getTokenCount() - mark1.getIndex() > 0) { | |||
boolean ordered = other.getSecondMark().getIndex() - mark2.getIndex() < 0; | |||
if ((ordered && (other.getEndIndex() - mark2.getIndex() > 0)) | |||
|| ( !ordered && (match.getEndIndex() - other.getSecondMark().getIndex()) > 0)) { | |||
|| (!ordered && (match.getEndIndex() - other.getSecondMark().getIndex()) > 0)) { | |||
if (other.getTokenCount() >= match.getTokenCount()) { | |||
add = false; | |||
break; | |||
@@ -170,12 +163,12 @@ public class MatchCollector { | |||
if (mark1.getIndex() == 0) { | |||
return false; | |||
} | |||
return !matchEnded(ma.tokenAt( -1, mark1), ma.tokenAt( -1, mark2)); | |||
return !matchEnded(ma.tokenAt(-1, mark1), ma.tokenAt(-1, mark2)); | |||
} | |||
private int countDuplicateTokens(TokenEntry mark1, TokenEntry mark2) { | |||
int index = 0; | |||
while ( !matchEnded(ma.tokenAt(index, mark1), ma.tokenAt(index, mark2))) { | |||
while (!matchEnded(ma.tokenAt(index, mark1), ma.tokenAt(index, mark2))) { | |||
index++; | |||
} | |||
return index; | |||
@@ -184,4 +177,4 @@ public class MatchCollector { | |||
private boolean matchEnded(TokenEntry token1, TokenEntry token2) { | |||
return token1.getIdentifier() != token2.getIdentifier() || token1 == TokenEntry.EOF || token2 == TokenEntry.EOF; | |||
} | |||
} | |||
} |
@@ -0,0 +1,62 @@ | |||
/* | |||
* Sonar, open source software quality management tool. | |||
* Copyright (C) 2008-2011 SonarSource | |||
* mailto:contact AT sonarsource DOT com | |||
* | |||
* Sonar is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* Sonar is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public | |||
* License along with Sonar; if not, write to the Free Software | |||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 | |||
*/ | |||
package net.sourceforge.pmd.cpd; | |||
import static org.hamcrest.Matchers.is; | |||
import static org.junit.Assert.*; | |||
import java.io.File; | |||
import java.io.FilenameFilter; | |||
import java.io.IOException; | |||
import org.junit.Test; | |||
/** | |||
* We use modified version of {@link AbstractLanguage} in comparison with PMD - it doesn't use package "net.sourceforge.pmd.util.filter", | |||
* so goal of this test is to verify that behavior wasn't changed: | |||
* filter should always accept directories and files with a specified set of extensions (comparison is case insensitive). | |||
*/ | |||
public class AbstractLanguageTest { | |||
@Test | |||
public void shouldCreateCorrectFilenameFilterForExtensions() throws IOException { | |||
AbstractLanguage language = new AbstractLanguage(null, "java") { | |||
}; | |||
FilenameFilter filter = language.getFileFilter(); | |||
assertThat(filter.accept(new File("test-resources"), "org"), is(true)); | |||
assertThat(filter.accept(new File("test-resources/org/sonar/duplications/cpd/CPDTest"), "CPDFile1.java"), is(true)); | |||
assertThat(filter.accept(new File("test-resources/org/sonar/duplications/cpd/CPDTest"), "CPDFile1.cpp"), is(false)); | |||
language = new AbstractLanguage(null, "Java") { | |||
}; | |||
assertThat(filter.accept(new File("test-resources/org/sonar/duplications/cpd/CPDTest"), "CPDFile1.java"), is(true)); | |||
language = new AbstractLanguage(null, new String[] {}) { | |||
}; | |||
assertThat(filter.accept(new File("test-resources/org/sonar/duplications/cpd/CPDTest"), "CPDFile1.java"), is(true)); | |||
} | |||
@Test(expected = NullPointerException.class) | |||
public void shouldThrowException() { | |||
new AbstractLanguage(null, null) { | |||
}; | |||
} | |||
} |