View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.xtcl.Context;
4   import net.sf.tomp.xtcl.filter.XTFilter;
5   import net.sf.tomp.xtcl.filter.XTFilterFactory;
6   
7   import org.xml.sax.XMLFilter;
8   
9   import java.util.HashMap;
10  import java.util.Iterator;
11  import java.util.Map;
12  
13  /***
14   * Command for loading and initializing a XMLFilter (XTFilter)
15   * 
16   * @author tomp
17   */
18  public class FilterCommand extends AbstractParametrizedVarCommand {
19  	
20      /*** XTCL's XTFilter or SAX's XMLFilter class name */
21      protected String className;
22  
23      /*** formal params */
24      protected Map params = new HashMap();
25  
26      /*** real params */
27      protected Map realParams = new HashMap();
28  
29      /***
30       * sets a parameter
31       * 
32       * @param k param name
33       * @param v param value
34       */
35      public void setParameter(String k, Object v) {
36          params.put(k, v);
37  
38      }
39  
40      /***
41       * sets the filter class name 
42       * 
43       * @param r filter class name
44       */
45      public void setClassName(String r) {
46          className = r;
47      }
48  
49      /***
50       * Loads and initializes (parametrizes) the filter.
51       * Only the filter implementing the XTFilter is parametrized.
52       * 
53       * @param context 
54       * @return 0 if OK, otherwise 1
55       * @throws Exception 
56       */
57      public int execute(Context context) throws Exception {
58          // transfer props to params
59          propertiesToParameters(context);
60  
61          XTFilterFactory factory = context.getXTFilterFactory();
62          int retCode = 0;
63  
64          try {
65              XMLFilter filter = factory.newXMLFilter(className);
66  
67              if (filter instanceof XTFilter) {
68                  retCode = initFilter((XTFilter) filter, context);
69  
70                  // FIXME
71  //                listMap(realParams);
72              }
73  
74              context.put(var, filter);
75          } catch (Exception e) {
76              e.printStackTrace();
77              retCode = 1;
78          }
79  
80          return done(context, retCode);
81      }
82  
83      /***
84       * @return "FILTER " + var + "=" + className + " (" + listMap(params) + ")";
85       */
86      public String toString() {
87          return "FILTER " + var + "=" + className + " (" + listMap(params) + ")";
88      }
89  
90      /***
91       * Initializes the filter:
92       * Evaluates the parameters, gets the real params and fetches 
93       * them to the XTFilter. 
94       * 
95       * @param f the filter to be initialized
96       * @param context Context to evaluate the params
97       * @return 1 if strict parameter passing fails, otherwise 0
98       */
99      protected int initFilter(XTFilter f, Context context) {
100         for (Iterator i = params.keySet().iterator(); i.hasNext();) {
101             String k = (String) i.next();
102             Object v = params.get(k);
103 
104             if (v instanceof String && ((String) v).startsWith("$")) {
105                 v = context.get(v);
106 
107                 if ((v == null) && context.isStrictPassing()) {
108                     return 1;
109                 }
110             }
111 
112             f.setParameter(k, v);
113             realParams.put(k, v);
114         }
115 
116         return 0;
117     }
118 }
119 
120 /*
121  * The contents of this file are subject to the Mozilla Public License Version
122  * 1.1 (the "License"); you may not use this file except in compliance with the
123  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
124  * Software distributed under the License is distributed on an "AS IS" basis,
125  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
126  * the specific language governing rights and limitations under the License. The
127  * Original Code is: all this file. The Initial Developer of the Original Code
128  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
129  */