001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle; 021 022import java.util.HashMap; 023import java.util.List; 024import java.util.Map; 025 026import com.puppycrawl.tools.checkstyle.api.AuditEvent; 027import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 028import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; 029import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator; 030 031/** 032 * Catches {@code TreeWalkerAuditEvent} and generates corresponding xpath query. 033 * Stores localized messages and xpath queries map inside static variable 034 * for {@code XpathFileGeneratorAuditListener}. 035 * See issue #102 https://github.com/checkstyle/checkstyle/issues/102 036 */ 037public class XpathFileGeneratorAstFilter extends AutomaticBean implements TreeWalkerFilter { 038 039 /** The delimiter between xpath queries. */ 040 private static final String DELIMITER = " | \n"; 041 042 /** Map from {@code LocalizedMessage} objects to xpath queries. */ 043 private static final Map<LocalizedMessage, String> MESSAGE_QUERY_MAP = new HashMap<>(); 044 045 /** The distance between tab stop position. */ 046 private int tabWidth; 047 048 /** 049 * Sets tab width. 050 * 051 * @param tabWidth the distance between tab stops 052 */ 053 public void setTabWidth(int tabWidth) { 054 this.tabWidth = tabWidth; 055 } 056 057 /** 058 * Returns xpath query corresponding to localized message of the 059 * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified 060 * {@code AuditEvent} object. 061 * 062 * @param event the {@code AuditEvent} object. 063 * @return returns corresponding xpath query 064 */ 065 public static String findCorrespondingXpathQuery(AuditEvent event) { 066 return MESSAGE_QUERY_MAP.get(event.getLocalizedMessage()); 067 } 068 069 @Override 070 protected void finishLocalSetup() { 071 MESSAGE_QUERY_MAP.clear(); 072 } 073 074 @Override 075 public boolean accept(TreeWalkerAuditEvent event) { 076 if (event.getTokenType() != 0) { 077 final XpathQueryGenerator xpathQueryGenerator = 078 new XpathQueryGenerator(event, tabWidth); 079 final List<String> xpathQueries = xpathQueryGenerator.generate(); 080 if (!xpathQueries.isEmpty()) { 081 final String query = String.join(DELIMITER, xpathQueries); 082 MESSAGE_QUERY_MAP.put(event.getLocalizedMessage(), query); 083 } 084 } 085 return true; 086 } 087}