KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > filters > SuppressionsLoader


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.filters;
20
21 import com.puppycrawl.tools.checkstyle.api.AbstractLoader;
22 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
23 import com.puppycrawl.tools.checkstyle.api.FilterSet;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.regex.PatternSyntaxException JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * Loads a filter chain of suppressions.
37  * @author Rick Giles
38  */

39 public final class SuppressionsLoader
40     extends AbstractLoader
41 {
42     /** the public ID for the configuration dtd */
43     private static final String JavaDoc DTD_PUBLIC_ID_1_0 =
44         "-//Puppy Crawl//DTD Suppressions 1.0//EN";
45     /** the resource for the configuration dtd */
46     private static final String JavaDoc DTD_RESOURCE_NAME_1_0 =
47         "com/puppycrawl/tools/checkstyle/suppressions_1_0.dtd";
48     /** the public ID for the configuration dtd */
49     private static final String JavaDoc DTD_PUBLIC_ID_1_1 =
50         "-//Puppy Crawl//DTD Suppressions 1.1//EN";
51     /** the resource for the configuration dtd */
52     private static final String JavaDoc DTD_RESOURCE_NAME_1_1 =
53         "com/puppycrawl/tools/checkstyle/suppressions_1_1.dtd";
54
55     /**
56      * the filter chain to return in getAFilterChain(),
57      * configured during parsing
58      */

59     private final FilterSet mFilterChain = new FilterSet();
60
61     /**
62      * Creates a new <code>SuppressionsLoader</code> instance.
63      * @throws ParserConfigurationException if an error occurs
64      * @throws SAXException if an error occurs
65      */

66     private SuppressionsLoader()
67         throws ParserConfigurationException JavaDoc, SAXException JavaDoc
68     {
69         super(createIdToResourceNameMap());
70     }
71
72     /**
73      * Returns the loaded filter chain.
74      * @return the loaded filter chain.
75      */

76     public FilterSet getFilterChain()
77     {
78         return mFilterChain;
79     }
80
81     /** {@inheritDoc} **/
82     public void startElement(String JavaDoc aNamespaceURI,
83                              String JavaDoc aLocalName,
84                              String JavaDoc aQName,
85                              Attributes JavaDoc aAtts)
86         throws SAXException JavaDoc
87     {
88         if (aQName.equals("suppress")) {
89             //add SuppressElement filter to the filter chain
90
final String JavaDoc files = aAtts.getValue("files");
91             if (files == null) {
92                 throw new SAXException JavaDoc("missing files attribute");
93             }
94             final String JavaDoc checks = aAtts.getValue("checks");
95             final String JavaDoc modId = aAtts.getValue("id");
96             if ((checks == null) && (modId == null)) {
97                 throw new SAXException JavaDoc("missing checks and id attribute");
98             }
99             final SuppressElement suppress;
100             try {
101                 suppress = new SuppressElement(files);
102                 if (modId != null) {
103                     suppress.setModuleId(modId);
104                 }
105                 if (checks != null) {
106                     suppress.setChecks(checks);
107                 }
108             }
109             catch (final PatternSyntaxException JavaDoc e) {
110                 throw new SAXException JavaDoc("invalid files or checks format");
111             }
112             final String JavaDoc lines = aAtts.getValue("lines");
113             if (lines != null) {
114                 suppress.setLines(lines);
115             }
116             final String JavaDoc columns = aAtts.getValue("columns");
117             if (columns != null) {
118                 suppress.setColumns(columns);
119             }
120             mFilterChain.addFilter(suppress);
121         }
122     }
123
124     /**
125      * Returns the suppression filters in a specified file.
126      * @param aFilename name of the suppresssions file.
127      * @return the filter chain of suppression elements specified in the file.
128      * @throws CheckstyleException if an error occurs.
129      */

130     public static FilterSet loadSuppressions(String JavaDoc aFilename)
131         throws CheckstyleException
132     {
133         final FileInputStream JavaDoc fis;
134         try {
135             fis = new FileInputStream JavaDoc(aFilename);
136         }
137         catch (final FileNotFoundException JavaDoc e) {
138             throw new CheckstyleException(
139                 "unable to find " + aFilename, e);
140         }
141         final InputSource JavaDoc source = new InputSource JavaDoc(fis);
142         return loadSuppressions(source, aFilename);
143     }
144
145     /**
146      * Returns the suppression filters in a specified source.
147      * @param aSource the source for the suppressions.
148      * @param aSourceName the name of the source.
149      * @return the filter chain of suppression elements in aSource.
150      * @throws CheckstyleException if an error occurs.
151      */

152     private static FilterSet loadSuppressions(
153             InputSource JavaDoc aSource, String JavaDoc aSourceName)
154         throws CheckstyleException
155     {
156         try {
157             final SuppressionsLoader suppressionsLoader =
158                 new SuppressionsLoader();
159             suppressionsLoader.parseInputSource(aSource);
160             return suppressionsLoader.getFilterChain();
161         }
162         catch (final FileNotFoundException JavaDoc e) {
163             throw new CheckstyleException("unable to find " + aSourceName, e);
164         }
165         catch (final ParserConfigurationException JavaDoc e) {
166             throw new CheckstyleException("unable to parse " + aSourceName, e);
167         }
168         catch (final SAXException JavaDoc e) {
169             throw new CheckstyleException("unable to parse "
170                     + aSourceName + " - " + e.getMessage(), e);
171         }
172         catch (final IOException JavaDoc e) {
173             throw new CheckstyleException("unable to read " + aSourceName, e);
174         }
175         catch (final NumberFormatException JavaDoc e) {
176             throw new CheckstyleException("number format exception "
177                 + aSourceName + " - " + e.getMessage(), e);
178         }
179     }
180
181     /**
182      * Creates mapping between local resources and dtd ids.
183      * @return map between local resources and dtd ids.
184      */

185     private static Map JavaDoc createIdToResourceNameMap()
186     {
187         final Map JavaDoc map = new HashMap JavaDoc();
188         map.put(DTD_PUBLIC_ID_1_0, DTD_RESOURCE_NAME_1_0);
189         map.put(DTD_PUBLIC_ID_1_1, DTD_RESOURCE_NAME_1_1);
190         return map;
191     }
192 }
193
Popular Tags