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