com.qulice.checkstyle.PuzzleFormatCheck Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2014, Qulice.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the Qulice.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.qulice.checkstyle;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Check format of PDD puzzles.
*
* This is how you should format them:
*
*
* /**
* * This is my new method.
* * @todo #123:1hr! I will implement it later, when more information
* * come to light and I have some extra documentation
* */
* public void func() {
* // ...
* }
*
*
* Full syntax is explained
* here.
*
* @author Krzysztof Krason ([email protected])
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @see Puzzle Driven Development
* @checkstyle PuzzleFormat (200 lines)
*/
public final class PuzzleFormatCheck extends AbstractFileSetCheck {
/**
* Pattern first line of todo tag.
* @checkstyle LineLength (3 lines)
*/
private static final Pattern FIRST = Pattern.compile(
"^\\s+(?:\\*|//) @todo #[\\w\\d:\\-]+[!?]?(:[0-9]+(\\.[0-9]){0,2}hrs?)? [A-Z][^\n]+$"
);
/**
* Pattern for the rest of todo lines.
*/
private static final Pattern FOLLOWING =
Pattern.compile("^\\s+(?:\\*|//) [^ ].+$");
/**
* Pattern marking the end of todo text.
*/
private static final Pattern OTHER =
Pattern.compile("^(?:\\s+\\*(/?| *@.*)|[^/*]*)$");
@Override
public void processFiltered(final File file, final List lines) {
boolean failure = false;
for (int pos = 0; pos < lines.size(); pos += 1) {
final String line = lines.get(pos);
// @checkstyle PuzzleFormat (1 line)
if (line.contains("@todo")) {
if (!PuzzleFormatCheck.FIRST.matcher(line).matches()) {
// @checkstyle PuzzleFormat (1 line)
this.log(pos + 1, "@todo tag has wrong format");
failure = true;
}
final List defects = this.indentDefects(lines, pos);
if (!defects.isEmpty()) {
for (final int defect : defects) {
this.log(
defect + 1,
"One space indentation expected in TODO puzzle"
);
failure = true;
}
}
}
}
if (failure) {
this.fireErrors(file.getPath());
}
}
/**
* Check the rest of todo tag lines, and find defective lines.
* @param lines All lines in a file.
* @param start Line number of todo tag start.
* @return List of line numbers that have wrong format.
*/
private List indentDefects(final List lines,
final int start) {
final List defects = new ArrayList(lines.size());
for (int pos = start + 1; pos < lines.size(); pos += 1) {
final String line = lines.get(pos);
if (PuzzleFormatCheck.OTHER.matcher(line).matches()) {
break;
}
if (!PuzzleFormatCheck.FOLLOWING.matcher(line).matches()) {
defects.add(pos);
}
}
return defects;
}
}