-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ImportControlLoader.java
215 lines (192 loc) · 8.26 KB
/
ImportControlLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2016 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.imports;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.puppycrawl.tools.checkstyle.api.AbstractLoader;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
/**
* Responsible for loading the contents of an import control configuration file.
* @author Oliver Burn
*/
final class ImportControlLoader extends AbstractLoader {
/** The public ID for the configuration dtd. */
private static final String DTD_PUBLIC_ID_1_0 =
"-//Puppy Crawl//DTD Import Control 1.0//EN";
/** The public ID for the configuration dtd. */
private static final String DTD_PUBLIC_ID_1_1 =
"-//Puppy Crawl//DTD Import Control 1.1//EN";
/** The resource for the configuration dtd. */
private static final String DTD_RESOURCE_NAME_1_0 =
"com/puppycrawl/tools/checkstyle/checks/imports/import_control_1_0.dtd";
/** The resource for the configuration dtd. */
private static final String DTD_RESOURCE_NAME_1_1 =
"com/puppycrawl/tools/checkstyle/checks/imports/import_control_1_1.dtd";
/** The map to lookup the resource name by the id. */
private static final Map<String, String> DTD_RESOURCE_BY_ID = new HashMap<>();
/** Name for attribute 'pkg'. */
private static final String PKG_ATTRIBUTE_NAME = "pkg";
/** Qualified name for element 'subpackage'. */
private static final String SUBPACKAGE_ELEMENT_NAME = "subpackage";
/** Qualified name for element 'allow'. */
private static final String ALLOW_ELEMENT_NAME = "allow";
/** Used to hold the {@link PkgControl} objects. */
private final Deque<PkgControl> stack = new ArrayDeque<>();
static {
DTD_RESOURCE_BY_ID.put(DTD_PUBLIC_ID_1_0, DTD_RESOURCE_NAME_1_0);
DTD_RESOURCE_BY_ID.put(DTD_PUBLIC_ID_1_1, DTD_RESOURCE_NAME_1_1);
}
/**
* Constructs an instance.
* @throws ParserConfigurationException if an error occurs.
* @throws SAXException if an error occurs.
*/
private ImportControlLoader() throws ParserConfigurationException,
SAXException {
super(DTD_RESOURCE_BY_ID);
}
@Override
public void startElement(final String namespaceUri,
final String localName,
final String qName,
final Attributes attributes)
throws SAXException {
if ("import-control".equals(qName)) {
final String pkg = safeGet(attributes, PKG_ATTRIBUTE_NAME);
final boolean regex = containsRegexAttribute(attributes);
stack.push(new PkgControl(pkg, regex));
}
else if (SUBPACKAGE_ELEMENT_NAME.equals(qName)) {
final String name = safeGet(attributes, "name");
final boolean regex = containsRegexAttribute(attributes);
stack.push(new PkgControl(stack.peek(), name, regex));
}
else if (ALLOW_ELEMENT_NAME.equals(qName) || "disallow".equals(qName)) {
// Need to handle either "pkg" or "class" attribute.
// May have "exact-match" for "pkg"
// May have "local-only"
final boolean isAllow = ALLOW_ELEMENT_NAME.equals(qName);
final boolean isLocalOnly = attributes.getValue("local-only") != null;
final String pkg = attributes.getValue(PKG_ATTRIBUTE_NAME);
final boolean regex = containsRegexAttribute(attributes);
final AbstractImportRule rule;
if (pkg == null) {
// handle class names which can be normal class names or regular
// expressions
final String clazz = safeGet(attributes, "class");
rule = new ClassImportRule(isAllow, isLocalOnly, clazz, regex);
}
else {
final boolean exactMatch =
attributes.getValue("exact-match") != null;
rule = new PkgImportRule(isAllow, isLocalOnly, pkg, exactMatch, regex);
}
stack.peek().addImportRule(rule);
}
}
/**
* Check if the given attributes contain the regex attribute.
* @param attributes the attributes.
* @return if the regex attribute is contained.
*/
private static boolean containsRegexAttribute(final Attributes attributes) {
return attributes.getValue("regex") != null;
}
@Override
public void endElement(final String namespaceUri, final String localName,
final String qName) {
if (SUBPACKAGE_ELEMENT_NAME.equals(qName)) {
stack.pop();
}
}
/**
* Loads the import control file from a file.
* @param uri the uri of the file to load.
* @return the root {@link PkgControl} object.
* @throws CheckstyleException if an error occurs.
*/
public static PkgControl load(final URI uri) throws CheckstyleException {
final InputStream inputStream;
try {
inputStream = uri.toURL().openStream();
}
catch (final MalformedURLException ex) {
throw new CheckstyleException("syntax error in url " + uri, ex);
}
catch (final IOException ex) {
throw new CheckstyleException("unable to find " + uri, ex);
}
final InputSource source = new InputSource(inputStream);
return load(source, uri);
}
/**
* Loads the import control file from a {@link InputSource}.
* @param source the source to load from.
* @param uri uri of the source being loaded.
* @return the root {@link PkgControl} object.
* @throws CheckstyleException if an error occurs.
*/
private static PkgControl load(final InputSource source,
final URI uri) throws CheckstyleException {
try {
final ImportControlLoader loader = new ImportControlLoader();
loader.parseInputSource(source);
return loader.getRoot();
}
catch (final ParserConfigurationException | SAXException ex) {
throw new CheckstyleException("unable to parse " + uri
+ " - " + ex.getMessage(), ex);
}
catch (final IOException ex) {
throw new CheckstyleException("unable to read " + uri, ex);
}
}
/**
* @return the root {@link PkgControl} object loaded.
*/
private PkgControl getRoot() {
return stack.peek();
}
/**
* Utility to safely get an attribute. If it does not exist an exception
* is thrown.
* @param attributes collect to get attribute from.
* @param name name of the attribute to get.
* @return the value of the attribute.
* @throws SAXException if the attribute does not exist.
*/
private static String safeGet(final Attributes attributes, final String name)
throws SAXException {
final String returnValue = attributes.getValue(name);
if (returnValue == null) {
throw new SAXException("missing attribute " + name);
}
return returnValue;
}
}