📄 fileparser.java
字号:
* <p>
* This method exists to keep the file format opaque to parent objects.
*
* @throws InvalidMetaFileException If a syntax error is found.
*/
public String extractString()
throws InvalidMetaFileException
{
return extractString( '\"', '\"', true );
}
/**
* Extracts a sample from the data file. The method
* expects that the data file has one sample per line,
* which consists of a comma-delimited list of
* attributes. A line may or not be terminated by an
* additional comma.
*
* @return A Vector of Strings from the comma
* delimited sample, in the order they appear
* on the line in the file, or null if the
* end of the file has been reached.
*
* @throws InvalidDataFileException If a syntax error
* is found.
*
* @throws IOException If an IO error occurs while
* reading a line from the file.
*/
public Vector extractDataSample()
throws InvalidDataFileException,
IOException
{
do {
m_fileText = m_lineReader.readLine();
if( m_fileText == null ) return null;
}
while( isEmptyLine( m_fileText ) );
int pos;
Vector attribVals = new Vector();
// Parse the line.
while( (pos = m_fileText.indexOf( ',' )) > -1 ) {
if( pos == 0 )
throw new
InvalidDataFileException( "Syntax error " +
"in data file (line " + m_lineReader.getLineNumber() + "): " +
"Null attribute before comma." );
attribVals.add( m_fileText.substring( 0, pos ).trim() );
// Strip off the value we just grabbed, and
// the trailing comma.
if( (pos + 1) == m_fileText.length() )
m_fileText = new String();
else
m_fileText = m_fileText.substring( pos + 1 );
}
// There may or may not be something left on the
// line after parsing the last comma - if there
// is, add it to our values vector.
if( !isEmptyLine( m_fileText ) )
attribVals.add( m_fileText.trim() );
return attribVals;
}
/**
* Returns the line number for the line that was
* last read from the current file.
*
* <p>
* <b>Note:</b> This method is only useful for
* data files.
*
* @return the current line number, or -1 if no file
* is open.
*/
public int getCurrentLineNum()
{
if( m_lineReader == null ) return -1;
return m_lineReader.getLineNumber();
}
// Private methods
/**
* Extracts and returns a string from the current
* file text, using the supplied characters as delimiters
* (starting from the beginning of the text). The string
* and the delimiters are removed from the file text.
*
* @param startDelim Delimiter character that marks the
* start of the string.
*
* @param stopDelim Delimiter character that marks the
* end of the string.
*
* @param trim Determines if whitespace is trimmed from
* the beginning and the end of the extracted string.
* Set to true to remove whitespace, or false
* otherwise.
*
* @throws InvalidMetaFileException If there
* is a problem extracting a String contained
* within the specified delimiters.
*/
private
String extractString( char startDelim, char stopDelim, boolean trim )
throws InvalidMetaFileException
{
int d1pos, d2pos; // delimiter positions
d1pos = m_fileText.indexOf( startDelim );
if( d1pos < 0 )
throw new
InvalidMetaFileException( "Syntax error in " +
"configuration file: Missing opening " +
"delimiter character " + startDelim + "." );
else if( m_fileText.length() == (d1pos+1) )
throw new
InvalidMetaFileException( "Syntax error in " +
"configuration file: Empty after first " +
"delimiter character." );
d2pos = m_fileText.indexOf( stopDelim, d1pos + 1 );
if( d2pos < 0 )
throw new
InvalidMetaFileException( "Syntax error in " +
"configuration file: Missing closing " +
"delimiter character." );
if( (d1pos+1) == d2pos )
// The string is empty.
throw new
InvalidMetaFileException( "Syntax error in " +
"configuration file: String between delimiters is empty." );
// Extract the attribute name from the file text.
String extractedString = m_fileText.substring( d1pos+1, d2pos );
if( (d2pos+1) < m_fileText.length() )
m_fileText = m_fileText.substring( d2pos+1 );
else
// There was nothing after the last delimiter.
m_fileText = new String( "" );
if( trim ) return extractedString.trim();
return extractedString;
}
/**
* Strips the specified string, and anything before
* it, out of the file text. The method also searches
* for and removes the '=' sign immediately following
* the string.
*
* @throws InvalidMetaFileException If the string
* is not located in the file text, or there
* is no trailing '=' sign.
*/
private void stripString( String stringToStrip )
throws InvalidMetaFileException
{
// Determine where in the file buffer the string
// is located, and strip it, and anything before it, out.
int pos;
if( (pos = m_fileText.indexOf( stringToStrip )) == -1 )
throw new
InvalidMetaFileException( "Syntax error in " +
"configuration file: Missing '" +
stringToStrip + "' keyword(s)." );
try {
m_fileText = m_fileText.substring( pos + stringToStrip.length() );
}
catch( StringIndexOutOfBoundsException e ) {
throw new
InvalidMetaFileException( "Syntax error in " +
"configuration file: Empty after '" +
stringToStrip + "' keyword(s)." );
}
if( !stripCharacter( '=' ) )
throw new
InvalidMetaFileException( "Syntax error in " +
"configuration file: No '=' sign after '" +
stringToStrip + "' keyword(s)." );
}
/**
* Starts at the beginning of the current file text
* and finds the first non-whitespace character. If
* the character matches, the method removes
* the sign and any whitespace before and after it.
* Otherwise, the method leaves the file text untouched.
*
* @param charToStrip Character to remove.
*
* @return true if the character was found and chopped,
* false otherwise.
*/
private boolean stripCharacter( char charToStrip )
{
int pos = 0;
boolean foundChar = false;
while( pos < m_fileText.length() ) {
char currChar = m_fileText.charAt( pos );
if( currChar == ' ' || currChar == '\r' ||
currChar == '\t' || currChar == '\n' )
pos++;
else if( currChar == charToStrip ) {
foundChar = true;
break;
}
else
break;
}
if( foundChar ) {
pos++;
while( pos < m_fileText.length() ) {
char currChar = m_fileText.charAt( pos );
if( currChar == ' ' || currChar == '\r' ||
currChar == '\t' || currChar == '\n' )
pos++;
else
break;
}
// Chop the file text.
if( pos < m_fileText.length() )
m_fileText = m_fileText.substring( pos );
else
// The only thing after the character
// was whitespace.
m_fileText = new String( "" );
}
return foundChar;
}
/**
* Returns true if the supplied string only
* contains whitespace (tab and space) characters,
* or is empty.
*/
private boolean isEmptyLine( String line )
{
if( line.length() == 0 ) return true;
int pos = 0;
boolean isEmpty = true;
while( pos < line.length() ) {
char currChar = m_fileText.charAt( pos );
if( currChar == ' ' || currChar == '\r' ||
currChar == '\t' || currChar == '\n' )
pos++;
else {
isEmpty = false;
break;
}
}
return isEmpty;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -