1 package net.sf.tomp.xtcl.command;
2
3 import net.sf.tomp.general.Parametrized;
4 import net.sf.tomp.xtcl.Context;
5 import net.sf.tomp.xtcl.source.XTSourceFactory;
6
7 import org.xml.sax.InputSource;
8
9 import javax.xml.transform.sax.SAXSource;
10
11 import java.util.HashMap;
12 import java.util.Map;
13
14 /***
15 * Creates SAXSource or XTSAXSource by class name.
16 *
17 * @author tomp
18 */
19 public class SAXSourceCommand extends AbstractParametrizedVarCommand {
20
21 /*** SAXSource or XTSAXSource class name */
22 protected String className;
23
24 /*** reference to create InputSource */
25 protected String inputSourceRef;
26
27 /*** formal parameters */
28 protected Map params = new HashMap();
29
30 /*** real parameters */
31 protected Map realParams = new HashMap();
32
33 /***
34 * set (XT)SAXSource class name
35 *
36 * @param r (XT)SAXSource class name
37 */
38 public void setClassName(String r) {
39 className = r;
40 }
41
42 /***
43 * set InputSource reference
44 *
45 * @param r InputSource reference
46 */
47 public void setInputSource(String r) {
48 inputSourceRef = r;
49 }
50
51 /***
52 * set parameter
53 *
54 * @param k name
55 * @param v value
56 */
57 public void setParameter(String k, Object v) {
58 params.put(k, v);
59
60
61 }
62
63 /***
64 * Creates SAXSource or XTSAXSource by class name.
65 */
66 public int execute(Context context) throws Exception {
67 int retCode = 0;
68
69
70 propertiesToParameters(context);
71
72 XTSourceFactory factory = context.getXTSourceFactory();
73 try {
74 InputSource is = inputSourceRef.startsWith("$") ? context
75 .refToInputSource(inputSourceRef) : new InputSource(context
76 .getFile(inputSourceRef).getCanonicalPath());
77 SAXSource xr = factory.newSAXSource(className, is);
78
79 if (xr instanceof Parametrized) {
80 retCode = initParameters(params, realParams, (Parametrized) xr,
81 context);
82 }
83
84 context.put(var, xr);
85 } catch (Exception e) {
86 e.printStackTrace();
87 retCode = 1;
88 }
89
90 return done(context, retCode);
91 }
92
93 /***
94 * @return "SAXSOURCE " + var + "=" + className + " reading from '"
95 + inputSourceRef + "' (" + listMap(params) + ")";
96 */
97 public String toString() {
98 return "SAXSOURCE " + var + "=" + className + " reading from '"
99 + inputSourceRef + "' (" + listMap(params) + ")";
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112