Package org.plumelib.util
Class EntryReader
java.lang.Object
java.io.Reader
java.io.BufferedReader
java.io.LineNumberReader
org.plumelib.util.EntryReader
Class that reads records or "entries" from a file. In the simplest case, entries can be lines. It
supports:
- include files,
- comments, and
- multi-line entries (paragraphs).
The syntax of each of these is customizable.
Example use:
// EntryReader constructor args are: filename, comment regexp, include regexp
try (EntryReader er = new EntryReader(filename, "^#.*", null)) {
for (String line : er) {
...
}
} catch (IOException e) {
System.err.println("Problem reading " + filename + ": " + e.getMessage());
}
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Descriptor for an entry (record, paragraph, etc.). -
Field Summary
Modifier and TypeFieldDescription@MonotonicNonNull @Regex(1) Pattern
Regular expression that starts a long entry.@MonotonicNonNull Pattern
SeeentryStartRegex
. -
Constructor Summary
ConstructorDescriptionEntryReader
(@MustCallAlias InputStream in) Create an EntryReader that does not support comments or include directives.EntryReader
(@MustCallAlias InputStream in, String filename) Create an EntryReader that uses the default character set and does not support comments or include directives.EntryReader
(@MustCallAlias InputStream in, String filename, @Nullable @Regex String commentRegexString, @Nullable @Regex(1) String includeRegexString) Create an EntryReader.EntryReader
(@MustCallAlias InputStream in, String charsetName, String filename) Create an EntryReader that does not support comments or include directives.EntryReader
(@MustCallAlias InputStream in, String charsetName, String filename, @Nullable @Regex String commentRegexString, @Nullable @Regex(1) String includeRegexString) Create an EntryReader that uses the given character set.EntryReader
(@MustCallAlias Reader reader) Create an EntryReader that does not support comments or include directives.EntryReader
(@MustCallAlias Reader reader, String filename, @Nullable @Regex String commentRegexString, @Nullable @Regex(1) String includeRegexString) Create an EntryReader.EntryReader
(File file) Create an EntryReader that does not support comments or include directives.EntryReader
(File file, @Nullable @Regex String commentRegex, @Nullable @Regex(1) String includeRegex) Create an EntryReader.EntryReader
(File file, String charsetName) Create an EntryReader that does not support comments or include directives.EntryReader
(String filename) Create an EntryReader that does not support comments or include directives.EntryReader
(String filename, @Nullable @Regex String commentRegex, @Nullable @Regex(1) String includeRegex) Create a new EntryReader starting with the specified file.EntryReader
(String filename, String charsetName) Create an EntryReader that does not support comments or include directives.EntryReader
(Path path) Create an EntryReader that does not support comments or include directives.EntryReader
(Path path, @Nullable @Regex String commentRegex, @Nullable @Regex(1) String includeRegex) Create an EntryReader.EntryReader
(Path path, String charsetName) Create an EntryReader that does not support comments or include directives. -
Method Summary
Modifier and TypeMethodDescription@Nullable EntryReader.Entry
getEntry()
Returns the next entry (paragraph) in the file.Returns the current filename.@org.checkerframework.checker.index.qual.NonNegative int
Returns the current line number in the current file.boolean
hasNext()
Returns whether or not there is another line to read.iterator()
Returns a line-by-line iterator for this file.static void
Simple usage example.void
mark
(int readAheadLimit) next()
Returns the next line in the multi-file.void
Puts the specified line back in the input.@org.checkerframework.checker.index.qual.GTENegativeOne int
read()
@org.checkerframework.checker.index.qual.IndexOrLow({"#1"}) int
read
(char[] cbuf, int off, int len) @Nullable String
readLine()
Read a line, ignoring comments and processing includes.void
remove()
remove() is not supported.void
reset()
void
setEntryStartStop
(@Regex(1) String entryStartRegex, @Regex String entryStopRegex) Set the regular expressions for the start and stop of long entries (multiple lines that are read as a group by getEntry()).void
setEntryStartStop
(@Regex(1) Pattern entryStartRegex, Pattern entryStopRegex) Set the regular expressions for the start and stop of long entries (multiple lines that are read as a group by getEntry()).void
setLineNumber
(@org.checkerframework.checker.index.qual.NonNegative int lineNumber) Set the current line number in the current file.@org.checkerframework.checker.index.qual.NonNegative long
skip
(long n) Methods inherited from class java.io.BufferedReader
close, lines, markSupported, ready
Methods inherited from class java.io.Reader
nullReader, read, read, transferTo
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface java.util.Iterator
forEachRemaining
-
Field Details
-
entryStartRegex
Regular expression that starts a long entry.If the first line of an entry matches this regexp, then the entry is terminated by:
entryStopRegex
, another line that matchesentryStartRegex
(even not following a newline), or the end of the current file.Otherwise, the first line of an entry does NOT match this regexp (or the regexp is null), in which case the entry is terminated by a blank line or the end of the current file.
-
entryStopRegex
SeeentryStartRegex
.- See Also:
-
-
Constructor Details
-
EntryReader
@MustCallAlias public EntryReader(@MustCallAlias @MustCallAlias InputStream in, String charsetName, String filename, @Nullable @Regex String commentRegexString, @Nullable @Regex(1) String includeRegexString) throws UnsupportedEncodingException Create an EntryReader that uses the given character set.- Parameters:
in
- source from which to read entriescharsetName
- the character set to usefilename
- non-null file name for stream being readcommentRegexString
- regular expression that matches comments. Any text that matches commentRegex is removed. A line that is entirely a comment is ignored.includeRegexString
- regular expression that matches include directives. The expression should define one group that contains the include file name.- Throws:
UnsupportedEncodingException
- if the charset encoding is not supported- See Also:
-
EntryReader
@MustCallAlias public EntryReader(@MustCallAlias @MustCallAlias InputStream in, String charsetName, String filename) throws UnsupportedEncodingException Create an EntryReader that does not support comments or include directives.- Parameters:
in
- the InputStreamcharsetName
- the character set to usefilename
- the file name- Throws:
UnsupportedEncodingException
- if the charset encoding is not supported- See Also:
-
EntryReader
@MustCallAlias public EntryReader(@MustCallAlias @MustCallAlias InputStream in, String filename, @Nullable @Regex String commentRegexString, @Nullable @Regex(1) String includeRegexString) Create an EntryReader.- Parameters:
in
- source from which to read entriesfilename
- non-null file name for stream being readcommentRegexString
- regular expression that matches comments. Any text that matches commentRegex is removed. A line that is entirely a comment is ignored.includeRegexString
- regular expression that matches include directives. The expression should define one group that contains the include file name.
-
EntryReader
Create an EntryReader that uses the default character set and does not support comments or include directives.- Parameters:
in
- the InputStreamfilename
- the file name- See Also:
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
in
- the InputStream- See Also:
-
EntryReader
@MustCallAlias public EntryReader(@MustCallAlias @MustCallAlias Reader reader, String filename, @Nullable @Regex String commentRegexString, @Nullable @Regex(1) String includeRegexString) Create an EntryReader.- Parameters:
reader
- source from which to read entriesfilename
- file name corresponding to reader, for use in error messagescommentRegexString
- regular expression that matches comments. Any text that matches commentRegex is removed. A line that is entirely a comment is ignoredincludeRegexString
- regular expression that matches include directives. The expression should define one group that contains the include file name
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
reader
- source from which to read entries- See Also:
-
EntryReader
public EntryReader(Path path, @Nullable @Regex String commentRegex, @Nullable @Regex(1) String includeRegex) throws IOException Create an EntryReader.- Parameters:
path
- initial file to readcommentRegex
- regular expression that matches comments. Any text that matches commentRegex is removed. A line that is entirely a comment is ignored.includeRegex
- regular expression that matches include directives. The expression should define one group that contains the include file name.- Throws:
IOException
- if there is a problem reading the file
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
path
- the file to read- Throws:
IOException
- if there is a problem reading the file- See Also:
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
path
- the file to readcharsetName
- the character set to use- Throws:
IOException
- if there is a problem reading the file- See Also:
-
EntryReader
public EntryReader(File file, @Nullable @Regex String commentRegex, @Nullable @Regex(1) String includeRegex) throws IOException Create an EntryReader.- Parameters:
file
- initial file to readcommentRegex
- regular expression that matches comments. Any text that matches commentRegex is removed. A line that is entirely a comment is ignored.includeRegex
- regular expression that matches include directives. The expression should define one group that contains the include file name.- Throws:
IOException
- if there is a problem reading the file
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
file
- the file to read- Throws:
IOException
- if there is a problem reading the file- See Also:
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
file
- the file to readcharsetName
- the character set to use- Throws:
IOException
- if there is a problem reading the file- See Also:
-
EntryReader
public EntryReader(String filename, @Nullable @Regex String commentRegex, @Nullable @Regex(1) String includeRegex) throws IOException Create a new EntryReader starting with the specified file.- Parameters:
filename
- initial file to readcommentRegex
- regular expression that matches comments. Any text that matchescommentRegex
is removed. A line that is entirely a comment is ignored.includeRegex
- regular expression that matches include directives. The expression should define one group that contains the include file name.- Throws:
IOException
- if there is a problem reading the file- See Also:
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
filename
- source from which to read entries- Throws:
IOException
- if there is a problem reading the file- See Also:
-
EntryReader
Create an EntryReader that does not support comments or include directives.- Parameters:
filename
- source from which to read entriescharsetName
- the character set to use- Throws:
IOException
- if there is a problem reading the file- See Also:
-
-
Method Details
-
readLine
Read a line, ignoring comments and processing includes. Note that a line that is completely a comment is completely ignored (and not returned as a blank line). Returns null at end of file.- Overrides:
readLine
in classLineNumberReader
- Returns:
- the string that was read, or null at end of file
- Throws:
IOException
-
iterator
Returns a line-by-line iterator for this file.Warning: This does not return a fresh iterator each time. The iterator is a singleton, the same one is returned each time, and a new one can never be created after it is exhausted.
-
hasNext
public boolean hasNext(@GuardSatisfied EntryReader this) Returns whether or not there is another line to read. Any IOExceptions are turned into errors (because the definition of hasNext() in Iterator doesn't throw any exceptions). -
next
Returns the next line in the multi-file.- Specified by:
next
in interfaceIterator<String>
- Returns:
- the next line in the multi-file
- Throws:
NoSuchElementException
- at end of file
-
remove
public void remove(@GuardSatisfied EntryReader this) remove() is not supported. -
getEntry
Returns the next entry (paragraph) in the file. Entries are separated by blank lines unless the entry started withentryStartRegex
(seesetEntryStartStop(java.lang.String, java.lang.String)
). If no more entries are available, returns null.- Returns:
- the next entry (paragraph) in the file
- Throws:
IOException
- if there is a problem reading the file
-
getFileName
Returns the current filename.- Returns:
- the current filename
-
getLineNumber
public @org.checkerframework.checker.index.qual.NonNegative int getLineNumber(@GuardSatisfied EntryReader this) Returns the current line number in the current file.- Overrides:
getLineNumber
in classLineNumberReader
- Returns:
- the current line number
-
setLineNumber
public void setLineNumber(@GuardSatisfied EntryReader this, @org.checkerframework.checker.index.qual.NonNegative int lineNumber) Set the current line number in the current file.- Overrides:
setLineNumber
in classLineNumberReader
- Parameters:
lineNumber
- new line number for the current file
-
setEntryStartStop
public void setEntryStartStop(@GuardSatisfied EntryReader this, @Regex(1) String entryStartRegex, @Regex String entryStopRegex) Set the regular expressions for the start and stop of long entries (multiple lines that are read as a group by getEntry()).- Parameters:
entryStartRegex
- regular expression that starts a long entryentryStopRegex
- regular expression that ends a long entry
-
setEntryStartStop
public void setEntryStartStop(@GuardSatisfied EntryReader this, @Regex(1) Pattern entryStartRegex, Pattern entryStopRegex) Set the regular expressions for the start and stop of long entries (multiple lines that are read as a group by getEntry()).- Parameters:
entryStartRegex
- regular expression that starts a long entryentryStopRegex
- regular expression that ends a long entry
-
putback
Puts the specified line back in the input. Only one line can be put back.- Parameters:
line
- the line to be put back in the input
-
mark
public void mark(@GuardSatisfied EntryReader this, int readAheadLimit) - Overrides:
mark
in classLineNumberReader
-
read
public @org.checkerframework.checker.index.qual.GTENegativeOne int read(@GuardSatisfied EntryReader this) - Overrides:
read
in classLineNumberReader
-
read
public @org.checkerframework.checker.index.qual.IndexOrLow({"#1"}) int read(@GuardSatisfied EntryReader this, char[] cbuf, int off, int len) - Overrides:
read
in classLineNumberReader
-
reset
public void reset(@GuardSatisfied EntryReader this) - Overrides:
reset
in classLineNumberReader
-
skip
public @org.checkerframework.checker.index.qual.NonNegative long skip(@GuardSatisfied EntryReader this, long n) - Overrides:
skip
in classLineNumberReader
-
main
Simple usage example.- Parameters:
args
- command-line arguments: filename [commentRegex [includeRegex]]- Throws:
IOException
- if there is a problem reading a file
-