aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java
diff options
context:
space:
mode:
authorAurimas Liutikas <aurimas@google.com>2017-11-28 20:29:59 -0800
committerAurimas Liutikas <aurimas@google.com>2017-11-28 20:29:59 -0800
commitb4aec831a096b89efed151c9b2c5754d9491e6ea (patch)
treef8c04870544dda72e99139fcc713a66b04514dcc /src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java
parentb8aa773e4886dabd0ffc5026420edd061069f044 (diff)
parentdbbd00f58fd36c7c23e24e6652076b735b8e7c0c (diff)
downloadcheckstyle-b4aec831a096b89efed151c9b2c5754d9491e6ea.tar.gz
Merge Checkstyle 8.5 into aosp/master
Test: None
Diffstat (limited to 'src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java')
-rw-r--r--src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java155
1 files changed, 127 insertions, 28 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java
index 3a2e05c47..ee3c66ed8 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionsLoader.java
@@ -23,8 +23,10 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
+import java.util.Set;
import java.util.regex.PatternSyntaxException;
import javax.xml.parsers.ParserConfigurationException;
@@ -33,6 +35,7 @@ import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
import com.puppycrawl.tools.checkstyle.api.AbstractLoader;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.FilterSet;
@@ -56,8 +59,26 @@ public final class SuppressionsLoader
/** The resource for the configuration dtd. */
private static final String DTD_RESOURCE_NAME_1_1 =
"com/puppycrawl/tools/checkstyle/suppressions_1_1.dtd";
+ /** The public ID for the configuration dtd. */
+ private static final String DTD_PUBLIC_ID_1_1_XPATH =
+ "-//Puppy Crawl//DTD Suppressions Xpath Experimental 1.1//EN";
+ /** The resource for the configuration dtd. */
+ private static final String DTD_RESOURCE_NAME_1_1_XPATH =
+ "com/puppycrawl/tools/checkstyle/suppressions_1_1_xpath_experimental.dtd";
/** File search error message. **/
private static final String UNABLE_TO_FIND_ERROR_MESSAGE = "Unable to find: ";
+ /** String literal for attribute name. **/
+ private static final String ATTRIBUTE_NAME_FILES = "files";
+ /** String literal for attribute name. **/
+ private static final String ATTRIBUTE_NAME_CHECKS = "checks";
+ /** String literal for attribute name. **/
+ private static final String ATTRIBUTE_NAME_ID = "id";
+ /** String literal for attribute name. **/
+ private static final String ATTRIBUTE_NAME_QUERY = "query";
+ /** String literal for attribute name. **/
+ private static final String ATTRIBUTE_NAME_LINES = "lines";
+ /** String literal for attribute name. **/
+ private static final String ATTRIBUTE_NAME_COLUMNS = "columns";
/**
* The filter chain to return in getAFilterChain(),
@@ -66,6 +87,11 @@ public final class SuppressionsLoader
private final FilterSet filterChain = new FilterSet();
/**
+ * The set of the {@code TreeWalkerFilter} filters. Being filled during parsing.
+ */
+ private final Set<TreeWalkerFilter> treeWalkerFilters = new HashSet<>();
+
+ /**
* Creates a new {@code SuppressionsLoader} instance.
* @throws ParserConfigurationException if an error occurs
* @throws SAXException if an error occurs
@@ -83,35 +109,68 @@ public final class SuppressionsLoader
throws SAXException {
if ("suppress".equals(qName)) {
//add SuppressElement filter to the filter chain
- final String checks = attributes.getValue("checks");
- final String modId = attributes.getValue("id");
- if (checks == null && modId == null) {
- // -@cs[IllegalInstantiation] SAXException is in the overridden method signature
- throw new SAXException("missing checks and id attribute");
- }
- final SuppressElement suppress;
- try {
- final String files = attributes.getValue("files");
- suppress = new SuppressElement(files);
- suppress.setModuleId(modId);
- if (checks != null) {
- suppress.setChecks(checks);
- }
- }
- catch (final PatternSyntaxException ex) {
- // -@cs[IllegalInstantiation] SAXException is in the overridden method signature
- throw new SAXException("invalid files or checks format", ex);
- }
- final String lines = attributes.getValue("lines");
- if (lines != null) {
- suppress.setLines(lines);
- }
- final String columns = attributes.getValue("columns");
- if (columns != null) {
- suppress.setColumns(columns);
- }
+ final SuppressElement suppress = getSuppressElement(attributes);
filterChain.addFilter(suppress);
}
+ else if ("suppress-xpath".equals(qName)) {
+ final XpathFilter filter = getXpathFilter(attributes);
+ treeWalkerFilters.add(filter);
+ }
+ }
+
+ /**
+ * Returns the suppress element, initialized from given attributes.
+ * @param attributes the attributes of xml-tag "<suppress></suppress>", specified inside
+ * suppression file.
+ * @return the suppress element
+ * @throws SAXException if an error occurs.
+ */
+ private static SuppressElement getSuppressElement(Attributes attributes) throws SAXException {
+ final String checks = attributes.getValue(ATTRIBUTE_NAME_CHECKS);
+ final String modId = attributes.getValue(ATTRIBUTE_NAME_ID);
+ if (checks == null && modId == null) {
+ // -@cs[IllegalInstantiation] SAXException is in the overridden method signature
+ throw new SAXException("missing checks and id attribute");
+ }
+ final SuppressElement suppress;
+ try {
+ final String files = attributes.getValue(ATTRIBUTE_NAME_FILES);
+ final String lines = attributes.getValue(ATTRIBUTE_NAME_LINES);
+ final String columns = attributes.getValue(ATTRIBUTE_NAME_COLUMNS);
+ suppress = new SuppressElement(files, checks, modId, lines, columns);
+ }
+ catch (final PatternSyntaxException ex) {
+ // -@cs[IllegalInstantiation] SAXException is in the overridden method signature
+ throw new SAXException("invalid files or checks format", ex);
+ }
+ return suppress;
+ }
+
+ /**
+ * Returns the xpath filter, initialized from given attributes.
+ * @param attributes the attributes of xml-tag "<suppress-xpath></suppress-xpath>",
+ * specified inside suppression file.
+ * @return the xpath filter
+ * @throws SAXException if an error occurs.
+ */
+ private static XpathFilter getXpathFilter(Attributes attributes) throws SAXException {
+ final String checks = attributes.getValue(ATTRIBUTE_NAME_CHECKS);
+ final String modId = attributes.getValue(ATTRIBUTE_NAME_ID);
+ if (checks == null && modId == null) {
+ // -@cs[IllegalInstantiation] SAXException is in the overridden method signature
+ throw new SAXException("missing checks and id attribute for suppress-xpath");
+ }
+ final XpathFilter filter;
+ try {
+ final String files = attributes.getValue(ATTRIBUTE_NAME_FILES);
+ final String xpathQuery = attributes.getValue(ATTRIBUTE_NAME_QUERY);
+ filter = new XpathFilter(files, checks, modId, xpathQuery);
+ }
+ catch (final PatternSyntaxException ex) {
+ // -@cs[IllegalInstantiation] SAXException is in the overridden method signature
+ throw new SAXException("invalid files or checks format for suppress-xpath", ex);
+ }
+ return filter;
}
/**
@@ -138,11 +197,50 @@ public final class SuppressionsLoader
private static FilterSet loadSuppressions(
InputSource source, String sourceName)
throws CheckstyleException {
+ return getSuppressionLoader(source, sourceName).filterChain;
+ }
+
+ /**
+ * Returns the suppression {@code TreeWalker} filters in a specified file.
+ * @param filename name of the suppressions file.
+ * @return the set of xpath suppression elements specified in the file.
+ * @throws CheckstyleException if an error occurs.
+ */
+ public static Set<TreeWalkerFilter> loadXpathSuppressions(String filename)
+ throws CheckstyleException {
+ // figure out if this is a File or a URL
+ final URI uri = CommonUtils.getUriByFilename(filename);
+ final InputSource source = new InputSource(uri.toString());
+ return loadXpathSuppressions(source, filename);
+ }
+
+ /**
+ * Returns the suppression {@code TreeWalker} filters in a specified source.
+ * @param source the source for the suppressions.
+ * @param sourceName the name of the source.
+ * @return the set of xpath suppression elements specified in source.
+ * @throws CheckstyleException if an error occurs.
+ */
+ private static Set<TreeWalkerFilter> loadXpathSuppressions(
+ InputSource source, String sourceName)
+ throws CheckstyleException {
+ return getSuppressionLoader(source, sourceName).treeWalkerFilters;
+ }
+
+ /**
+ * Parses specified source and returns the suppression loader.
+ * @param source the source for the suppressions.
+ * @param sourceName the name of the source.
+ * @return the suppression loader
+ * @throws CheckstyleException if an error occurs.
+ */
+ private static SuppressionsLoader getSuppressionLoader(InputSource source, String sourceName)
+ throws CheckstyleException {
try {
final SuppressionsLoader suppressionsLoader =
new SuppressionsLoader();
suppressionsLoader.parseInputSource(source);
- return suppressionsLoader.filterChain;
+ return suppressionsLoader;
}
catch (final FileNotFoundException ex) {
throw new CheckstyleException(UNABLE_TO_FIND_ERROR_MESSAGE + sourceName, ex);
@@ -170,6 +268,7 @@ public final class SuppressionsLoader
final Map<String, String> map = new HashMap<>();
map.put(DTD_PUBLIC_ID_1_0, DTD_RESOURCE_NAME_1_0);
map.put(DTD_PUBLIC_ID_1_1, DTD_RESOURCE_NAME_1_1);
+ map.put(DTD_PUBLIC_ID_1_1_XPATH, DTD_RESOURCE_NAME_1_1_XPATH);
return map;
}
}