001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2026 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.Violation;
028import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator;
029
030/**
031 * Catches {@code TreeWalkerAuditEvent} and generates corresponding xpath query.
032 * Stores violations and xpath queries map inside static variable
033 * for {@code XpathFileGeneratorAuditListener}.
034 * See issue <a href="https://github.com/checkstyle/checkstyle/issues/102">#102</a>
035 */
036public class XpathFileGeneratorAstFilter extends AbstractAutomaticBean implements TreeWalkerFilter {
037
038    /** The delimiter between xpath queries. */
039    private static final String DELIMITER = " | \n";
040
041    /** Map from {@code Violation} objects to xpath queries. */
042    private static final Map<Violation, String> MESSAGE_QUERY_MAP = new HashMap<>();
043
044    /** The distance between tab stop position. */
045    private int tabWidth;
046
047    /**
048     * Creates a new {@code XpathFileGeneratorAstFilter} instance.
049     */
050    public XpathFileGeneratorAstFilter() {
051        // no code by default
052    }
053
054    /**
055     * Sets tab width.
056     *
057     * @param tabWidth the distance between tab stops
058     */
059    public void setTabWidth(int tabWidth) {
060        this.tabWidth = tabWidth;
061    }
062
063    /**
064     * Returns xpath query corresponding to violation of the
065     * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified
066     * {@code AuditEvent} object.
067     *
068     * @param event the {@code AuditEvent} object.
069     * @return returns corresponding xpath query
070     */
071    public static String findCorrespondingXpathQuery(AuditEvent event) {
072        return MESSAGE_QUERY_MAP.get(event.getViolation());
073    }
074
075    @Override
076    protected void finishLocalSetup() {
077        MESSAGE_QUERY_MAP.clear();
078    }
079
080    @Override
081    public boolean accept(TreeWalkerAuditEvent event) {
082        if (event.getTokenType() != 0) {
083            final XpathQueryGenerator xpathQueryGenerator =
084                    new XpathQueryGenerator(event, tabWidth);
085            final List<String> xpathQueries = xpathQueryGenerator.generate();
086            if (!xpathQueries.isEmpty()) {
087                final String query = String.join(DELIMITER, xpathQueries);
088                MESSAGE_QUERY_MAP.put(event.violation(), query);
089            }
090        }
091        return true;
092    }
093
094}