1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.data;
20
21 import net.sf.jameleon.event.DataDrivableEventHandler;
22
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 /***
28 * Ties DataDriver and DataDrivable together.
29 */
30 public class DataExecuter{
31
32 protected DataDriver driver;
33
34 /***
35 * Default constructor. At the moment this does nothing.
36 */
37 public DataExecuter(){}
38
39 /***
40 * Uses the given <code>driver</code> to drive the DataDrivable.
41 * @param driver The drive used to drive the DataDrivable.
42 */
43 public DataExecuter(DataDriver driver){
44 this.driver = driver;
45 }
46
47 /***
48 * Gets the DataDriver used by this class.
49 * @return the DataDriver used by this class.
50 */
51 public DataDriver getDataDriver(){
52 return driver;
53 }
54
55 /***
56 * When it is time to make the class data-driven, this method should be called.
57 * @param drivable - The class to be data-driven, for example, a csv tag class
58 * @param noExecute - If set to true, then the driver's methods will not actually get called
59 * and the executeDrivableRow will get called with an empty data set.
60 * @throws IOException the datasource handle cannot be opened or the datasource
61 * cannot be read from.
62 */
63 public void executeData(DataDrivable drivable, boolean noExecute) throws IOException{
64 int row = 1;
65 if (noExecute) {
66 drivable.addVariablesToRowData(new HashMap());
67 drivable.executeDrivableRow(row);
68 drivable.destroyVariables(new HashMap().keySet());
69 }else{
70 DataDrivableEventHandler eventHandler = DataDrivableEventHandler.getInstance();
71 try{
72 eventHandler.openEvent(drivable);
73 driver.open();
74 Map vars;
75 while (driver.hasMoreRows()) {
76 vars = driver.getNextRow();
77 eventHandler.executeRowEvent(vars, drivable, row);
78 drivable.addVariablesToRowData(vars);
79 drivable.executeDrivableRow(row++);
80 drivable.destroyVariables(vars.keySet());
81 }
82 }finally{
83 eventHandler.closeEvent(drivable);
84 driver.close();
85 }
86 }
87 }
88
89 }