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