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.io.PrintStream;
10  
11  import java.util.HashMap;
12  import java.util.Iterator;
13  import java.util.Map;
14  
15  /***
16   * DOCUMENT ME!
17   * 
18   * @author tomp
19   */
20  public class FilterCommand extends VarCommand {
21      /*** DOCUMENT ME! */
22      protected String className;
23  
24      /*** DOCUMENT ME! */
25      protected Map params = new HashMap();
26  
27      /*** DOCUMENT ME! */
28      protected Map realParams = new HashMap();
29  
30      /***
31       * DOCUMENT ME!
32       * 
33       * @param k DOCUMENT ME!
34       * @param v DOCUMENT ME!
35       */
36      public void setParameter(String k, Object v) {
37          params.put(k, v);
38  
39          //System.out.println("Setting param "+k+"="+v);
40      }
41  
42      /***
43       * DOCUMENT ME!
44       * 
45       * @param r DOCUMENT ME!
46       */
47      public void setClassName(String r) {
48          className = r;
49      }
50  
51      /***
52       * DOCUMENT ME!
53       * 
54       * @param c DOCUMENT ME!
55       * @return DOCUMENT ME!
56       * @throws Exception DOCUMENT ME!
57       */
58      public int execute(Context c) throws Exception {
59          PrintStream out = c.getOut();
60          XTFilterFactory factory = c.getXTFilterFactory();
61          int retCode = 0;
62  
63          try {
64              XMLFilter filter = factory.newXMLFilter(className);
65  
66              if (filter instanceof XTFilter) {
67                  retCode = initFilter((XTFilter) filter, c);
68  
69                  // FIXME
70                  listMap(realParams);
71              }
72  
73              c.put(var, filter);
74          } catch (Exception e) {
75              e.printStackTrace();
76              retCode = 1;
77          }
78  
79          if (out != null) {
80              out.print(((retCode <= 0) ? "OK " : "ERR ") + this);
81          }
82  
83          return retCode;
84      }
85  
86      /***
87       * DOCUMENT ME!
88       * 
89       * @return DOCUMENT ME!
90       */
91      public String toString() {
92          return "FILTER " + var + "=" + className + " (" + listMap(params) + ")";
93      }
94  
95      /***
96       * DOCUMENT ME!
97       * 
98       * @param f DOCUMENT ME!
99       * @param context DOCUMENT ME!
100      * @return DOCUMENT ME!
101      */
102     protected int initFilter(XTFilter f, Context context) {
103         for (Iterator i = params.keySet().iterator(); i.hasNext();) {
104             String k = (String) i.next();
105             Object v = params.get(k);
106 
107             if (v instanceof String && ((String) v).startsWith("$")) {
108                 v = context.get(v);
109 
110                 if ((v == null) && context.isStrictPassing()) {
111                     return 1;
112                 }
113             }
114 
115             f.setParameter(k, v);
116             realParams.put(k, v);
117         }
118 
119         return 0;
120     }
121 }
122 
123 /*
124  * The contents of this file are subject to the Mozilla Public License Version
125  * 1.1 (the "License"); you may not use this file except in compliance with the
126  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
127  * Software distributed under the License is distributed on an "AS IS" basis,
128  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
129  * the specific language governing rights and limitations under the License. The
130  * Original Code is: all this file. The Initial Developer of the Original Code
131  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
132  */