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.HashMap;
023import java.util.List;
024import java.util.Map;
025import java.util.stream.Collectors;
026
027import com.puppycrawl.tools.checkstyle.api.AuditEvent;
028import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
029import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
030import com.puppycrawl.tools.checkstyle.xpath.XpathQueryGenerator;
031
032/**
033 * Catches {@code TreeWalkerAuditEvent} and generates corresponding xpath query.
034 * Stores localized messages and xpath queries map inside static variable
035 * for {@code XpathFileGeneratorAuditListener}.
036 * See issue #102 https://github.com/checkstyle/checkstyle/issues/102
037 */
038public class XpathFileGeneratorAstFilter extends AutomaticBean implements TreeWalkerFilter {
039
040    /** The delimiter between xpath queries. */
041    private static final String DELIMITER = " | \n";
042
043    /** Map from {@code LocalizedMessage} objects to xpath queries. */
044    private static final Map<LocalizedMessage, String> MESSAGE_QUERY_MAP = new HashMap<>();
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 localized message of the
059     * {@code TreeWalkerAuditEvent} object which points to the same AST element as specified
060     * {@code AuditEvent} object.
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.getLocalizedMessage());
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 = xpathQueries.stream().collect(Collectors.joining(DELIMITER));
081                MESSAGE_QUERY_MAP.put(event.getLocalizedMessage(), query);
082            }
083        }
084        return true;
085    }
086}