001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2016 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.filters;
021
022import java.io.FileNotFoundException;
023import java.io.IOException;
024import java.net.URI;
025import java.util.Locale;
026import java.util.Map;
027import java.util.regex.PatternSyntaxException;
028
029import javax.xml.parsers.ParserConfigurationException;
030
031import org.xml.sax.Attributes;
032import org.xml.sax.InputSource;
033import org.xml.sax.SAXException;
034
035import com.google.common.collect.Maps;
036import com.puppycrawl.tools.checkstyle.api.AbstractLoader;
037import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
038import com.puppycrawl.tools.checkstyle.api.FilterSet;
039import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
040
041/**
042 * Loads a filter chain of suppressions.
043 * @author Rick Giles
044 */
045public final class SuppressionsLoader
046    extends AbstractLoader {
047    /** The public ID for the configuration dtd. */
048    private static final String DTD_PUBLIC_ID_1_0 =
049        "-//Puppy Crawl//DTD Suppressions 1.0//EN";
050    /** The resource for the configuration dtd. */
051    private static final String DTD_RESOURCE_NAME_1_0 =
052        "com/puppycrawl/tools/checkstyle/suppressions_1_0.dtd";
053    /** The public ID for the configuration dtd. */
054    private static final String DTD_PUBLIC_ID_1_1 =
055        "-//Puppy Crawl//DTD Suppressions 1.1//EN";
056    /** The resource for the configuration dtd. */
057    private static final String DTD_RESOURCE_NAME_1_1 =
058        "com/puppycrawl/tools/checkstyle/suppressions_1_1.dtd";
059    /** File search error message. **/
060    private static final String UNABLE_TO_FIND_ERROR_MESSAGE = "Unable to find: ";
061
062    /**
063     * The filter chain to return in getAFilterChain(),
064     * configured during parsing.
065     */
066    private final FilterSet filterChain = new FilterSet();
067
068    /**
069     * Creates a new {@code SuppressionsLoader} instance.
070     * @throws ParserConfigurationException if an error occurs
071     * @throws SAXException if an error occurs
072     */
073    private SuppressionsLoader()
074            throws ParserConfigurationException, SAXException {
075        super(createIdToResourceNameMap());
076    }
077
078    @Override
079    public void startElement(String namespaceUri,
080                             String localName,
081                             String qName,
082                             Attributes attributes)
083            throws SAXException {
084        if ("suppress".equals(qName)) {
085            //add SuppressElement filter to the filter chain
086            final String checks = attributes.getValue("checks");
087            final String modId = attributes.getValue("id");
088            if (checks == null && modId == null) {
089                throw new SAXException("missing checks and id attribute");
090            }
091            final SuppressElement suppress;
092            try {
093                final String files = attributes.getValue("files");
094                suppress = new SuppressElement(files);
095                if (modId != null) {
096                    suppress.setModuleId(modId);
097                }
098                if (checks != null) {
099                    suppress.setChecks(checks);
100                }
101            }
102            catch (final PatternSyntaxException ex) {
103                throw new SAXException("invalid files or checks format", ex);
104            }
105            final String lines = attributes.getValue("lines");
106            if (lines != null) {
107                suppress.setLines(lines);
108            }
109            final String columns = attributes.getValue("columns");
110            if (columns != null) {
111                suppress.setColumns(columns);
112            }
113            filterChain.addFilter(suppress);
114        }
115    }
116
117    /**
118     * Returns the suppression filters in a specified file.
119     * @param filename name of the suppressions file.
120     * @return the filter chain of suppression elements specified in the file.
121     * @throws CheckstyleException if an error occurs.
122     */
123    public static FilterSet loadSuppressions(String filename)
124            throws CheckstyleException {
125        // figure out if this is a File or a URL
126        final URI uri = CommonUtils.getUriByFilename(filename);
127        final InputSource source = new InputSource(uri.toString());
128        return loadSuppressions(source, filename);
129    }
130
131    /**
132     * Returns the suppression filters in a specified source.
133     * @param source the source for the suppressions.
134     * @param sourceName the name of the source.
135     * @return the filter chain of suppression elements in source.
136     * @throws CheckstyleException if an error occurs.
137     */
138    private static FilterSet loadSuppressions(
139            InputSource source, String sourceName)
140            throws CheckstyleException {
141        try {
142            final SuppressionsLoader suppressionsLoader =
143                new SuppressionsLoader();
144            suppressionsLoader.parseInputSource(source);
145            return suppressionsLoader.filterChain;
146        }
147        catch (final FileNotFoundException ex) {
148            throw new CheckstyleException(UNABLE_TO_FIND_ERROR_MESSAGE + sourceName, ex);
149        }
150        catch (final ParserConfigurationException | SAXException ex) {
151            final String message = String.format(Locale.ROOT, "Unable to parse %s - %s",
152                    sourceName, ex.getMessage());
153            throw new CheckstyleException(message, ex);
154        }
155        catch (final IOException ex) {
156            throw new CheckstyleException("Unable to read " + sourceName, ex);
157        }
158        catch (final NumberFormatException ex) {
159            final String message = String.format(Locale.ROOT, "Number format exception %s - %s",
160                    sourceName, ex.getMessage());
161            throw new CheckstyleException(message, ex);
162        }
163    }
164
165    /**
166     * Creates mapping between local resources and dtd ids.
167     * @return map between local resources and dtd ids.
168     */
169    private static Map<String, String> createIdToResourceNameMap() {
170        final Map<String, String> map = Maps.newHashMap();
171        map.put(DTD_PUBLIC_ID_1_0, DTD_RESOURCE_NAME_1_0);
172        map.put(DTD_PUBLIC_ID_1_1, DTD_RESOURCE_NAME_1_1);
173        return map;
174    }
175}