1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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.AutomaticBean;
28  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
29  import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator;
30  
31  /**
32   * Catches {@code TreeWalkerAuditEvent} and generates corresponding xpath query.
33   * Stores localized messages and xpath queries map inside static variable
34   * for {@code XpathFileGeneratorAuditListener}.
35   * See issue #102 https://github.com/checkstyle/checkstyle/issues/102
36   */
37  public class XpathFileGeneratorAstFilter extends AutomaticBean implements TreeWalkerFilter {
38  
39      /** The delimiter between xpath queries. */
40      private static final String DELIMITER = " | \n";
41  
42      /** Map from {@code LocalizedMessage} objects to xpath queries. */
43      private static final Map<LocalizedMessage, String> MESSAGE_QUERY_MAP = new HashMap<>();
44  
45      /** The distance between tab stop position. */
46      private int tabWidth;
47  
48      /**
49       * Sets tab width.
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 localized message of the
58       * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified
59       * {@code AuditEvent} object.
60       * @param event the {@code AuditEvent} object.
61       * @return returns corresponding xpath query
62       */
63      public static String findCorrespondingXpathQuery(AuditEvent event) {
64          return MESSAGE_QUERY_MAP.get(event.getLocalizedMessage());
65      }
66  
67      @Override
68      protected void finishLocalSetup() {
69          MESSAGE_QUERY_MAP.clear();
70      }
71  
72      @Override
73      public boolean accept(TreeWalkerAuditEvent event) {
74          if (event.getTokenType() != 0) {
75              final XpathQueryGenerator xpathQueryGenerator =
76                      new XpathQueryGenerator(event, tabWidth);
77              final List<String> xpathQueries = xpathQueryGenerator.generate();
78              if (!xpathQueries.isEmpty()) {
79                  final String query = String.join(DELIMITER, xpathQueries);
80                  MESSAGE_QUERY_MAP.put(event.getLocalizedMessage(), query);
81              }
82          }
83          return true;
84      }
85  }