1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.puppycrawl.tools.checkstyle.checks.header;
20
21 import java.io.BufferedInputStream;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.io.LineNumberReader;
27 import java.io.Reader;
28 import java.io.StringReader;
29 import java.io.UnsupportedEncodingException;
30 import java.net.MalformedURLException;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.net.URL;
34 import java.nio.charset.Charset;
35 import java.util.List;
36
37 import org.apache.commons.beanutils.ConversionException;
38
39 import com.google.common.collect.ImmutableList;
40 import com.google.common.collect.Lists;
41 import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
42 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
43 import com.puppycrawl.tools.checkstyle.api.Utils;
44
45
46
47
48
49
50 public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
51 {
52
53 private String filename;
54
55
56 private String charset = System.getProperty("file.encoding", "UTF-8");
57
58
59 private final List<String> readerLines = Lists.newArrayList();
60
61
62
63
64
65
66 protected ImmutableList<String> getHeaderLines()
67 {
68 return ImmutableList.copyOf(readerLines);
69 }
70
71
72
73
74
75
76 public void setCharset(String charset) throws UnsupportedEncodingException
77 {
78 if (!Charset.isSupported(charset)) {
79 final String message = "unsupported charset: '" + charset + "'";
80 throw new UnsupportedEncodingException(message);
81 }
82 this.charset = charset;
83 }
84
85
86
87
88
89 public void setHeaderFile(String fileName)
90 {
91
92 if (fileName == null || fileName.trim().length() == 0) {
93 return;
94 }
95
96 filename = fileName;
97 }
98
99
100
101
102
103 private void loadHeaderFile() throws CheckstyleException
104 {
105 checkHeaderNotInitialized();
106 Reader headerReader = null;
107 try {
108 final URI uri = resolveHeaderFile();
109 headerReader = new InputStreamReader(new BufferedInputStream(
110 uri.toURL().openStream()), charset);
111 loadHeader(headerReader);
112 }
113 catch (final IOException ex) {
114 throw new CheckstyleException(
115 "unable to load header file " + filename, ex);
116 }
117 finally {
118 Utils.closeQuietly(headerReader);
119 }
120 }
121
122
123
124
125
126
127 private URI resolveHeaderFile() throws IOException
128 {
129
130 URI uri;
131 try {
132 final URL url = new URL(filename);
133 uri = url.toURI();
134 }
135 catch (final MalformedURLException ex) {
136 uri = null;
137 }
138 catch (final URISyntaxException ex) {
139
140 uri = null;
141 }
142 if (uri == null) {
143 final File file = new File(filename);
144 if (file.exists()) {
145 uri = file.toURI();
146 }
147 else {
148
149 try {
150 final URL configUrl = AbstractHeaderCheck.class
151 .getResource(filename);
152 if (configUrl == null) {
153 throw new FileNotFoundException(filename);
154 }
155 uri = configUrl.toURI();
156 }
157 catch (final URISyntaxException e) {
158 throw new FileNotFoundException(filename);
159 }
160 }
161 }
162 return uri;
163 }
164
165
166
167
168
169 private void checkHeaderNotInitialized()
170 {
171 if (!readerLines.isEmpty()) {
172 throw new ConversionException(
173 "header has already been set - "
174 + "set either header or headerFile, not both");
175 }
176 }
177
178
179
180
181
182
183
184 public void setHeader(String header)
185 {
186 if (header == null || header.trim().length() == 0) {
187 return;
188 }
189
190 checkHeaderNotInitialized();
191
192 final String headerExpandedNewLines = header.replaceAll("\\\\n", "\n");
193
194 final Reader headerReader = new StringReader(headerExpandedNewLines);
195 try {
196 loadHeader(headerReader);
197 }
198 catch (final IOException ex) {
199 throw new ConversionException("unable to load header", ex);
200 }
201 finally {
202 Utils.closeQuietly(headerReader);
203 }
204 }
205
206
207
208
209
210
211 private void loadHeader(final Reader headerReader) throws IOException
212 {
213 final LineNumberReader lnr = new LineNumberReader(headerReader);
214 readerLines.clear();
215 while (true) {
216 final String l = lnr.readLine();
217 if (l == null) {
218 break;
219 }
220 readerLines.add(l);
221 }
222 postprocessHeaderLines();
223 }
224
225
226
227
228
229 protected void postprocessHeaderLines()
230 {
231 }
232
233 @Override
234 protected final void finishLocalSetup() throws CheckstyleException
235 {
236 if (filename != null) {
237 loadHeaderFile();
238 }
239 if (readerLines.isEmpty()) {
240 throw new CheckstyleException(
241 "property 'headerFile' is missing or invalid in module "
242 + getConfiguration().getName());
243 }
244 }
245 }