View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle;
21  
22  import java.io.BufferedInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.URL;
26  import java.util.ArrayDeque;
27  import java.util.Collections;
28  import java.util.Deque;
29  import java.util.Enumeration;
30  import java.util.HashMap;
31  import java.util.Iterator;
32  import java.util.LinkedHashSet;
33  import java.util.Map;
34  import java.util.Set;
35  
36  import javax.xml.parsers.ParserConfigurationException;
37  
38  import org.xml.sax.Attributes;
39  import org.xml.sax.InputSource;
40  import org.xml.sax.SAXException;
41  
42  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
43  
44  /**
45   * Loads a list of package names from a package name XML file.
46   */
47  public final class PackageNamesLoader
48      extends XmlLoader {
49  
50      /** The public ID for the configuration dtd. */
51      private static final String DTD_PUBLIC_ID =
52          "-//Puppy Crawl//DTD Package Names 1.0//EN";
53  
54      /** The new public ID for the configuration dtd. */
55      private static final String DTD_PUBLIC_CS_ID =
56          "-//Checkstyle//DTD Package Names Configuration 1.0//EN";
57  
58      /** The resource for the configuration dtd. */
59      private static final String DTD_RESOURCE_NAME =
60          "com/puppycrawl/tools/checkstyle/packages_1_0.dtd";
61  
62      /**
63       * Name of default checkstyle package names resource file.
64       * The file must be in the classpath.
65       */
66      private static final String CHECKSTYLE_PACKAGES =
67          "checkstyle_packages.xml";
68  
69      /** Qualified name for element 'package'. */
70      private static final String PACKAGE_ELEMENT_NAME = "package";
71  
72      /** The temporary stack of package name parts. */
73      private final Deque<String> packageStack = new ArrayDeque<>();
74  
75      /** The fully qualified package names. */
76      private final Set<String> packageNames = new LinkedHashSet<>();
77  
78      /**
79       * Creates a new {@code PackageNamesLoader} instance.
80       *
81       * @throws ParserConfigurationException if an error occurs
82       * @throws SAXException if an error occurs
83       */
84      private PackageNamesLoader()
85              throws ParserConfigurationException, SAXException {
86          super(createIdToResourceNameMap());
87      }
88  
89      @Override
90      public void startElement(String uri,
91                               String localName,
92                               String qName,
93                               Attributes attributes) {
94          if (PACKAGE_ELEMENT_NAME.equals(qName)) {
95              // push package name, name is mandatory attribute with not empty value by DTD
96              final String name = attributes.getValue("name");
97              packageStack.push(name);
98          }
99      }
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 }