Class FileText
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<java.lang.String>
-
- com.puppycrawl.tools.checkstyle.api.FileText
-
- All Implemented Interfaces:
java.lang.Iterable<java.lang.String>
,java.util.Collection<java.lang.String>
,java.util.List<java.lang.String>
public final class FileText extends java.util.AbstractList<java.lang.String>
Represents the text contents of a file of arbitrary plain text type.This class will be passed to instances of class FileSetCheck by Checker. It implements a string list to ensure backwards compatibility, but can be extended in the future to allow more flexible, more powerful or more efficient handling of certain situations.
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.charset.Charset
charset
The charset used to read the file.private java.io.File
file
The name of the file.private java.lang.String
fullText
The full text contents of the file.private static java.util.regex.Pattern
LINE_TERMINATOR
Regular expression pattern matching all line terminators.private int[]
lineBreaks
The first position of each line within the full text.private java.lang.String[]
lines
The lines of the file, without terminators.private static int
READ_BUFFER_SIZE
The number of characters to read in one go.
-
Constructor Summary
Constructors Modifier Constructor Description FileText(FileText fileText)
Copy constructor.FileText(java.io.File file, java.lang.String charsetName)
Creates a new file text representation.private
FileText(java.io.File file, java.util.List<java.lang.String> lines)
Compatibility constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private int[]
findLineBreaks()
Find positions of line breaks in the full text.static FileText
fromLines(java.io.File file, java.util.List<java.lang.String> lines)
Compatibility conversion.java.lang.String
get(int lineNo)
Retrieves a line of the text by its number.java.nio.charset.Charset
getCharset()
Get the character set which was used to read the file.java.io.File
getFile()
Get the name of the file.java.lang.CharSequence
getFullText()
Retrieve the full text of the file.LineColumn
lineColumn(int pos)
Determine line and column numbers in full text.private static java.lang.String
readFile(java.io.File inputFile, java.nio.charset.CharsetDecoder decoder)
Reads file using specific decoder and returns all its content as a String.int
size()
Counts the lines of the text.java.lang.String[]
toLinesArray()
Returns an array of all lines.-
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
-
-
-
-
Field Detail
-
READ_BUFFER_SIZE
private static final int READ_BUFFER_SIZE
The number of characters to read in one go.- See Also:
- Constant Field Values
-
LINE_TERMINATOR
private static final java.util.regex.Pattern LINE_TERMINATOR
Regular expression pattern matching all line terminators.
-
file
private final java.io.File file
The name of the file.null
if no file name is available for whatever reason.
-
charset
private final java.nio.charset.Charset charset
The charset used to read the file.null
if the file was reconstructed from a list of lines.
-
fullText
private final java.lang.String fullText
The full text contents of the file.
-
lines
private final java.lang.String[] lines
The lines of the file, without terminators.
-
lineBreaks
private int[] lineBreaks
The first position of each line within the full text.
-
-
Constructor Detail
-
FileText
public FileText(java.io.File file, java.lang.String charsetName) throws java.io.IOException
Creates a new file text representation.The file will be read using the specified encoding, replacing malformed input and unmappable characters with the default replacement character.
- Parameters:
file
- the name of the filecharsetName
- the encoding to use when reading the file- Throws:
java.lang.NullPointerException
- if the text is nulljava.io.IOException
- if the file could not be read
-
FileText
public FileText(FileText fileText)
Copy constructor.- Parameters:
fileText
- to make copy of
-
FileText
private FileText(java.io.File file, java.util.List<java.lang.String> lines)
Compatibility constructor.This constructor reconstructs the text of the file by joining lines with linefeed characters. This process does not restore the original line terminators and should therefore be avoided.
- Parameters:
file
- the name of the filelines
- the lines of the text, without terminators- Throws:
java.lang.NullPointerException
- if the lines array is null
-
-
Method Detail
-
readFile
private static java.lang.String readFile(java.io.File inputFile, java.nio.charset.CharsetDecoder decoder) throws java.io.IOException
Reads file using specific decoder and returns all its content as a String.- Parameters:
inputFile
- File to readdecoder
- Charset decoder- Returns:
- File's text
- Throws:
java.io.IOException
- Unable to open or read the file
-
fromLines
public static FileText fromLines(java.io.File file, java.util.List<java.lang.String> lines)
Compatibility conversion.This method can be used to convert the arguments passed to
FileSetCheck.process(File,List)
to a FileText object. If the list of lines already is a FileText, it is returned as is. Otherwise, a new FileText is constructed by joining the lines using line feed characters.- Parameters:
file
- the name of the filelines
- the lines of the text, without terminators- Returns:
- an object representing the denoted text file
-
getFile
public java.io.File getFile()
Get the name of the file.- Returns:
- an object containing the name of the file
-
getCharset
public java.nio.charset.Charset getCharset()
Get the character set which was used to read the file. Will benull
for a file reconstructed from its lines.- Returns:
- the charset used when the file was read
-
getFullText
public java.lang.CharSequence getFullText()
Retrieve the full text of the file.- Returns:
- the full text of the file
-
toLinesArray
public java.lang.String[] toLinesArray()
Returns an array of all lines.text.toLinesArray()
is equivalent totext.toArray(new String[text.size()])
.- Returns:
- an array of all lines of the text
-
findLineBreaks
private int[] findLineBreaks()
Find positions of line breaks in the full text.- Returns:
- an array giving the first positions of each line.
-
lineColumn
public LineColumn lineColumn(int pos)
Determine line and column numbers in full text.- Parameters:
pos
- the character position in the full text- Returns:
- the line and column numbers of this character
-
get
public java.lang.String get(int lineNo)
Retrieves a line of the text by its number. The returned line will not contain a trailing terminator.- Specified by:
get
in interfacejava.util.List<java.lang.String>
- Specified by:
get
in classjava.util.AbstractList<java.lang.String>
- Parameters:
lineNo
- the number of the line to get, starting at zero- Returns:
- the line with the given number
-
size
public int size()
Counts the lines of the text.- Specified by:
size
in interfacejava.util.Collection<java.lang.String>
- Specified by:
size
in interfacejava.util.List<java.lang.String>
- Specified by:
size
in classjava.util.AbstractCollection<java.lang.String>
- Returns:
- the number of lines in the text
-
-