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