/** @author Scott Marshall @author University of Glasgow, MSc IT Project 2001 @author Building an OnLine Course in Computing Fundamentals Multiple Choice Questions */ package ukacgla_HTMLMulti; import ukacgla_HTMLMulti.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; import java.net.*; /** TestItem holds ALL the the lines for a single question. TestItem is a single test, made of one question and a number of answers. Answers are built from reading a txt file located at the answerURL location. Each option to be displayed is a separate object, a TestLine object. This class reads the txt file passed in as answerURL, and constructs TestLine objects. TestLine objects are then held in the Vector answerList. */ public class TestItem { /** Holds the TestLines that make this TestItem. */ private Vector answerList; /** URL of Question HTML file to be displayed. */ private URL questionURL; /** URL of each option to be displayed. */ private URL answerURL; /** URL of any images to be displayed. */ private URL iconURL; /** URL of any follow up HTML files to be displayed. */ private URL followOnURL; /** If the TestLine is specified as containing an image, it will be created as this icon. */ private ImageIcon icon; /** Reads input txt file. Constructs a TestLine object from information read from file. Places TestLine objects into a vector, then randomises the order of the vector */ public TestItem(URL questionURL, URL answerURL) { this.questionURL = questionURL; this.answerURL = answerURL; answerList = new Vector(); try { //open a stream to read the answer file InputStream inStream = answerURL.openStream(); BufferedReader dataStream = new BufferedReader(new InputStreamReader(inStream)); String inLine = null; while ((inLine = dataStream.readLine())!=null){ //use a StringTokenizer to read all the info held in inLine StringTokenizer t = new StringTokenizer(inLine,"$*"); //use this to make URL of the follow up information while (t.hasMoreTokens()){ //test if there is signal for an icon if (t.nextToken().equals("Icon")) { //Form the URL of any image icons try { iconURL = new URL(answerURL,new String(t.nextToken())); } catch (MalformedURLException e) { System.out.println("Invalid iconURL read from file : " + iconURL); } icon = new ImageIcon(iconURL); } else { String chaff = t.nextToken(); //skips next token icon = null; //records no icon with this file } //get HTML fragment from inLine String HTMLtext = t.nextToken(); //use this to make URL of the follow up information try { followOnURL = new URL(answerURL,new String(t.nextToken())); } catch (MalformedURLException e) { System.out.println("Invalid followOnURL read from file : " + followOnURL); } //then read if answer is correct or not int correct = Integer.parseInt(t.nextToken()); TestLine temp = new TestLine(icon, HTMLtext, followOnURL, correct); answerList.add(temp); }//ends hasMoreTokens }//ends while TestRandomiser random = new TestRandomiser(answerList.size()); answerList = random.randomise(answerList); }//ends try catch(IOException e){ } }//ends ctor /** Returns questionURL for this TestItem. */ protected URL getQuestionURL(){ return questionURL; } /** Returns number of TestLines in current TestItem */ protected int getNumLines(){ return answerList.size(); } /** Returns a specific TestLine held in the answerList vector */ protected TestLine getLine(int n){ return (TestLine) answerList.get(n); } }//ends class