Prefixes, header, #512
This commit is contained in:
parent
16dac636b2
commit
29027109bf
|
|
@ -50,13 +50,13 @@ import com.puppycrawl.tools.checkstyle.api.Utils;
|
|||
public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
||||
{
|
||||
/** The file that contains the header to check against. */
|
||||
private String mFilename;
|
||||
private String filename;
|
||||
|
||||
/** Name of a charset to use for loading the header from a file. */
|
||||
private String mCharset = System.getProperty("file.encoding", "UTF-8");
|
||||
private String charset = System.getProperty("file.encoding", "UTF-8");
|
||||
|
||||
/** the lines of the header file. */
|
||||
private final List<String> mHeaderLines = Lists.newArrayList();
|
||||
private final List<String> readerLines = Lists.newArrayList();
|
||||
|
||||
|
||||
/**
|
||||
|
|
@ -65,35 +65,35 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
*/
|
||||
protected ImmutableList<String> getHeaderLines()
|
||||
{
|
||||
return ImmutableList.copyOf(mHeaderLines);
|
||||
return ImmutableList.copyOf(readerLines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the charset to use for loading the header from a file.
|
||||
* @param aCharset the charset to use for loading the header from a file
|
||||
* @throws UnsupportedEncodingException if aCharset is unsupported
|
||||
* @param charset the charset to use for loading the header from a file
|
||||
* @throws UnsupportedEncodingException if charset is unsupported
|
||||
*/
|
||||
public void setCharset(String aCharset) throws UnsupportedEncodingException
|
||||
public void setCharset(String charset) throws UnsupportedEncodingException
|
||||
{
|
||||
if (!Charset.isSupported(aCharset)) {
|
||||
final String message = "unsupported charset: '" + aCharset + "'";
|
||||
if (!Charset.isSupported(charset)) {
|
||||
final String message = "unsupported charset: '" + charset + "'";
|
||||
throw new UnsupportedEncodingException(message);
|
||||
}
|
||||
mCharset = aCharset;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the header file to check against.
|
||||
* @param aFileName the file that contains the header to check against.
|
||||
* @param fileName the file that contains the header to check against.
|
||||
*/
|
||||
public void setHeaderFile(String aFileName)
|
||||
public void setHeaderFile(String fileName)
|
||||
{
|
||||
// Handle empty param
|
||||
if ((aFileName == null) || (aFileName.trim().length() == 0)) {
|
||||
if ((fileName == null) || (fileName.trim().length() == 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
mFilename = aFileName;
|
||||
filename = fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -107,12 +107,12 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
try {
|
||||
final URI uri = resolveHeaderFile();
|
||||
headerReader = new InputStreamReader(new BufferedInputStream(
|
||||
uri.toURL().openStream()), mCharset);
|
||||
uri.toURL().openStream()), charset);
|
||||
loadHeader(headerReader);
|
||||
}
|
||||
catch (final IOException ex) {
|
||||
throw new CheckstyleException(
|
||||
"unable to load header file " + mFilename, ex);
|
||||
"unable to load header file " + filename, ex);
|
||||
}
|
||||
finally {
|
||||
Utils.closeQuietly(headerReader);
|
||||
|
|
@ -129,7 +129,7 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
// figure out if this is a File or a URL
|
||||
URI uri;
|
||||
try {
|
||||
final URL url = new URL(mFilename);
|
||||
final URL url = new URL(filename);
|
||||
uri = url.toURI();
|
||||
}
|
||||
catch (final MalformedURLException ex) {
|
||||
|
|
@ -140,7 +140,7 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
uri = null;
|
||||
}
|
||||
if (uri == null) {
|
||||
final File file = new File(mFilename);
|
||||
final File file = new File(filename);
|
||||
if (file.exists()) {
|
||||
uri = file.toURI();
|
||||
}
|
||||
|
|
@ -148,14 +148,14 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
// check to see if the file is in the classpath
|
||||
try {
|
||||
final URL configUrl = AbstractHeaderCheck.class
|
||||
.getResource(mFilename);
|
||||
.getResource(filename);
|
||||
if (configUrl == null) {
|
||||
throw new FileNotFoundException(mFilename);
|
||||
throw new FileNotFoundException(filename);
|
||||
}
|
||||
uri = configUrl.toURI();
|
||||
}
|
||||
catch (final URISyntaxException e) {
|
||||
throw new FileNotFoundException(mFilename);
|
||||
throw new FileNotFoundException(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -168,7 +168,7 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
*/
|
||||
private void checkHeaderNotInitialized()
|
||||
{
|
||||
if (!mHeaderLines.isEmpty()) {
|
||||
if (!readerLines.isEmpty()) {
|
||||
throw new ConversionException(
|
||||
"header has already been set - "
|
||||
+ "set either header or headerFile, not both");
|
||||
|
|
@ -178,18 +178,18 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
/**
|
||||
* Set the header to check against. Individual lines in the header
|
||||
* must be separated by '\n' characters.
|
||||
* @param aHeader header content to check against.
|
||||
* @param header header content to check against.
|
||||
* @throws ConversionException if the header cannot be interpreted
|
||||
*/
|
||||
public void setHeader(String aHeader)
|
||||
public void setHeader(String header)
|
||||
{
|
||||
if ((aHeader == null) || (aHeader.trim().length() == 0)) {
|
||||
if ((header == null) || (header.trim().length() == 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
checkHeaderNotInitialized();
|
||||
|
||||
final String headerExpandedNewLines = aHeader.replaceAll("\\\\n", "\n");
|
||||
final String headerExpandedNewLines = header.replaceAll("\\\\n", "\n");
|
||||
|
||||
final Reader headerReader = new StringReader(headerExpandedNewLines);
|
||||
try {
|
||||
|
|
@ -204,20 +204,20 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
}
|
||||
|
||||
/**
|
||||
* Load header to check against from a Reader into mHeaderLines.
|
||||
* @param aHeaderReader delivers the header to check against
|
||||
* Load header to check against from a Reader into readerLines.
|
||||
* @param headerReader delivers the header to check against
|
||||
* @throws IOException if
|
||||
*/
|
||||
private void loadHeader(final Reader aHeaderReader) throws IOException
|
||||
private void loadHeader(final Reader headerReader) throws IOException
|
||||
{
|
||||
final LineNumberReader lnr = new LineNumberReader(aHeaderReader);
|
||||
mHeaderLines.clear();
|
||||
final LineNumberReader lnr = new LineNumberReader(headerReader);
|
||||
readerLines.clear();
|
||||
while (true) {
|
||||
final String l = lnr.readLine();
|
||||
if (l == null) {
|
||||
break;
|
||||
}
|
||||
mHeaderLines.add(l);
|
||||
readerLines.add(l);
|
||||
}
|
||||
postprocessHeaderLines();
|
||||
}
|
||||
|
|
@ -233,10 +233,10 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
|
|||
@Override
|
||||
protected final void finishLocalSetup() throws CheckstyleException
|
||||
{
|
||||
if (mFilename != null) {
|
||||
if (filename != null) {
|
||||
loadHeaderFile();
|
||||
}
|
||||
if (mHeaderLines.isEmpty()) {
|
||||
if (readerLines.isEmpty()) {
|
||||
throw new CheckstyleException(
|
||||
"property 'headerFile' is missing or invalid in module "
|
||||
+ getConfiguration().getName());
|
||||
|
|
|
|||
|
|
@ -33,55 +33,55 @@ public class HeaderCheck extends AbstractHeaderCheck
|
|||
private static final int[] EMPTY_INT_ARRAY = new int[0];
|
||||
|
||||
/** the header lines to ignore in the check, sorted. */
|
||||
private int[] mIgnoreLines = EMPTY_INT_ARRAY;
|
||||
private int[] ignoreLines = EMPTY_INT_ARRAY;
|
||||
|
||||
/**
|
||||
* @param aLineNo a line number
|
||||
* @return if <code>aLineNo</code> is one of the ignored header lines.
|
||||
* @param lineNo a line number
|
||||
* @return if <code>lineNo</code> is one of the ignored header lines.
|
||||
*/
|
||||
private boolean isIgnoreLine(int aLineNo)
|
||||
private boolean isIgnoreLine(int lineNo)
|
||||
{
|
||||
return (Arrays.binarySearch(mIgnoreLines, aLineNo) >= 0);
|
||||
return (Arrays.binarySearch(ignoreLines, lineNo) >= 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a code line matches the required header line.
|
||||
* @param aLineNumber the line number to check against the header
|
||||
* @param aLine the line contents
|
||||
* @param lineNumber the line number to check against the header
|
||||
* @param line the line contents
|
||||
* @return true if and only if the line matches the required header line
|
||||
*/
|
||||
protected boolean isMatch(int aLineNumber, String aLine)
|
||||
protected boolean isMatch(int lineNumber, String line)
|
||||
{
|
||||
// skip lines we are meant to ignore
|
||||
return isIgnoreLine(aLineNumber + 1)
|
||||
|| getHeaderLines().get(aLineNumber).equals(aLine);
|
||||
return isIgnoreLine(lineNumber + 1)
|
||||
|| getHeaderLines().get(lineNumber).equals(line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the lines numbers to ignore in the header check.
|
||||
* @param aList comma separated list of line numbers to ignore in header.
|
||||
* @param list comma separated list of line numbers to ignore in header.
|
||||
*/
|
||||
public void setIgnoreLines(int[] aList)
|
||||
public void setIgnoreLines(int[] list)
|
||||
{
|
||||
if ((aList == null) || (aList.length == 0)) {
|
||||
mIgnoreLines = EMPTY_INT_ARRAY;
|
||||
if ((list == null) || (list.length == 0)) {
|
||||
ignoreLines = EMPTY_INT_ARRAY;
|
||||
return;
|
||||
}
|
||||
|
||||
mIgnoreLines = new int[aList.length];
|
||||
System.arraycopy(aList, 0, mIgnoreLines, 0, aList.length);
|
||||
Arrays.sort(mIgnoreLines);
|
||||
ignoreLines = new int[list.length];
|
||||
System.arraycopy(list, 0, ignoreLines, 0, list.length);
|
||||
Arrays.sort(ignoreLines);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processFiltered(File aFile, List<String> aLines)
|
||||
protected void processFiltered(File file, List<String> lines)
|
||||
{
|
||||
if (getHeaderLines().size() > aLines.size()) {
|
||||
if (getHeaderLines().size() > lines.size()) {
|
||||
log(1, "header.missing");
|
||||
}
|
||||
else {
|
||||
for (int i = 0; i < getHeaderLines().size(); i++) {
|
||||
if (!isMatch(i, aLines.get(i))) {
|
||||
if (!isMatch(i, lines.get(i))) {
|
||||
log(i + 1, "header.mismatch", getHeaderLines().get(i));
|
||||
break; // stop checking
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,41 +45,41 @@ public class RegexpHeaderCheck extends AbstractHeaderCheck
|
|||
private static final int[] EMPTY_INT_ARRAY = new int[0];
|
||||
|
||||
/** the compiled regular expressions */
|
||||
private final List<Pattern> mHeaderRegexps = Lists.newArrayList();
|
||||
private final List<Pattern> headerRegexps = Lists.newArrayList();
|
||||
|
||||
/** the header lines to repeat (0 or more) in the check, sorted. */
|
||||
private int[] mMultiLines = EMPTY_INT_ARRAY;
|
||||
private int[] multiLines = EMPTY_INT_ARRAY;
|
||||
|
||||
/**
|
||||
* Set the lines numbers to repeat in the header check.
|
||||
* @param aList comma separated list of line numbers to repeat in header.
|
||||
* @param list comma separated list of line numbers to repeat in header.
|
||||
*/
|
||||
public void setMultiLines(int[] aList)
|
||||
public void setMultiLines(int[] list)
|
||||
{
|
||||
if ((aList == null) || (aList.length == 0)) {
|
||||
mMultiLines = EMPTY_INT_ARRAY;
|
||||
if ((list == null) || (list.length == 0)) {
|
||||
multiLines = EMPTY_INT_ARRAY;
|
||||
return;
|
||||
}
|
||||
|
||||
mMultiLines = new int[aList.length];
|
||||
System.arraycopy(aList, 0, mMultiLines, 0, aList.length);
|
||||
Arrays.sort(mMultiLines);
|
||||
multiLines = new int[list.length];
|
||||
System.arraycopy(list, 0, multiLines, 0, list.length);
|
||||
Arrays.sort(multiLines);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processFiltered(File aFile, List<String> aLines)
|
||||
protected void processFiltered(File file, List<String> lines)
|
||||
{
|
||||
final int headerSize = getHeaderLines().size();
|
||||
final int fileSize = aLines.size();
|
||||
final int fileSize = lines.size();
|
||||
|
||||
if (headerSize - mMultiLines.length > fileSize) {
|
||||
if (headerSize - multiLines.length > fileSize) {
|
||||
log(1, "header.missing");
|
||||
}
|
||||
else {
|
||||
int headerLineNo = 0;
|
||||
int i;
|
||||
for (i = 0; (headerLineNo < headerSize) && (i < fileSize); i++) {
|
||||
final String line = aLines.get(i);
|
||||
final String line = lines.get(i);
|
||||
boolean isMatch = isMatch(line, headerLineNo);
|
||||
while (!isMatch && isMultiLine(headerLineNo)) {
|
||||
headerLineNo++;
|
||||
|
|
@ -110,37 +110,37 @@ public class RegexpHeaderCheck extends AbstractHeaderCheck
|
|||
|
||||
/**
|
||||
* Checks if a code line matches the required header line.
|
||||
* @param aLine the code line
|
||||
* @param aHeaderLineNo the header line number.
|
||||
* @param line the code line
|
||||
* @param headerLineNo the header line number.
|
||||
* @return true if and only if the line matches the required header line.
|
||||
*/
|
||||
private boolean isMatch(String aLine, int aHeaderLineNo)
|
||||
private boolean isMatch(String line, int headerLineNo)
|
||||
{
|
||||
return mHeaderRegexps.get(aHeaderLineNo).matcher(aLine).find();
|
||||
return headerRegexps.get(headerLineNo).matcher(line).find();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param aLineNo a line number
|
||||
* @return if <code>aLineNo</code> is one of the repeat header lines.
|
||||
* @param lineNo a line number
|
||||
* @return if <code>lineNo</code> is one of the repeat header lines.
|
||||
*/
|
||||
private boolean isMultiLine(int aLineNo)
|
||||
private boolean isMultiLine(int lineNo)
|
||||
{
|
||||
return (Arrays.binarySearch(mMultiLines, aLineNo + 1) >= 0);
|
||||
return (Arrays.binarySearch(multiLines, lineNo + 1) >= 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postprocessHeaderLines()
|
||||
{
|
||||
final List<String> headerLines = getHeaderLines();
|
||||
mHeaderRegexps.clear();
|
||||
headerRegexps.clear();
|
||||
for (String line : headerLines) {
|
||||
try {
|
||||
// TODO: Not sure if cache in Utils is still necessary
|
||||
mHeaderRegexps.add(Utils.getPattern(line));
|
||||
headerRegexps.add(Utils.getPattern(line));
|
||||
}
|
||||
catch (final PatternSyntaxException ex) {
|
||||
throw new ConversionException("line "
|
||||
+ (mHeaderRegexps.size() + 1)
|
||||
+ (headerRegexps.size() + 1)
|
||||
+ " in header specification"
|
||||
+ " is not a regular expression");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue