001: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
002: // Released under the terms of the GNU General Public License version 2 or later.
003:
004: package eg;
005:
006: import fit.*;
007: import java.io.*;
008: import java.util.*;
009:
010: public class AllFiles extends Fixture {
011:
012: public void doRow(Parse row) {
013: Parse cell = row.leaf();
014: List files = expand(cell.text());
015: if (files.size() > 0) {
016: doRow(row, files);
017: } else {
018: ignore(cell);
019: info(cell, " no match");
020: }
021: }
022:
023: protected List expand(String pattern) {
024: StringTokenizer tokens = new StringTokenizer(pattern,
025: File.separator);
026: List files = new ArrayList();
027: expand(new File("."), tokens, files);
028: return files;
029: }
030:
031: protected void expand(File path, StringTokenizer tokens, List result) {
032: if (tokens.hasMoreTokens()) {
033: File files[] = path.listFiles(new WildCard(tokens
034: .nextToken()));
035: for (int i = 0; i < files.length; i++) {
036: expand(files[i], tokens, result);
037: }
038: } else {
039: result.add(path);
040: }
041: }
042:
043: protected void doRow(Parse row, List files) {
044: doFiles(row, files);
045: }
046:
047: protected void doFiles(Parse row, List files) {
048: for (Iterator i = files.iterator(); i.hasNext();) {
049: File path = (File) i.next();
050: Parse cells = td(path.getName(), td("", null));
051: row = (row.more = tr(cells, row.more));
052: Fixture fixture = new Fixture();
053: run(path, fixture, cells);
054: summarize(fixture, path);
055: }
056: }
057:
058: public static int runCount = 0;
059:
060: protected void run(File path, Fixture fixture, Parse cells) {
061: if (pushAndCheck(path)) {
062: ignore(cells);
063: info(cells, "recursive");
064: return;
065: }
066: try {
067: String input = read(path);
068: Parse tables;
069: if (input.indexOf("<wiki>") >= 0) {
070: tables = new Parse(input, new String[] { "wiki",
071: "table", "tr", "td" });
072: fixture.doTables(tables.parts);
073: } else {
074: tables = new Parse(input, new String[] { "table", "tr",
075: "td" });
076: fixture.doTables(tables);
077: }
078:
079: info(cells.more, fixture.counts.toString());
080: if (fixture.counts.wrong == 0
081: && fixture.counts.exceptions == 0) {
082: right(cells.more);
083: } else {
084: wrong(cells.more);
085: cells.more.addToBody(tables.footnote());
086: }
087: } catch (Exception e) {
088: exception(cells, e);
089: }
090: pop(path);
091: }
092:
093: public static List fileStack = new ArrayList();
094:
095: protected boolean pushAndCheck(File path) {
096: String name = path.getAbsolutePath();
097: if (fileStack.contains(name)) {
098: return true;
099: }
100: fileStack.add(name);
101: return false;
102: }
103:
104: protected void pop(File path) {
105: fileStack.remove(path.getAbsolutePath());
106: }
107:
108: private void summarize(Fixture fixture, File path) {
109: fixture.summary.put("input file", path.getAbsolutePath());
110: fixture.summary.put("input update", new Date(path
111: .lastModified()));
112: Counts runCounts = summary.containsKey("counts run") ? (Counts) summary
113: .get("counts run")
114: : new Counts();
115: runCounts.tally(fixture.counts);
116: summary.put("counts run", runCounts);
117: }
118:
119: protected String read(File input) throws IOException {
120: char chars[] = new char[(int) (input.length())];
121: FileReader in = new FileReader(input);
122: in.read(chars);
123: in.close();
124: return new String(chars);
125: }
126:
127: Parse tr(Parse cells, Parse more) {
128: return new Parse("tr", null, cells, more);
129: }
130:
131: Parse td(String text, Parse more) {
132: return new Parse("td", info(text), null, more);
133: }
134:
135: class WildCard implements FilenameFilter {
136:
137: String prefix;
138: String sufix;
139: int minimum;
140:
141: WildCard(String pattern) {
142: int star = pattern.indexOf("*");
143: if (star >= 0) {
144: prefix = pattern.substring(0, star);
145: sufix = pattern.substring(star + 1);
146: minimum = prefix.length() + sufix.length();
147: } else {
148: prefix = pattern;
149: sufix = null;
150: minimum = prefix.length();
151: }
152: }
153:
154: public boolean accept(File dir, String name) {
155: return !(name.startsWith("."))
156: && name.length() >= minimum
157: && name.startsWith(prefix)
158: && (sufix == null ? name.length() == minimum : name
159: .endsWith(sufix));
160: }
161: }
162:
163: // Self Test ////////////////////////////////
164:
165: public static class Expand extends ColumnFixture {
166:
167: public String path;
168: AllFiles fixture = new AllFiles();
169:
170: public String[] expansion() {
171: List files = fixture.expand(path);
172: String[] result = new String[files.size()];
173: for (int i = 0; i < result.length; i++) {
174: result[i] = ((File) files.get(i)).getName();
175: }
176: return result;
177: }
178: }
179:
180: }
|