1 package fit;
2
3
4
5
6
7 import java.io.*;
8 import java.util.*;
9
10 public class FileRunner
11 {
12
13 public String input;
14
15 public Parse tables;
16
17 public Fixture fixture = new Fixture();
18
19 public PrintWriter output;
20
21 public static void main(String argv[])
22 {
23 try
24 {
25 new FileRunner().run(argv);
26 }
27 catch (Exception e)
28 {
29
30
31 }
32 }
33
34 public void run(String argv[]) throws IOException
35 {
36 args(argv);
37 process();
38 exit();
39 }
40
41 public void process()
42 {
43 try
44 {
45 if (input.indexOf("<wiki>") >= 0)
46 {
47 tables = new Parse(input, new String[] {"wiki", "table", "tr", "td"});
48 fixture.doTables(tables.parts);
49 }
50 else
51 {
52 tables = new Parse(input, new String[] {"table", "tr", "td"});
53 fixture.doTables(tables);
54 }
55 }
56 catch (Exception e)
57 {
58 exception(e);
59 }
60 tables.print(output);
61 }
62
63 public void args(String[] argv) throws IOException
64 {
65 if (argv.length != 2)
66 {
67 System.err.println("usage: java fit.FileRunner input-file output-file");
68 System.exit(-1);
69 }
70 File in = new File(argv[0]);
71 File out = new File(argv[1]);
72 fixture.summary.put("input file", in.getAbsolutePath());
73 fixture.summary.put("input update", new Date(in.lastModified()));
74 fixture.summary.put("output file", out.getAbsolutePath());
75 input = read(in);
76 output = new PrintWriter(new BufferedWriter(new FileWriter(out)));
77 }
78
79 protected String read(File input) throws IOException
80 {
81 char chars[] = new char[(int) (input.length())];
82 FileReader in = new FileReader(input);
83 in.read(chars);
84 in.close();
85 return new String(chars);
86 }
87
88 protected void exception(Exception e)
89 {
90 tables = new Parse("body", "Unable to parse input. Input ignored.", null,
91 null);
92 fixture.exception(tables, e);
93 }
94
95 protected void exit()
96 {
97 output.close();
98 System.err.println(fixture.counts());
99 }
100
101 }