001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.header; 021 022import java.io.BufferedInputStream; 023import java.io.IOException; 024import java.io.InputStreamReader; 025import java.io.LineNumberReader; 026import java.io.Reader; 027import java.io.StringReader; 028import java.io.UnsupportedEncodingException; 029import java.net.URI; 030import java.nio.charset.Charset; 031import java.util.ArrayList; 032import java.util.Collections; 033import java.util.List; 034import java.util.Set; 035import java.util.regex.Pattern; 036 037import org.apache.commons.beanutils.ConversionException; 038 039import com.google.common.io.Closeables; 040import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 041import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 042import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder; 043import com.puppycrawl.tools.checkstyle.utils.CommonUtils; 044 045/** 046 * Abstract super class for header checks. 047 * Provides support for header and headerFile properties. 048 * @author o_sukhosolsky 049 */ 050public abstract class AbstractHeaderCheck extends AbstractFileSetCheck 051 implements ExternalResourceHolder { 052 /** Pattern to detect occurrences of '\n' in text. */ 053 private static final Pattern ESCAPED_LINE_FEED_PATTERN = Pattern.compile("\\\\n"); 054 055 /** The lines of the header file. */ 056 private final List<String> readerLines = new ArrayList<>(); 057 058 /** The file that contains the header to check against. */ 059 private URI headerFile; 060 061 /** Name of a charset to use for loading the header from a file. */ 062 private String charset = System.getProperty("file.encoding", "UTF-8"); 063 064 /** 065 * Hook method for post processing header lines. 066 * This implementation does nothing. 067 */ 068 protected abstract void postProcessHeaderLines(); 069 070 /** 071 * Return the header lines to check against. 072 * @return the header lines to check against. 073 */ 074 protected List<String> getHeaderLines() { 075 final List<String> copy = new ArrayList<>(readerLines); 076 return Collections.unmodifiableList(copy); 077 } 078 079 /** 080 * Set the charset to use for loading the header from a file. 081 * @param charset the charset to use for loading the header from a file 082 * @throws UnsupportedEncodingException if charset is unsupported 083 */ 084 public void setCharset(String charset) throws UnsupportedEncodingException { 085 if (!Charset.isSupported(charset)) { 086 final String message = "unsupported charset: '" + charset + "'"; 087 throw new UnsupportedEncodingException(message); 088 } 089 this.charset = charset; 090 } 091 092 /** 093 * Set the header file to check against. 094 * @param uri the uri of the header to load. 095 * @throws CheckstyleException if fileName is empty. 096 */ 097 public void setHeaderFile(URI uri) throws CheckstyleException { 098 if (uri == null) { 099 throw new CheckstyleException( 100 "property 'headerFile' is missing or invalid in module " 101 + getConfiguration().getName()); 102 } 103 104 headerFile = uri; 105 } 106 107 /** 108 * Load the header from a file. 109 * @throws CheckstyleException if the file cannot be loaded 110 */ 111 private void loadHeaderFile() throws CheckstyleException { 112 checkHeaderNotInitialized(); 113 Reader headerReader = null; 114 try { 115 headerReader = new InputStreamReader(new BufferedInputStream( 116 headerFile.toURL().openStream()), charset); 117 loadHeader(headerReader); 118 } 119 catch (final IOException ex) { 120 throw new CheckstyleException( 121 "unable to load header file " + headerFile, ex); 122 } 123 finally { 124 Closeables.closeQuietly(headerReader); 125 } 126 } 127 128 /** 129 * Called before initializing the header. 130 * @throws ConversionException if header has already been set 131 */ 132 private void checkHeaderNotInitialized() { 133 if (!readerLines.isEmpty()) { 134 throw new ConversionException( 135 "header has already been set - " 136 + "set either header or headerFile, not both"); 137 } 138 } 139 140 /** 141 * Set the header to check against. Individual lines in the header 142 * must be separated by '\n' characters. 143 * @param header header content to check against. 144 * @throws ConversionException if the header cannot be interpreted 145 */ 146 public void setHeader(String header) { 147 if (!CommonUtils.isBlank(header)) { 148 checkHeaderNotInitialized(); 149 150 final String headerExpandedNewLines = ESCAPED_LINE_FEED_PATTERN 151 .matcher(header).replaceAll("\n"); 152 153 final Reader headerReader = new StringReader(headerExpandedNewLines); 154 try { 155 loadHeader(headerReader); 156 } 157 catch (final IOException ex) { 158 throw new ConversionException("unable to load header", ex); 159 } 160 finally { 161 Closeables.closeQuietly(headerReader); 162 } 163 } 164 } 165 166 /** 167 * Load header to check against from a Reader into readerLines. 168 * @param headerReader delivers the header to check against 169 * @throws IOException if 170 */ 171 private void loadHeader(final Reader headerReader) throws IOException { 172 readerLines.clear(); 173 final LineNumberReader lnr = new LineNumberReader(headerReader); 174 while (true) { 175 final String line = lnr.readLine(); 176 if (line == null) { 177 break; 178 } 179 readerLines.add(line); 180 } 181 postProcessHeaderLines(); 182 } 183 184 @Override 185 protected final void finishLocalSetup() throws CheckstyleException { 186 if (headerFile != null) { 187 loadHeaderFile(); 188 } 189 if (readerLines.isEmpty()) { 190 setHeader(null); 191 } 192 } 193 194 @Override 195 public Set<String> getExternalResourceLocations() { 196 final Set<String> result; 197 198 if (headerFile == null) { 199 result = Collections.emptySet(); 200 } 201 else { 202 result = Collections.singleton(headerFile.toString()); 203 } 204 205 return result; 206 } 207}