edu.umd.cfar.lamp.viper.util.reader
Class VReader

java.lang.Object
  extended byedu.umd.cfar.lamp.viper.util.reader.VReader
All Implemented Interfaces:
ErrorWriter

public class VReader
extends java.lang.Object
implements ErrorWriter

This class offers a "Preprocessor" to ViPER data files, removing comments and expanding #includes. It also contains some knowledge of the gtf data format, so it should be checked for accuracy if the descriptor format is changed greatly.

Since:
Version 1.1. Before, this was called GTReader.
Author:
David Mihalcik, Charles Lin

Nested Class Summary
protected  class VReader.TextLine
          This class is used to represent a line of text or a sequence of lines connected by '\' characters.
 
Constructor Summary
VReader(java.util.List paths)
          Creates a new VReader that is block-type agnostic.
VReader(java.util.List paths, java.lang.String returnAs)
          Opens a new VReader looking for the specified block type.
VReader(java.lang.String pathName)
          Initializes a new VReader with the file specified.
VReader(java.lang.String directory, java.lang.String fileName)
          Initializes a new VReader with the file specified by directory and file name.
 
Method Summary
 java.lang.String advanceToBeginDirective()
          Advances to next begin directive; noop if current line is a begin directive.
 boolean advanceToBeginDirective(java.lang.String name)
          Advance to the first occurance of the begin directive for section name.
 java.lang.String advanceToNextRealLine()
          like gotoNextRealLine, but also returns the line.
 void changeSwitches(boolean all, boolean err, boolean warn, boolean bad, boolean totals)
          Change the error switches.
 void close()
          closes the input file
 boolean currentLineIsAttribute()
          Checks to see if the line at which the VReader is pointing contains well-formed Attribute data.
 boolean currentLineIsAttribute(boolean val)
          Checks to see if the line at which the VReader is pointing contains well-formed Attribute data.
 boolean currentLineIsBeginDirective()
          Checks to see if the current line contains a #BEGIN_ directive
 boolean currentLineIsDescriptor()
          Tests to see if the cursor is on a descriptor line
 boolean currentLineIsEndDirective()
          Checks to see if the current line contains an #END_ directive
 boolean fileHandleSet()
          Checks to see if any file handles remain
protected  void finalize()
           
 java.lang.String getCurrentLine()
          Get the current line as a String.
 int getCurrentLineNumber()
          Gets the current line number of the cursor.
 void gotoNextRealLine()
          Advances the cursor to the next line.
 boolean isAttributeLine(java.lang.String line, boolean printErr)
          Checks attribute by looking for colon, and making sure attribute name is not a descriptor type.
static boolean isBeginDirective(VReader.TextLine line)
          Checks to see if the specified line contains a #BEGIN_ directive
static boolean isDescriptorLine(VReader.TextLine line)
          Tests to see if the line starts with a descriptor type keyword, such as CONTENT or OBJECT.
static boolean isEndDirective(VReader.TextLine line)
          Checks to see if the specified line contains an #END_ directive
static boolean isKeyword(java.lang.String name)
          Returns true if this is one of the Descriptor keywords.
 void payAttentionTo(java.lang.String name)
          The block type to look for.
 void printError(java.lang.String message)
          Print an error.
 void printError(java.lang.String message, int start, int stop)
          Print an error at a specific place inside the current line.
 void printErrorAtLineNumber(java.lang.String message, int lineNum)
          Print an error at a a specific line number.
 void printErrorTotals()
          Print out the error count totals, usually in the form x Errors, y Warnings, z General Errors
 void printGeneralError(java.lang.String message)
          Prints an error that is unconnected to a line number.
 void printWarning(java.lang.String message)
          Print an warning.
 void printWarning(java.lang.String message, int start, int stop)
          Print an warning at a specific place inside the current line.
 void printWarningAtLineNumber(java.lang.String message, int lineNum)
          Print an warning at a a specific line number.
 void resetTotals()
          Reset the count of warning and errors.
 void setOutput(java.io.PrintStream ps)
          Sets the output stream.
 void setOutput(java.io.PrintWriter pw)
          Sets the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VReader

public VReader(java.lang.String pathName)
        throws java.io.IOException
Initializes a new VReader with the file specified.

Parameters:
pathName - the file to open
Throws:
java.io.IOException - if there is an error while opening/reading the file

VReader

public VReader(java.lang.String directory,
               java.lang.String fileName)
        throws java.io.IOException
Initializes a new VReader with the file specified by directory and file name.

Parameters:
directory - the directory name
fileName - the file name
Throws:
java.io.IOException

VReader

public VReader(java.util.List paths)
Creates a new VReader that is block-type agnostic.

Parameters:
paths - List of files to search in order (as if they were a single file)

VReader

public VReader(java.util.List paths,
               java.lang.String returnAs)
Opens a new VReader looking for the specified block type.

Parameters:
paths - List of files to search in order (as if they were a single file)
returnAs - block type to pay attention to, e.g. EVALUATION
Method Detail

setOutput

public void setOutput(java.io.PrintWriter pw)
Sets the output stream.

Specified by:
setOutput in interface ErrorWriter
Parameters:
pw - the new output stream

setOutput

public void setOutput(java.io.PrintStream ps)
Sets the output stream.

Parameters:
ps - the new output stream

close

public void close()
           throws java.io.IOException
closes the input file

Throws:
java.io.IOException

finalize

protected void finalize()
                 throws java.io.IOException
Throws:
java.io.IOException

changeSwitches

public void changeSwitches(boolean all,
                           boolean err,
                           boolean warn,
                           boolean bad,
                           boolean totals)
Change the error switches.

Parameters:
all - report all lines as they are parsed
err - report errors
warn - report warnings
bad - report the bad stuff
totals - print warning and error totals at the end

fileHandleSet

public boolean fileHandleSet()
Checks to see if any file handles remain

Returns:
true if more files remain

resetTotals

public void resetTotals()
Reset the count of warning and errors.


getCurrentLineNumber

public int getCurrentLineNumber()
Gets the current line number of the cursor.

Returns:
the cursor's line number

getCurrentLine

public java.lang.String getCurrentLine()
Get the current line as a String. It may contain new line characters, thanks to those slashes at the end of lines.

Returns:
the current line

advanceToNextRealLine

public java.lang.String advanceToNextRealLine()
                                       throws java.io.IOException,
                                              java.io.EOFException
like gotoNextRealLine, but also returns the line.

Returns:
the current line, as a string.
Throws:
java.io.IOException
java.io.EOFException

gotoNextRealLine

public void gotoNextRealLine()
                      throws java.io.IOException,
                             java.io.EOFException
Advances the cursor to the next line.

Throws:
java.io.IOException
java.io.EOFException

advanceToBeginDirective

public boolean advanceToBeginDirective(java.lang.String name)
Advance to the first occurance of the begin directive for section name. Does nothing if the current line is the requested begin directive. Therefore, to go to the third #BEGIN_ALPHA in a file, make sure to call gotoNextLine() after each call to this method.

Parameters:
name - Case-sensitive begin directive name - usually all-caps.
Returns:
true if the directive was found. Otherwise, the reader has advanced to the end of the file.

payAttentionTo

public void payAttentionTo(java.lang.String name)
The block type to look for.

Parameters:
name - the block to look for.

advanceToBeginDirective

public java.lang.String advanceToBeginDirective()
Advances to next begin directive; noop if current line is a begin directive.

Returns:
The section name that this begin directive marks, eg DATA or CONFIG If there is an IOException, this returns null

currentLineIsBeginDirective

public boolean currentLineIsBeginDirective()
Checks to see if the current line contains a #BEGIN_ directive

Returns:
if the line is a begin directive

isBeginDirective

public static boolean isBeginDirective(VReader.TextLine line)
Checks to see if the specified line contains a #BEGIN_ directive

Parameters:
line - the line to check
Returns:
if the line is a begin directive

currentLineIsEndDirective

public boolean currentLineIsEndDirective()
Checks to see if the current line contains an #END_ directive

Returns:
if the line is an end directive

isEndDirective

public static boolean isEndDirective(VReader.TextLine line)
Checks to see if the specified line contains an #END_ directive

Parameters:
line - the line to check
Returns:
if the line is an end directive

currentLineIsDescriptor

public boolean currentLineIsDescriptor()
Tests to see if the cursor is on a descriptor line

Returns:
true if the line looks like the start of a descriptor

isDescriptorLine

public static boolean isDescriptorLine(VReader.TextLine line)
Tests to see if the line starts with a descriptor type keyword, such as CONTENT or OBJECT.

Parameters:
line - the line to check
Returns:
true if the line starts with a descriptor keyword

currentLineIsAttribute

public boolean currentLineIsAttribute()
Checks to see if the line at which the VReader is pointing contains well-formed Attribute data.

Returns:
true if the current line looks like Attribute data.

currentLineIsAttribute

public boolean currentLineIsAttribute(boolean val)
Checks to see if the line at which the VReader is pointing contains well-formed Attribute data.

Parameters:
val - if you want to print errors if it is not an Attribute line.
Returns:
true if the current line looks like Attribute data.

isAttributeLine

public boolean isAttributeLine(java.lang.String line,
                               boolean printErr)
Checks attribute by looking for colon, and making sure attribute name is not a descriptor type.

Parameters:
line - The line to check
printErr - Set if you want to print errors if it is not an Attribute line.
Returns:
true if it looks like an attribute.

isKeyword

public static boolean isKeyword(java.lang.String name)
Returns true if this is one of the Descriptor keywords. (FILE, OBJECT, etc.) N.B. This is case sensitive.

Parameters:
name - A string to check against the list of keywords.
Returns:
true if name is a Descriptor keyword (category).

printError

public void printError(java.lang.String message)
Description copied from interface: ErrorWriter
Print an error. Errors are something that prevents the file from being understood usefully. This is used when the error refers to the whole line or the parser is unable to determine where the error occurred in the line.

Specified by:
printError in interface ErrorWriter
Parameters:
message - the message to print with the error
See Also:
ErrorWriter.printError(String)

printErrorAtLineNumber

public void printErrorAtLineNumber(java.lang.String message,
                                   int lineNum)
Description copied from interface: ErrorWriter
Print an error at a a specific line number. Errors are something that prevents the file from being understood usefully. This is used when the error refers to a line that has already been parsed, but the has only now been proven incorrect.

Specified by:
printErrorAtLineNumber in interface ErrorWriter
Parameters:
message - The message to print with the error.
lineNum - The line where the error occured.
See Also:
ErrorWriter.printErrorAtLineNumber(String, int)

printWarning

public void printWarning(java.lang.String message)
Description copied from interface: ErrorWriter
Print an warning. A warning may make the parser generate an improper representation or otherwise misinterpret the data, but not necessarily. This is used when the error refers to the whole line or the parser is unable to determine where the fault occurred in the line.

Specified by:
printWarning in interface ErrorWriter
Parameters:
message - the message to print with the warning
See Also:
ErrorWriter.printWarning(String)

printWarningAtLineNumber

public void printWarningAtLineNumber(java.lang.String message,
                                     int lineNum)
Description copied from interface: ErrorWriter
Print an warning at a a specific line number. A warning may make the parser generate an improper representation or otherwise misinterpret the data, but not necessarily. This is used when the error refers to a line that has already been parsed, but the has only now been discovered to be invalid.

Specified by:
printWarningAtLineNumber in interface ErrorWriter
Parameters:
message - the message to print with the warning
lineNum - The line where the error occured.
See Also:
ErrorWriter.printWarningAtLineNumber(String, int)

printError

public void printError(java.lang.String message,
                       int start,
                       int stop)
Description copied from interface: ErrorWriter
Print an error at a specific place inside the current line. Errors are something that prevents the file from being understood usefully.

Specified by:
printError in interface ErrorWriter
Parameters:
message - the message to print with the error
start - the column where the error begins
stop - the column where the error ends
See Also:
ErrorWriter.printError(String, int, int)

printWarning

public void printWarning(java.lang.String message,
                         int start,
                         int stop)
Description copied from interface: ErrorWriter
Print an warning at a specific place inside the current line. A warning may make the parser generate an improper representation or otherwise misinterpret the data, but not necessarily.

Specified by:
printWarning in interface ErrorWriter
Parameters:
message - the message to print with the warning
start - the column where the questionable part begins
stop - the column where the questionable part ends
See Also:
ErrorWriter.printWarning(String, int, int)

printGeneralError

public void printGeneralError(java.lang.String message)
Description copied from interface: ErrorWriter
Prints an error that is unconnected to a line number. For example, error opening file, I/O exception, etc

Specified by:
printGeneralError in interface ErrorWriter
Parameters:
message - the message to print with the warning
See Also:
ErrorWriter.printGeneralError(String)

printErrorTotals

public void printErrorTotals()
Description copied from interface: ErrorWriter
Print out the error count totals, usually in the form
  x Errors, y Warnings, z General Errors
 

Specified by:
printErrorTotals in interface ErrorWriter
See Also:
ErrorWriter.printErrorTotals()