KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > PackageNamesLoader


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
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
package com.puppycrawl.tools.checkstyle;
20
21 import com.puppycrawl.tools.checkstyle.api.AbstractLoader;
22 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Stack JavaDoc;
29 import javax.xml.parsers.ParserConfigurationException JavaDoc;
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * Loads a list of package names from a package name XML file.
36  * @author Rick Giles
37  * @version 4-Dec-2002
38  */

39 public final class PackageNamesLoader
40     extends AbstractLoader
41 {
42     /** the public ID for the configuration dtd */
43     private static final String JavaDoc DTD_PUBLIC_ID =
44         "-//Puppy Crawl//DTD Package Names 1.0//EN";
45
46     /** the resource for the configuration dtd */
47     private static final String JavaDoc DTD_RESOURCE_NAME =
48         "com/puppycrawl/tools/checkstyle/packages_1_0.dtd";
49
50     /** Name of default checkstyle package names resource file.
51      * The file must be in the classpath.
52      */

53     private static final String JavaDoc DEFAULT_PACKAGES =
54         "com/puppycrawl/tools/checkstyle/checkstyle_packages.xml";
55
56     /**
57      * the factory to return in getModuleFactory(),
58      * configured during parsing
59      */

60     private final PackageObjectFactory mModuleFactory =
61         new PackageObjectFactory();
62
63     /** The loaded package names */
64     private final Stack JavaDoc mPackageStack = new Stack JavaDoc();
65
66     /**
67      * Creates a new <code>PackageNamesLoader</code> instance.
68      * @throws ParserConfigurationException if an error occurs
69      * @throws SAXException if an error occurs
70      */

71     private PackageNamesLoader()
72         throws ParserConfigurationException JavaDoc, SAXException JavaDoc
73     {
74         super(DTD_PUBLIC_ID, DTD_RESOURCE_NAME);
75     }
76
77     /** {@inheritDoc} */
78     public void startElement(String JavaDoc aNamespaceURI,
79                              String JavaDoc aLocalName,
80                              String JavaDoc aQName,
81                              Attributes JavaDoc aAtts)
82         throws SAXException JavaDoc
83     {
84         if (aQName.equals("package")) {
85             //push package name
86
final String JavaDoc name = aAtts.getValue("name");
87             if (name == null) {
88                 throw new SAXException JavaDoc("missing package name");
89             }
90             mPackageStack.push(name);
91         }
92     }
93
94     /**
95      * Creates a full package name from the package names on the stack.
96      * @return the full name of the current package.
97      */

98     private String JavaDoc getPackageName()
99     {
100         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
101         final Iterator JavaDoc it = mPackageStack.iterator();
102         while (it.hasNext()) {
103             final String JavaDoc subPackage = (String JavaDoc) it.next();
104             buf.append(subPackage);
105             if (!subPackage.endsWith(".")) {
106                 buf.append(".");
107             }
108         }
109         return buf.toString();
110     }
111
112     /**
113      * Returns the module factory that has just been configured.
114      * @return the module factory, never null
115      */

116     private ModuleFactory getModuleFactory()
117     {
118         return mModuleFactory;
119     }
120
121     /** {@inheritDoc} */
122     public void endElement(String JavaDoc aNamespaceURI,
123                            String JavaDoc aLocalName,
124                            String JavaDoc aQName)
125     {
126         if (aQName.equals("package")) {
127             mModuleFactory.addPackage(getPackageName());
128             mPackageStack.pop();
129         }
130     }
131
132     /**
133      * Returns the default list of package names.
134      * @param aClassLoader the class loader that gets the
135      * default package names.
136      * @return the default list of package names.
137      * @throws CheckstyleException if an error occurs.
138      */

139     public static ModuleFactory loadModuleFactory(ClassLoader JavaDoc aClassLoader)
140         throws CheckstyleException
141     {
142
143         final InputStream JavaDoc stream =
144             aClassLoader.getResourceAsStream(DEFAULT_PACKAGES);
145         final InputSource JavaDoc source = new InputSource JavaDoc(stream);
146         return loadModuleFactory(source, "default package names");
147     }
148
149     /**
150      * Returns the package names in a specified file.
151      * @param aFilename name of the package file.
152      * @return the list of package names stored in the
153      * package file.
154      * @throws CheckstyleException if an error occurs.
155      */

156     public static ModuleFactory loadModuleFactory(String JavaDoc aFilename)
157         throws CheckstyleException
158     {
159         FileInputStream JavaDoc fis = null;
160         try {
161             fis = new FileInputStream JavaDoc(aFilename);
162         }
163         catch (final FileNotFoundException JavaDoc e) {
164             throw new CheckstyleException(
165                 "unable to find " + aFilename, e);
166         }
167         final InputSource JavaDoc source = new InputSource JavaDoc(fis);
168         return loadModuleFactory(source, aFilename);
169     }
170
171     /**
172      * Returns the list of package names in a specified source.
173      * @param aSource the source for the list.
174      * @param aSourceName the name of the source.
175      * @return the list ofpackage names stored in aSource.
176      * @throws CheckstyleException if an error occurs.
177      */

178     private static ModuleFactory loadModuleFactory(
179             InputSource JavaDoc aSource, String JavaDoc aSourceName)
180         throws CheckstyleException
181     {
182         try {
183             final PackageNamesLoader nameLoader = new PackageNamesLoader();
184             nameLoader.parseInputSource(aSource);
185             return nameLoader.getModuleFactory();
186         }
187         catch (final FileNotFoundException JavaDoc e) {
188             throw new CheckstyleException("unable to find " + aSourceName, e);
189         }
190         catch (final ParserConfigurationException JavaDoc e) {
191             throw new CheckstyleException("unable to parse " + aSourceName, e);
192         }
193         catch (final SAXException JavaDoc e) {
194             throw new CheckstyleException("unable to parse "
195                     + aSourceName + " - " + e.getMessage(), e);
196         }
197         catch (final IOException JavaDoc e) {
198             throw new CheckstyleException("unable to read " + aSourceName, e);
199         }
200     }
201 }
202
Popular Tags