1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.api;
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  import java.io.Reader;
29  import java.io.StringReader;
30  import java.nio.charset.Charset;
31  import java.nio.charset.CharsetDecoder;
32  import java.nio.charset.CodingErrorAction;
33  import java.nio.charset.UnsupportedCharsetException;
34  import java.nio.file.Files;
35  import java.util.ArrayList;
36  import java.util.Arrays;
37  import java.util.List;
38  import java.util.regex.Matcher;
39  import java.util.regex.Pattern;
40  
41  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
42  
43  /**
44   * Represents the text contents of a file of arbitrary plain text type.
45   * <p>
46   * This class will be passed to instances of class FileSetCheck by
47   * Checker.
48   * </p>
49   *
50   */
51  public final class FileText {
52  
53      /**
54       * The number of characters to read in one go.
55       */
56      private static final int READ_BUFFER_SIZE = 1024;
57  
58      /**
59       * Regular expression pattern matching all line terminators.
60       */
61      private static final Pattern LINE_TERMINATOR = Pattern.compile("\\n|\\r\\n?");
62  
63      // For now, we always keep both full text and lines array.
64      // In the long run, however, the one passed at initialization might be
65      // enough, while the other could be lazily created when requested.
66      // This would save memory but cost CPU cycles.
67  
68      /**
69       * The name of the file.
70       * {@code null} if no file name is available for whatever reason.
71       */
72      private final File file;
73  
74      /**
75       * The charset used to read the file.
76       * {@code null} if the file was reconstructed from a list of lines.
77       */
78      private final Charset charset;
79  
80      /**
81       * The full text contents of the file.
82       */
83      private final String fullText;
84  
85      /**
86       * The lines of the file, without terminators.
87       */
88      private final String[] lines;
89  
90      /**
91       * The first position of each line within the full text.
92       */
93      private int[] lineBreaks;
94  
95      /**
96       * Creates a new file text representation.
97       *
98       * <p>The file will be read using the specified encoding, replacing
99       * 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 }