001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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     * Sets tab width.
049     *
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 violation of the
058     * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified
059     * {@code AuditEvent} object.
060     *
061     * @param event the {@code AuditEvent} object.
062     * @return returns corresponding xpath query
063     */
064    public static String findCorrespondingXpathQuery(AuditEvent event) {
065        return MESSAGE_QUERY_MAP.get(event.getViolation());
066    }
067
068    @Override
069    protected void finishLocalSetup() {
070        MESSAGE_QUERY_MAP.clear();
071    }
072
073    @Override
074    public boolean accept(TreeWalkerAuditEvent event) {
075        if (event.getTokenType() != 0) {
076            final XpathQueryGenerator xpathQueryGenerator =
077                    new XpathQueryGenerator(event, tabWidth);
078            final List<String> xpathQueries = xpathQueryGenerator.generate();
079            if (!xpathQueries.isEmpty()) {
080                final String query = String.join(DELIMITER, xpathQueries);
081                MESSAGE_QUERY_MAP.put(event.getViolation(), query);
082            }
083        }
084        return true;
085    }
086}