001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2024 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.net.URI; 029import java.nio.charset.Charset; 030import java.nio.charset.UnsupportedCharsetException; 031import java.util.ArrayList; 032import java.util.List; 033import java.util.Set; 034import java.util.regex.Pattern; 035 036import com.puppycrawl.tools.checkstyle.PropertyType; 037import com.puppycrawl.tools.checkstyle.XdocsPropertyType; 038import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 039import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 040import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder; 041import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 042 043/** 044 * Abstract super class for header checks. 045 * Provides support for header and headerFile properties. 046 */ 047public abstract class AbstractHeaderCheck extends AbstractFileSetCheck 048 implements ExternalResourceHolder { 049 050 /** Pattern to detect occurrences of '\n' in text. */ 051 private static final Pattern ESCAPED_LINE_FEED_PATTERN = Pattern.compile("\\\\n"); 052 053 /** The lines of the header file. */ 054 private final List<String> readerLines = new ArrayList<>(); 055 056 /** Specify the name of the file containing the required header. */ 057 private URI headerFile; 058 059 /** Specify the character encoding to use when reading the headerFile. */ 060 @XdocsPropertyType(PropertyType.STRING) 061 private Charset charset; 062 063 /** 064 * Hook method for post-processing header lines. 065 * This implementation does nothing. 066 */ 067 protected abstract void postProcessHeaderLines(); 068 069 /** 070 * Return the header lines to check against. 071 * 072 * @return the header lines to check against. 073 */ 074 protected List<String> getHeaderLines() { 075 return List.copyOf(readerLines); 076 } 077 078 /** 079 * Setter to specify the character encoding to use when reading the headerFile. 080 * 081 * @param charset the charset name to use for loading the header from a file 082 */ 083 public void setCharset(String charset) { 084 this.charset = createCharset(charset); 085 } 086 087 /** 088 * Setter to specify the name of the file containing the required header. 089 * 090 * @param uri the uri of the header to load. 091 * @throws CheckstyleException if fileName is empty. 092 */ 093 public void setHeaderFile(URI uri) throws CheckstyleException { 094 if (uri == null) { 095 throw new CheckstyleException( 096 "property 'headerFile' is missing or invalid in module " 097 + getConfiguration().getName()); 098 } 099 100 headerFile = uri; 101 } 102 103 /** 104 * Load the header from a file. 105 * 106 * @throws CheckstyleException if the file cannot be loaded 107 */ 108 private void loadHeaderFile() throws CheckstyleException { 109 checkHeaderNotInitialized(); 110 try (Reader headerReader = new InputStreamReader(new BufferedInputStream( 111 headerFile.toURL().openStream()), charset)) { 112 loadHeader(headerReader); 113 } 114 catch (final IOException ex) { 115 throw new CheckstyleException( 116 "unable to load header file " + headerFile, ex); 117 } 118 } 119 120 /** 121 * Called before initializing the header. 122 * 123 * @throws IllegalArgumentException if header has already been set 124 */ 125 private void checkHeaderNotInitialized() { 126 if (!readerLines.isEmpty()) { 127 throw new IllegalArgumentException( 128 "header has already been set - " 129 + "set either header or headerFile, not both"); 130 } 131 } 132 133 /** 134 * Creates charset by name. 135 * 136 * @param name charset name 137 * @return created charset 138 * @throws UnsupportedCharsetException if charset is unsupported 139 */ 140 private static Charset createCharset(String name) { 141 if (!Charset.isSupported(name)) { 142 final String message = "unsupported charset: '" + name + "'"; 143 throw new UnsupportedCharsetException(message); 144 } 145 return Charset.forName(name); 146 } 147 148 /** 149 * Specify the required header specified inline. 150 * Individual header lines must be separated by the string 151 * {@code "\n"}(even on platforms with a different line separator). 152 * 153 * @param header header content to check against. 154 * @throws IllegalArgumentException if the header cannot be interpreted 155 */ 156 public void setHeader(String header) { 157 if (!CommonUtil.isBlank(header)) { 158 checkHeaderNotInitialized(); 159 160 final String headerExpandedNewLines = ESCAPED_LINE_FEED_PATTERN 161 .matcher(header).replaceAll("\n"); 162 163 try (Reader headerReader = new StringReader(headerExpandedNewLines)) { 164 loadHeader(headerReader); 165 } 166 catch (final IOException ex) { 167 throw new IllegalArgumentException("unable to load header", ex); 168 } 169 } 170 } 171 172 /** 173 * Load header to check against from a Reader into readerLines. 174 * 175 * @param headerReader delivers the header to check against 176 * @throws IOException if 177 */ 178 private void loadHeader(final Reader headerReader) throws IOException { 179 try (LineNumberReader lnr = new LineNumberReader(headerReader)) { 180 String line; 181 do { 182 line = lnr.readLine(); 183 if (line != null) { 184 readerLines.add(line); 185 } 186 } while (line != null); 187 postProcessHeaderLines(); 188 } 189 } 190 191 @Override 192 protected final void finishLocalSetup() throws CheckstyleException { 193 if (headerFile != null) { 194 loadHeaderFile(); 195 } 196 } 197 198 @Override 199 public Set<String> getExternalResourceLocations() { 200 final Set<String> result; 201 202 if (headerFile == null) { 203 result = Set.of(); 204 } 205 else { 206 result = Set.of(headerFile.toASCIIString()); 207 } 208 209 return result; 210 } 211 212}