package edu.princeton.toy; import java.io.*; import java.util.Vector; import java.util.Iterator; /** * TExampleManager is a class that reads the example manifest and loads all of the examples into * memory, parsing their titles as well. * * @author btsang * @version 7.1 */ public class TExampleManager implements Runnable { /** * The filename of the example manifest. */ public static final String EXAMPLE_MANIFEST_FILE = "examples.mf"; private static final TExampleManager INSTANCE = new TExampleManager(); private int exampleCount; private Vector exampleTitles; private Vector exampleText; private Thread runner; /** * Constructs a TExampleManager which launches another thread to load the examples in the * background. */ private TExampleManager() { exampleCount = 0; exampleTitles = new Vector(); exampleText = new Vector(); runner = new Thread(this); runner.start(); } /** * Indicates wheter or not each example in the manifest either does not exist or has been loaded * and processed. * * @return true iff all the example programs have either been accounted for. */ public static boolean isReady() { return INSTANCE.runner == null; } /** * Returns the number of examples which have been sucessfully loaded. * * @return The number of examples available. */ public static int getExampleCount() { if (INSTANCE.runner != null) throw new RuntimeException(); return INSTANCE.exampleCount; } /** * Returns the title of the requested example. * * @param index The index of the requested title. An ArrayIndexOutOfBounds exception will be * thrown if the index is invalid. * @return The title of the requested example. */ public static String getExampleTitle(int index) { return (String)INSTANCE.exampleTitles.get(index); } /** * Returns the program text of the requested example. * * @param index The index of the requested title. An ArrayIndexOutOfBounds exception will be * thrown if the index is invalid. * @return The text of the requested example. */ public static String getExampleText(int index) { return (String)INSTANCE.exampleText.get(index); } /** * Implement the Runnable interface to load the example files in a separate thread. */ public void run() { try { Vector examplePaths = new Vector(); // Load the data from the manifest try { BufferedReader reader = TResourceLoader.getBufferedReader(EXAMPLE_MANIFEST_FILE); String string = reader.readLine(); while(string != null) { string = string.trim(); if (string.length() > 0) examplePaths.add(string); string = reader.readLine(); } } catch (Exception e) { e.printStackTrace(); } // Load the example programs Iterator iterator = examplePaths.iterator(); char array[] = new char[100]; StringBuffer buffer = new StringBuffer(); TProgramDocument program = new TProgramDocument(""); while (iterator.hasNext()) { try { Reader reader = TResourceLoader.getReader((String)iterator.next()); int readChars = reader.read(array, 0, 100); buffer.delete(0, buffer.length()); // Load the characters into a string buffer 100 at a time while(readChars != -1) { buffer.append(array, 0, readChars); readChars = reader.read(array, 0, 100); } // Use a TProgramDocument to correct any extended characters in the text and // parse the title program.setText(buffer.toString()); exampleText.add(program.getText()); exampleTitles.add(program.getTitle()); exampleCount++; } catch (Exception e) { e.printStackTrace(); } } } finally { runner = null; } } }