001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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 * @param tabWidth the distance between tab stops 051 */ 052 public void setTabWidth(int tabWidth) { 053 this.tabWidth = tabWidth; 054 } 055 056 /** 057 * Returns xpath query corresponding to localized message of the 058 * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified 059 * {@code AuditEvent} object. 060 * @param event the {@code AuditEvent} object. 061 * @return returns corresponding xpath query 062 */ 063 public static String findCorrespondingXpathQuery(AuditEvent event) { 064 return MESSAGE_QUERY_MAP.get(event.getLocalizedMessage()); 065 } 066 067 @Override 068 protected void finishLocalSetup() { 069 MESSAGE_QUERY_MAP.clear(); 070 } 071 072 @Override 073 public boolean accept(TreeWalkerAuditEvent event) { 074 if (event.getTokenType() != 0) { 075 final XpathQueryGenerator xpathQueryGenerator = 076 new XpathQueryGenerator(event, tabWidth); 077 final List<String> xpathQueries = xpathQueryGenerator.generate(); 078 if (!xpathQueries.isEmpty()) { 079 final String query = String.join(DELIMITER, xpathQueries); 080 MESSAGE_QUERY_MAP.put(event.getLocalizedMessage(), query); 081 } 082 } 083 return true; 084 } 085}