View Javadoc

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          //System.out.println("Setting param "+k+"="+v);
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  * The contents of this file are subject to the Mozilla Public License Version
109  * 1.1 (the "License"); you may not use this file except in compliance with the
110  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
111  * Software distributed under the License is distributed on an "AS IS" basis,
112  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
113  * the specific language governing rights and limitations under the License. The
114  * Original Code is: all this file. The Initial Developer of the Original Code
115  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
116  */