KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > imports > ImportControlLoader


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.checks.imports;
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.util.Stack JavaDoc;
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * Responsible for loading the contents of an import control configuration file.
34  * @author Oliver Burn
35  */

36 final class ImportControlLoader extends AbstractLoader
37 {
38     /** the public ID for the configuration dtd */
39     private static final String JavaDoc DTD_PUBLIC_ID =
40         "-//Puppy Crawl//DTD Import Control 1.0//EN";
41
42     /** the resource for the configuration dtd */
43     private static final String JavaDoc DTD_RESOURCE_NAME =
44         "com/puppycrawl/tools/checkstyle/checks/imports/import_control_1_0.dtd";
45
46     /** Used to hold the {@link PkgControl} objects. */
47     private final Stack JavaDoc mStack = new Stack JavaDoc();
48
49     /**
50      * Constructs an instance.
51      * @throws ParserConfigurationException if an error occurs.
52      * @throws SAXException if an error occurs.
53      */

54     private ImportControlLoader() throws ParserConfigurationException JavaDoc,
55             SAXException JavaDoc
56     {
57         super(DTD_PUBLIC_ID, DTD_RESOURCE_NAME);
58     }
59
60     /** {@inheritDoc} */
61     public void startElement(final String JavaDoc aNamespaceURI,
62                              final String JavaDoc aLocalName,
63                              final String JavaDoc aQName,
64                              final Attributes JavaDoc aAtts)
65         throws SAXException JavaDoc
66     {
67         if (aQName.equals("import-control")) {
68             final String JavaDoc pkg = safeGet(aAtts, "pkg");
69             mStack.push(new PkgControl(pkg));
70         }
71         else if (aQName.equals("subpackage")) {
72             assert mStack.size() > 0;
73             final String JavaDoc name = safeGet(aAtts, "name");
74             mStack.push(new PkgControl((PkgControl) mStack.peek(), name));
75         }
76         else if (aQName.equals("allow") || aQName.equals("disallow")) {
77             assert mStack.size() > 0;
78             // Need to handle either "pkg" or "class" attribute.
79
// May have "exact-match" for "pkg"
80
// May have "local-only"
81
final boolean isAllow = aQName.equals("allow");
82             final boolean isLocalOnly = (aAtts.getValue("local-only") != null);
83             final String JavaDoc pkg = aAtts.getValue("pkg");
84             final Guard g;
85             if (pkg != null) {
86                 final boolean exactMatch =
87                     (aAtts.getValue("exact-match") != null);
88                 g = new Guard(isAllow, isLocalOnly, pkg, exactMatch);
89             }
90             else {
91                 final String JavaDoc clazz = safeGet(aAtts, "class");
92                 g = new Guard(isAllow, isLocalOnly, clazz);
93             }
94
95             final PkgControl pc = (PkgControl) mStack.peek();
96             pc.addGuard(g);
97         }
98     }
99
100     /** {@inheritDoc} */
101     public void endElement(final String JavaDoc aNamespaceURI, final String JavaDoc aLocalName,
102         final String JavaDoc aQName)
103     {
104         if (aQName.equals("subpackage")) {
105             assert mStack.size() > 1;
106             mStack.pop();
107         }
108     }
109
110     /**
111      * Loads the import control file from a file.
112      * @param aFilename the name of the file to load.
113      * @return the root {@link PkgControl} object.
114      * @throws CheckstyleException if an error occurs.
115      */

116     static PkgControl load(final String JavaDoc aFilename) throws CheckstyleException
117     {
118         FileInputStream JavaDoc fis = null;
119         try {
120             fis = new FileInputStream JavaDoc(aFilename);
121         }
122         catch (final FileNotFoundException JavaDoc e) {
123             throw new CheckstyleException("unable to find " + aFilename, e);
124         }
125         final InputSource JavaDoc source = new InputSource JavaDoc(fis);
126         return load(source, aFilename);
127     }
128
129     /**
130      * Loads the import control file from a {@link InputSource}.
131      * @param aSource the source to load from.
132      * @param aSourceName name of the source being loaded.
133      * @return the root {@link PkgControl} object.
134      * @throws CheckstyleException if an error occurs.
135      */

136     private static PkgControl load(final InputSource JavaDoc aSource,
137         final String JavaDoc aSourceName) throws CheckstyleException
138     {
139         try {
140             final ImportControlLoader loader = new ImportControlLoader();
141             loader.parseInputSource(aSource);
142             return loader.getRoot();
143         }
144         catch (final ParserConfigurationException JavaDoc e) {
145             throw new CheckstyleException("unable to parse " + aSourceName, e);
146         }
147         catch (final SAXException JavaDoc e) {
148             throw new CheckstyleException("unable to parse " + aSourceName
149                     + " - " + e.getMessage(), e);
150         }
151         catch (final IOException JavaDoc e) {
152             throw new CheckstyleException("unable to read " + aSourceName, e);
153         }
154     }
155
156     /**
157      * @return the root {@link PkgControl} object loaded.
158      */

159     private PkgControl getRoot()
160     {
161         assert mStack.size() == 1;
162         return (PkgControl) mStack.peek();
163     }
164
165     /**
166      * Utility to safely get an attribute. If it does not exist an exception
167      * is thrown.
168      * @param aAtts collect to get attribute from.
169      * @param aName name of the attribute to get.
170      * @return the value of the attribute.
171      * @throws SAXException if the attribute does not exist.
172      */

173     private String JavaDoc safeGet(final Attributes JavaDoc aAtts, String JavaDoc aName)
174         throws SAXException JavaDoc
175     {
176         final String JavaDoc retVal = aAtts.getValue(aName);
177         if (retVal == null) {
178             throw new SAXException JavaDoc("missing attibute " + aName);
179         }
180         return retVal;
181     }
182 }
183
Popular Tags