View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
27  import com.puppycrawl.tools.checkstyle.api.Violation;
28  import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator;
29  
30  /**
31   * Catches {@code TreeWalkerAuditEvent} and generates corresponding xpath query.
32   * Stores violations and xpath queries map inside static variable
33   * for {@code XpathFileGeneratorAuditListener}.
34   * See issue <a href="https://github.com/checkstyle/checkstyle/issues/102">#102</a>
35   */
36  public class XpathFileGeneratorAstFilter extends AbstractAutomaticBean implements TreeWalkerFilter {
37  
38      /** The delimiter between xpath queries. */
39      private static final String DELIMITER = " | \n";
40  
41      /** Map from {@code Violation} objects to xpath queries. */
42      private static final Map<Violation, String> MESSAGE_QUERY_MAP = new HashMap<>();
43  
44      /** The distance between tab stop position. */
45      private int tabWidth;
46  
47      /**
48       * Sets tab width.
49       *
50       * @param tabWidth the distance between tab stops
51       */
52      public void setTabWidth(int tabWidth) {
53          this.tabWidth = tabWidth;
54      }
55  
56      /**
57       * Returns xpath query corresponding to violation of the
58       * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified
59       * {@code AuditEvent} object.
60       *
61       * @param event the {@code AuditEvent} object.
62       * @return returns corresponding xpath query
63       */
64      public static String findCorrespondingXpathQuery(AuditEvent event) {
65          return MESSAGE_QUERY_MAP.get(event.getViolation());
66      }
67  
68      @Override
69      protected void finishLocalSetup() {
70          MESSAGE_QUERY_MAP.clear();
71      }
72  
73      @Override
74      public boolean accept(TreeWalkerAuditEvent event) {
75          if (event.getTokenType() != 0) {
76              final XpathQueryGenerator xpathQueryGenerator =
77                      new XpathQueryGenerator(event, tabWidth);
78              final List<String> xpathQueries = xpathQueryGenerator.generate();
79              if (!xpathQueries.isEmpty()) {
80                  final String query = String.join(DELIMITER, xpathQueries);
81                  MESSAGE_QUERY_MAP.put(event.getViolation(), query);
82              }
83          }
84          return true;
85      }
86  }