1 import java.io.IOException;
2 import java.io.BufferedReader;
3 import java.io.DataInputStream;
4 import java.io.FileInputStream;
5 import java.io.InputStreamReader;
6 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.StringTokenizer;
12 import java.util.TreeSet;
15 * Reads in a festival clunits unit catalog. The units are stored
16 * as a HashMap of ArrayLists where the HashMap is indexed by unit
17 * type (e.g., "ae_afternoon"), and the ArrayList is a list of the
18 * actual units themselves (e.g., "ae_afternoon_23").
20 * Assumption: unit indeces are in reverse order.
22 public class UnitCatalog extends HashMap {
23 /* Helpers for handling the previous and next units for each
26 private String currentFilename;
27 private Unit previousUnit;
28 private int currentIndex;
31 * Creates a new UnitCatalog from the festival unit catalog file.
33 * @param filename the festival unit catalog file
35 public UnitCatalog(String filename) throws IOException {
38 BufferedReader reader =
40 new InputStreamReader(
41 new FileInputStream(filename)));
43 /* Skip past the preamble.
45 String line = reader.readLine();
46 while (!line.equals("EST_Header_End")) {
47 line = reader.readLine();
50 /* Process each of the lines that have "real" content.
52 currentFilename = null;
55 line = reader.readLine();
56 while (line != null) {
58 line = reader.readLine();
65 * Processes a single line from the catalog.
67 void processLine(String line) {
68 StringTokenizer tokenizer = new StringTokenizer(line);
70 /* The whole unit, including its instance (e.g.,
73 String unitName = tokenizer.nextToken();
75 /* Split the unit into type ("ae_afternoon") and instance
78 String unitType = unitName.substring(0, unitName.lastIndexOf("_"));
79 int unitNum = Integer.parseInt(
80 unitName.substring(unitName.lastIndexOf("_") + 1));
82 /* Get the remaining parameters.
84 String filename = tokenizer.nextToken();
85 float start = Float.parseFloat(tokenizer.nextToken());
86 float middle = Float.parseFloat(tokenizer.nextToken());
87 float end = Float.parseFloat(tokenizer.nextToken());
89 /* Create a new ArrayList for this unit type if it
90 * is not in the catalog yet.
92 ArrayList units = (ArrayList) get(unitType);
94 units = new ArrayList();
99 * Make adjustments for previous and next units.
101 if (!filename.equals(currentFilename)) {
103 currentFilename = filename;
106 /* The units come in reverse order, so always
107 * add the unit to the begining of the entries.
109 Unit unit = new Unit(unitType,
120 /* Save away this unit as the next unit of the previous unit.
122 if (previousUnit != null) {
123 previousUnit.next = unit;
129 * Performs minimal checking on the catalog to make sure it was
132 void checkCatalog() {
133 /* Just make sure the unit numbers match the indexes in the
136 Iterator keys = keySet().iterator();
137 while (keys.hasNext()) {
138 String key = (String) keys.next();
139 ArrayList units = (ArrayList) get(key);
140 for (int i = 0; i < units.size(); i++) {
141 Unit unit = (Unit) units.get(i);
142 if (unit.unitNum != i) {
143 System.out.println(key + " check failed: expected "
144 + i + ", got " + unit.unitNum);
151 * Returns the given Unit or null if no such Unit exists.
153 * @param unitType the unit type (e.g., "ae_afternoon")
154 * @param unitNum the unit number
156 public Unit getUnit(String unitType, int unitNum) {
157 ArrayList units = (ArrayList) get(unitType);
158 if ((units == null) || (units.size() < unitNum)){
161 return (Unit) units.get(unitNum);
166 * For testing. args[0] = festival/clunits/*.catalogue.
168 public static void main(String[] args) {
170 UnitCatalog catalog = new UnitCatalog(args[0]);
171 catalog.checkCatalog();
172 } catch (IOException e) {