View Javadoc

1   package fit;
2   
3   // Copyright (c) 2002 Cunningham & Cunningham, Inc.
4   // Released under the terms of the GNU General Public License version 2 or later.
5   
6   public class ColumnFixture extends Fixture {
7   
8       protected TypeAdapter columnBindings[];
9       protected boolean hasExecuted = false;
10  
11      // Traversal ////////////////////////////////
12  
13      public void doRows(Parse rows) {
14          bind(rows.parts);
15          super.doRows(rows.more);
16      }
17  
18      public void doRow(Parse row) {
19          hasExecuted = false;
20          try {
21              reset();
22              super.doRow(row);
23              if (!hasExecuted) {
24                  execute();
25              }
26          } catch (Exception e) {
27              exception (row.leaf(), e);
28          }
29      }
30  
31      public void doCell(Parse cell, int column) {
32          TypeAdapter a = columnBindings[column];
33          try {
34              String text = cell.text();
35              if (text.equals("")) {
36                  check(cell, a);
37              } else if (a == null) {
38                  ignore(cell);
39              } else if (a.field != null) {
40                  a.set(a.parse(text));
41              } else if (a.method != null) {
42                  check(cell, a);
43              }
44          } catch(Exception e) {
45              exception(cell, e);
46          }
47      }
48  
49      public void check(Parse cell, TypeAdapter a) {
50          if (!hasExecuted) {
51              try {
52                  execute();
53              } catch (Exception e) {
54                  exception (cell, e);
55              }
56              hasExecuted = true;
57          }
58          super.check(cell, a);
59      }
60  
61      public void reset() throws Exception {
62          // about to process first cell of row
63      }
64  
65      public void execute() throws Exception {
66          // about to process first method call of row
67      }
68  
69      // Utility //////////////////////////////////
70  
71      protected void bind (Parse heads) {
72          columnBindings = new TypeAdapter[heads.size()];
73          for (int i=0; heads!=null; i++, heads=heads.more) {
74              String name = heads.text();
75              String suffix = "()";
76              try {
77                  if (name.equals("")) {
78                      columnBindings[i] = null;
79                  } else if (name.endsWith(suffix)) {
80                      columnBindings[i] = bindMethod(name.substring(0,name.length()-suffix.length()));
81                  } else {
82                      columnBindings[i] = bindField(name);
83                  }
84              }
85              catch (Exception e) {
86                  exception (heads, e);
87              }
88          }
89  
90      }
91  
92      protected TypeAdapter bindMethod (String name) throws Exception {
93          return TypeAdapter.on(this, getTargetClass().getMethod(camel(name), new Class[]{}));
94      }
95  
96      protected TypeAdapter bindField (String name) throws Exception {
97          return TypeAdapter.on(this, getTargetClass().getField(camel(name)));
98      }
99  
100     protected Class getTargetClass() {
101         return getClass();
102     }
103 }