001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.io.BufferedInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.net.URL;
026import java.util.ArrayDeque;
027import java.util.Deque;
028import java.util.Enumeration;
029import java.util.Iterator;
030import java.util.LinkedHashSet;
031import java.util.Set;
032
033import javax.xml.parsers.ParserConfigurationException;
034
035import org.xml.sax.Attributes;
036import org.xml.sax.InputSource;
037import org.xml.sax.SAXException;
038
039import com.google.common.io.Closeables;
040import com.puppycrawl.tools.checkstyle.api.AbstractLoader;
041import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
042import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
043
044/**
045 * Loads a list of package names from a package name XML file.
046 * @author Rick Giles
047 */
048public final class PackageNamesLoader
049    extends AbstractLoader {
050    /** The public ID for the configuration dtd. */
051    private static final String DTD_PUBLIC_ID =
052        "-//Puppy Crawl//DTD Package Names 1.0//EN";
053
054    /** The resource for the configuration dtd. */
055    private static final String DTD_RESOURCE_NAME =
056        "com/puppycrawl/tools/checkstyle/packages_1_0.dtd";
057
058    /** Name of default checkstyle package names resource file.
059     * The file must be in the classpath.
060     */
061    private static final String CHECKSTYLE_PACKAGES =
062        "checkstyle_packages.xml";
063
064    /** Qualified name for element 'package'. */
065    private static final String PACKAGE_ELEMENT_NAME = "package";
066
067    /** The temporary stack of package name parts. */
068    private final Deque<String> packageStack = new ArrayDeque<>();
069
070    /** The fully qualified package names. */
071    private final Set<String> packageNames = new LinkedHashSet<>();
072
073    /**
074     * Creates a new {@code PackageNamesLoader} instance.
075     * @throws ParserConfigurationException if an error occurs
076     * @throws SAXException if an error occurs
077     */
078    private PackageNamesLoader()
079            throws ParserConfigurationException, SAXException {
080        super(DTD_PUBLIC_ID, DTD_RESOURCE_NAME);
081    }
082
083    @Override
084    public void startElement(String uri,
085                             String localName,
086                             String qName,
087                             Attributes attributes) {
088        if (PACKAGE_ELEMENT_NAME.equals(qName)) {
089            //push package name, name is mandatory attribute with not empty value by DTD
090            final String name = attributes.getValue("name");
091            packageStack.push(name);
092        }
093    }
094
095    /**
096     * Creates a full package name from the package names on the stack.
097     * @return the full name of the current package.
098     */
099    private String getPackageName() {
100        final StringBuilder buf = new StringBuilder();
101        final Iterator<String> iterator = packageStack.descendingIterator();
102        while (iterator.hasNext()) {
103            final String subPackage = iterator.next();
104            buf.append(subPackage);
105            if (!CommonUtils.endsWithChar(subPackage, '.') && iterator.hasNext()) {
106                buf.append('.');
107            }
108        }
109        return buf.toString();
110    }
111
112    @Override
113    public void endElement(String uri,
114                           String localName,
115                           String qName) {
116        if (PACKAGE_ELEMENT_NAME.equals(qName)) {
117
118            packageNames.add(getPackageName());
119            packageStack.pop();
120        }
121    }
122
123    /**
124     * Returns the set of package names, compiled from all
125     * checkstyle_packages.xml files found on the given class loaders
126     * classpath.
127     * @param classLoader the class loader for loading the
128     *          checkstyle_packages.xml files.
129     * @return the set of package names.
130     * @throws CheckstyleException if an error occurs.
131     */
132    public static Set<String> getPackageNames(ClassLoader classLoader)
133            throws CheckstyleException {
134
135        final Set<String> result;
136        try {
137            //create the loader outside the loop to prevent PackageObjectFactory
138            //being created anew for each file
139            final PackageNamesLoader namesLoader = new PackageNamesLoader();
140
141            final Enumeration<URL> packageFiles = classLoader.getResources(CHECKSTYLE_PACKAGES);
142
143            while (packageFiles.hasMoreElements()) {
144                final URL packageFile = packageFiles.nextElement();
145                InputStream stream = null;
146
147                try {
148                    stream = new BufferedInputStream(packageFile.openStream());
149                    final InputSource source = new InputSource(stream);
150                    namesLoader.parseInputSource(source);
151                }
152                catch (IOException ex) {
153                    throw new CheckstyleException("unable to open " + packageFile, ex);
154                }
155                finally {
156                    Closeables.closeQuietly(stream);
157                }
158            }
159
160            result = namesLoader.packageNames;
161
162        }
163        catch (IOException ex) {
164            throw new CheckstyleException("unable to get package file resources", ex);
165        }
166        catch (ParserConfigurationException | SAXException ex) {
167            throw new CheckstyleException("unable to open one of package files", ex);
168        }
169
170        return result;
171    }
172}