001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2018 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.ArrayList; 023import java.util.List; 024import java.util.stream.Collectors; 025 026import com.puppycrawl.tools.checkstyle.api.AuditEvent; 027import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 028import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator; 029 030/** 031 * Catches {@code TreeWalkerAuditEvent} and generates corresponding xpath query. 032 * Stores event and query inside static variables for {@code XpathFileGeneratorAuditListener}. 033 * See issue #102 https://github.com/checkstyle/checkstyle/issues/102 034 */ 035public class XpathFileGeneratorAstFilter extends AutomaticBean implements TreeWalkerFilter { 036 037 /** The delimiter between xpath queries. */ 038 private static final String DELIMITER = " | \n"; 039 040 /** List of the {@code TreeWalkerAuditEvent} objects, which supports xpath suppressions. */ 041 private static final List<TreeWalkerAuditEvent> EVENTS = new ArrayList<>(); 042 043 /** List of xpath queries for corresponding {@code TreeWalkerAuditEvent} objects. */ 044 private static final List<String> QUERIES = new ArrayList<>(); 045 046 /** The distance between tab stop position. */ 047 private int tabWidth; 048 049 /** 050 * Sets tab width. 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 {@code TreeWalkerAuditEvent} object which points to 059 * the same AST element as specified {@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 String result = null; 065 for (int i = 0; i < EVENTS.size(); i++) { 066 final TreeWalkerAuditEvent treeWalkerAuditEvent = 067 EVENTS.get(i); 068 if (event.getSourceName().equals(treeWalkerAuditEvent.getSourceName()) 069 && event.getLine() == treeWalkerAuditEvent.getLine() 070 && event.getColumn() == treeWalkerAuditEvent.getColumn() 071 && event.getFileName().endsWith(treeWalkerAuditEvent.getFileName())) { 072 result = QUERIES.get(i); 073 break; 074 } 075 } 076 return result; 077 } 078 079 @Override 080 protected void finishLocalSetup() { 081 cleanup(); 082 } 083 084 /** 085 * Makes {@code EVENTS} and {@code QUERIES} lists empty. 086 */ 087 private static void cleanup() { 088 QUERIES.clear(); 089 EVENTS.clear(); 090 } 091 092 @Override 093 public boolean accept(TreeWalkerAuditEvent event) { 094 if (event.getTokenType() != 0) { 095 final XpathQueryGenerator xpathQueryGenerator = 096 new XpathQueryGenerator(event, tabWidth); 097 final List<String> xpathQueries = xpathQueryGenerator.generate(); 098 if (!xpathQueries.isEmpty()) { 099 final String query = xpathQueries.stream().collect(Collectors.joining(DELIMITER)); 100 EVENTS.add(event); 101 QUERIES.add(query); 102 } 103 } 104 return true; 105 } 106}