package exercise.writer;
/**
* This class is used to Parse a Text Question file into it's component
* parts that can be used to construct a Text Entry Module.
* @author Grant K. Olds
* @version
* @since JDK1.2
*/
import java.lang.String;
import java.util.StringTokenizer;
public class TextEntryParser extends QuestionEntryParser
{
private StringBuffer answer;
private StringBuffer feedback;
private String contents;
TextEntryParser(){}
TextEntryParser( String fileContents)
{
contents = fileContents;
file = new StringTokenizer( fileContents );
}
/**
* Returns answer
* @return answer the answer String null
string otherwise.
*/
public String getAnswer()
{
return answer.toString();
}
/**
* Returns feedback
* @return feedback the feedback string array null
string array otherwise.
*/
public String getFeedback()
{
return feedback.toString();
}
/**
* This function uses a string the StringTokenizer break each String
* into it's component parts and assign them to the appropriate
* variable.
* @parameter fileContents the contents of the file being read in.
*/
public void parseFile( String fileContents)
{
file = new StringTokenizer ( fileContents );
//Read all the Tokens
while( file.hasMoreTokens () )
{
String currToken = file.nextToken();
//if the token is equal to "Section"
if ( currToken.equals( "Section:" ) )
{
//assign all the tokens to the section string until you reach
//Exercise.
currToken = file.nextToken();
section = new StringBuffer();
while ( currToken.compareTo( "Exercise:") != 0 )
{
section.append ( currToken );
currToken = file.nextToken();
}
}
// assign all tokens to the exercise strint until you reach
// "Question:"
if ( currToken.equals( "Exercise:" ) )
{
exercise = new StringBuffer ();
currToken = file.nextToken();
while ( currToken.compareTo( "Question:") != 0 )
{
exercise.append ( currToken );
currToken = file.nextToken();
}
}
// Assign all tokens to the question string until you reach
// "Answer:"
if ( currToken.equals( "Question:" ) )
{
question = new StringBuffer ();
currToken = file.nextToken();
while ( currToken.compareTo( "Answer:" ) != 0 )
{
question.append ( currToken ).append(" ");
currToken = file.nextToken();
}
}
// Assign all tokens to the answer string until you reach
// "Feedback:"
if ( currToken.equals( "Answer:" ) )
{
answer = new StringBuffer();
currToken = file.nextToken();
while ( currToken.compareTo( "Feedback:" ) != 0 )
{
answer.append ( currToken ).append(" ");
currToken = file.nextToken();
}
}
// Assign all tokens tothe feedback string until you reach
// "Hint:"
if ( currToken.equals( "Feedback:" ) )
{
feedback = new StringBuffer();
currToken = file.nextToken();
while ( currToken.compareTo( "Hint:" ) != 0 )
{
feedback.append ( currToken ).append(" ");
currToken = file.nextToken();
}
}
if ( currToken.equals( "Hint:" ) )
{
hint = new StringBuffer();
currToken = file.nextToken();
while ( file.hasMoreTokens() )
{
hint.append ( currToken ).append(" ");
currToken = file.nextToken();
}
hint.append( currToken );
}
}
}
public void parseFile(){}
public void test()
{
System.out.println( "The Section is " + section );
System.out.println( "The Exercise is " + exercise );
System.out.println( "The Question is " + question );
System.out.println( "The Answer is " + answer );
System.out.println( "The Feedback is " + feedback );
System.out.println( "The Hint is " + hint );
}
}