001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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.api; 021 022import java.io.BufferedReader; 023import java.io.File; 024import java.io.FileNotFoundException; 025import java.io.IOException; 026import java.io.InputStream; 027import java.io.InputStreamReader; 028import java.io.Reader; 029import java.io.StringReader; 030import java.nio.charset.Charset; 031import java.nio.charset.CharsetDecoder; 032import java.nio.charset.CodingErrorAction; 033import java.nio.charset.UnsupportedCharsetException; 034import java.nio.file.Files; 035import java.util.ArrayList; 036import java.util.Arrays; 037import java.util.List; 038import java.util.regex.Matcher; 039import java.util.regex.Pattern; 040 041import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 042 043/** 044 * Represents the text contents of a file of arbitrary plain text type. 045 * <p> 046 * This class will be passed to instances of class FileSetCheck by 047 * Checker. 048 * </p> 049 * 050 */ 051public final class FileText { 052 053 /** 054 * The number of characters to read in one go. 055 */ 056 private static final int READ_BUFFER_SIZE = 1024; 057 058 /** 059 * Regular expression pattern matching all line terminators. 060 */ 061 private static final Pattern LINE_TERMINATOR = Pattern.compile("\\n|\\r\\n?"); 062 063 // For now, we always keep both full text and lines array. 064 // In the long run, however, the one passed at initialization might be 065 // enough, while the other could be lazily created when requested. 066 // This would save memory but cost CPU cycles. 067 068 /** 069 * The name of the file. 070 * {@code null} if no file name is available for whatever reason. 071 */ 072 private final File file; 073 074 /** 075 * The charset used to read the file. 076 * {@code null} if the file was reconstructed from a list of lines. 077 */ 078 private final Charset charset; 079 080 /** 081 * The full text contents of the file. 082 */ 083 private final String fullText; 084 085 /** 086 * The lines of the file, without terminators. 087 */ 088 private final String[] lines; 089 090 /** 091 * The first position of each line within the full text. 092 */ 093 private int[] lineBreaks; 094 095 /** 096 * Creates a new file text representation. 097 * 098 * <p>The file will be read using the specified encoding, replacing 099 * malformed input and unmappable characters with the default 100 * replacement character. 101 * 102 * @param file the name of the file 103 * @param charsetName the encoding to use when reading the file 104 * @throws NullPointerException if the text is null 105 * @throws IOException if the file could not be read 106 */ 107 public FileText(File file, String charsetName) throws IOException { 108 this.file = file; 109 110 // We use our own decoder, to be sure we have complete control 111 // about replacements. 112 final CharsetDecoder decoder; 113 try { 114 charset = Charset.forName(charsetName); 115 decoder = charset.newDecoder(); 116 decoder.onMalformedInput(CodingErrorAction.REPLACE); 117 decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); 118 } 119 catch (final UnsupportedCharsetException ex) { 120 final String message = "Unsupported charset: " + charsetName; 121 throw new IllegalStateException(message, ex); 122 } 123 124 fullText = readFile(file, decoder); 125 126 // Use the BufferedReader to break down the lines as this 127 // is about 30% faster than using the 128 // LINE_TERMINATOR.split(fullText, -1) method 129 try (BufferedReader reader = new BufferedReader(new StringReader(fullText))) { 130 final ArrayList<String> textLines = new ArrayList<>(); 131 while (true) { 132 final String line = reader.readLine(); 133 if (line == null) { 134 break; 135 } 136 textLines.add(line); 137 } 138 lines = textLines.toArray(CommonUtil.EMPTY_STRING_ARRAY); 139 } 140 } 141 142 /** 143 * Copy constructor. 144 * @param fileText to make copy of 145 */ 146 public FileText(FileText fileText) { 147 file = fileText.file; 148 charset = fileText.charset; 149 fullText = fileText.fullText; 150 lines = fileText.lines.clone(); 151 if (fileText.lineBreaks == null) { 152 lineBreaks = null; 153 } 154 else { 155 lineBreaks = fileText.lineBreaks.clone(); 156 } 157 } 158 159 /** 160 * Compatibility constructor. 161 * 162 * <p>This constructor reconstructs the text of the file by joining 163 * lines with linefeed characters. This process does not restore 164 * the original line terminators and should therefore be avoided. 165 * 166 * @param file the name of the file 167 * @param lines the lines of the text, without terminators 168 * @throws NullPointerException if the lines array is null 169 */ 170 public FileText(File file, List<String> lines) { 171 final StringBuilder buf = new StringBuilder(1024); 172 for (final String line : lines) { 173 buf.append(line).append('\n'); 174 } 175 176 this.file = file; 177 charset = null; 178 fullText = buf.toString(); 179 this.lines = lines.toArray(CommonUtil.EMPTY_STRING_ARRAY); 180 } 181 182 /** 183 * Reads file using specific decoder and returns all its content as a String. 184 * @param inputFile File to read 185 * @param decoder Charset decoder 186 * @return File's text 187 * @throws IOException Unable to open or read the file 188 */ 189 private static String readFile(final File inputFile, final CharsetDecoder decoder) 190 throws IOException { 191 if (!inputFile.exists()) { 192 throw new FileNotFoundException(inputFile.getPath() + " (No such file or directory)"); 193 } 194 final StringBuilder buf = new StringBuilder(1024); 195 final InputStream stream = Files.newInputStream(inputFile.toPath()); 196 try (Reader reader = new InputStreamReader(stream, decoder)) { 197 final char[] chars = new char[READ_BUFFER_SIZE]; 198 while (true) { 199 final int len = reader.read(chars); 200 if (len == -1) { 201 break; 202 } 203 buf.append(chars, 0, len); 204 } 205 } 206 return buf.toString(); 207 } 208 209 /** 210 * Get the name of the file. 211 * @return an object containing the name of the file 212 */ 213 public File getFile() { 214 return file; 215 } 216 217 /** 218 * Get the character set which was used to read the file. 219 * Will be {@code null} for a file reconstructed from its lines. 220 * @return the charset used when the file was read 221 */ 222 public Charset getCharset() { 223 return charset; 224 } 225 226 /** 227 * Retrieve the full text of the file. 228 * @return the full text of the file 229 */ 230 public CharSequence getFullText() { 231 return fullText; 232 } 233 234 /** 235 * Returns an array of all lines. 236 * {@code text.toLinesArray()} is equivalent to 237 * {@code text.toArray(new String[text.size()])}. 238 * @return an array of all lines of the text 239 */ 240 public String[] toLinesArray() { 241 return lines.clone(); 242 } 243 244 /** 245 * Find positions of line breaks in the full text. 246 * @return an array giving the first positions of each line. 247 */ 248 private int[] findLineBreaks() { 249 if (lineBreaks == null) { 250 final int[] lineBreakPositions = new int[size() + 1]; 251 lineBreakPositions[0] = 0; 252 int lineNo = 1; 253 final Matcher matcher = LINE_TERMINATOR.matcher(fullText); 254 while (matcher.find()) { 255 lineBreakPositions[lineNo] = matcher.end(); 256 lineNo++; 257 } 258 if (lineNo < lineBreakPositions.length) { 259 lineBreakPositions[lineNo] = fullText.length(); 260 } 261 lineBreaks = lineBreakPositions; 262 } 263 return lineBreaks; 264 } 265 266 /** 267 * Determine line and column numbers in full text. 268 * @param pos the character position in the full text 269 * @return the line and column numbers of this character 270 */ 271 public LineColumn lineColumn(int pos) { 272 final int[] lineBreakPositions = findLineBreaks(); 273 int lineNo = Arrays.binarySearch(lineBreakPositions, pos); 274 if (lineNo < 0) { 275 // we have: lineNo = -(insertion point) - 1 276 // we want: lineNo = (insertion point) - 1 277 lineNo = -lineNo - 2; 278 } 279 final int startOfLine = lineBreakPositions[lineNo]; 280 final int columnNo = pos - startOfLine; 281 // now we have lineNo and columnNo, both starting at zero. 282 return new LineColumn(lineNo + 1, columnNo); 283 } 284 285 /** 286 * Retrieves a line of the text by its number. 287 * The returned line will not contain a trailing terminator. 288 * @param lineNo the number of the line to get, starting at zero 289 * @return the line with the given number 290 */ 291 public String get(final int lineNo) { 292 return lines[lineNo]; 293 } 294 295 /** 296 * Counts the lines of the text. 297 * @return the number of lines in the text 298 */ 299 public int size() { 300 return lines.length; 301 } 302 303}