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.io.PrintStream;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 /***
17 * DOCUMENT ME!
18 *
19 * @author tomp
20 */
21 public class SAXSourceCommand extends VarCommand {
22 /*** DOCUMENT ME! */
23 protected String className;
24
25 /*** DOCUMENT ME! */
26 protected String inputSourceRef;
27
28 /*** DOCUMENT ME! */
29 protected Map params = new HashMap();
30
31 /*** DOCUMENT ME! */
32 protected Map realParams = new HashMap();
33
34 /***
35 * DOCUMENT ME!
36 *
37 * @param r DOCUMENT ME!
38 */
39 public void setClassName(String r) {
40 className = r;
41 }
42
43 /***
44 * DOCUMENT ME!
45 *
46 * @param r DOCUMENT ME!
47 */
48 public void setInputSource(String r) {
49 inputSourceRef = r;
50 }
51
52 /***
53 * DOCUMENT ME!
54 *
55 * @param k DOCUMENT ME!
56 * @param v DOCUMENT ME!
57 */
58 public void setParameter(String k, Object v) {
59 params.put(k, v);
60
61
62 }
63
64 /***
65 * DOCUMENT ME!
66 *
67 * @param context DOCUMENT ME!
68 * @return DOCUMENT ME!
69 * @throws Exception DOCUMENT ME!
70 */
71 public int execute(Context context) throws Exception {
72 PrintStream out = context.getOut();
73 XTSourceFactory factory = context.getXTSourceFactory();
74 int retCode = 0;
75
76 try {
77 InputSource is = inputSourceRef.startsWith("$") ? context
78 .refToInputSource(inputSourceRef) : new InputSource(context
79 .getFile(inputSourceRef).getCanonicalPath());
80 SAXSource xr = factory.newSAXSource(className, is);
81
82 if (xr instanceof Parametrized) {
83 retCode = initParameters(params, realParams, (Parametrized) xr,
84 context);
85 }
86
87 context.put(var, xr);
88 } catch (Exception e) {
89 e.printStackTrace();
90 retCode = 1;
91 }
92
93 return done(context, retCode);
94 }
95
96 /***
97 * DOCUMENT ME!
98 *
99 * @return DOCUMENT ME!
100 */
101 public String toString() {
102 return "SAXSOURCE " + var + "=" + className + " reading from '"
103 + inputSourceRef + "' (" + listMap(params) + ")";
104 }
105 }
106
107
108
109
110
111
112
113
114
115
116