View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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       * Creates a new {@code XpathFileGeneratorAstFilter} instance.
49       */
50      public XpathFileGeneratorAstFilter() {
51          // no code by default
52      }
53  
54      /**
55       * Sets tab width.
56       *
57       * @param tabWidth the distance between tab stops
58       */
59      public void setTabWidth(int tabWidth) {
60          this.tabWidth = tabWidth;
61      }
62  
63      /**
64       * Returns xpath query corresponding to violation of the
65       * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified
66       * {@code AuditEvent} object.
67       *
68       * @param event the {@code AuditEvent} object.
69       * @return returns corresponding xpath query
70       */
71      public static String findCorrespondingXpathQuery(AuditEvent event) {
72          return MESSAGE_QUERY_MAP.get(event.getViolation());
73      }
74  
75      @Override
76      protected void finishLocalSetup() {
77          MESSAGE_QUERY_MAP.clear();
78      }
79  
80      @Override
81      public boolean accept(TreeWalkerAuditEvent event) {
82          if (event.getTokenType() != 0) {
83              final XpathQueryGenerator xpathQueryGenerator =
84                      new XpathQueryGenerator(event, tabWidth);
85              final List<String> xpathQueries = xpathQueryGenerator.generate();
86              if (!xpathQueries.isEmpty()) {
87                  final String query = String.join(DELIMITER, xpathQueries);
88                  MESSAGE_QUERY_MAP.put(event.violation(), query);
89              }
90          }
91          return true;
92      }
93  
94  }