View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.general.Parametrized;
4   import net.sf.tomp.xtcl.Command;
5   import net.sf.tomp.xtcl.Context;
6   
7   import java.io.PrintStream;
8   
9   import java.util.Iterator;
10  import java.util.List;
11  import java.util.Map;
12  
13  /***
14   * DOCUMENT ME!
15   * 
16   * @author tomp
17   */
18  public class AbstractCommand implements Command {
19      /***
20       * DOCUMENT ME!
21       * 
22       * @param c DOCUMENT ME!
23       * @return DOCUMENT ME!
24       * @throws Exception DOCUMENT ME!
25       */
26      public int execute(Context c) throws Exception {
27          return done(c, 0);
28      }
29  
30      public int done(Context c, int i) throws Exception {
31          PrintStream out = c.getOut();
32  
33          if ((out != null) && c.isVerbose()) {
34              if (i == 0) {
35                  out.print("OK " + this);
36              } else {
37                  out.print("ERR " + i + " " + this);
38              }
39          }
40  
41          return i;
42      }
43  
44      /***
45       * DOCUMENT ME!
46       * 
47       * @param params DOCUMENT ME!
48       * @param realParams DOCUMENT ME!
49       * @param f DOCUMENT ME!
50       * @param context DOCUMENT ME!
51       * @return DOCUMENT ME!
52       */
53      protected int initParameters(Map params, Map realParams, Parametrized f,
54              Context context) {
55          for (Iterator i = params.keySet().iterator(); i.hasNext();) {
56              String k = (String) i.next();
57              Object v = params.get(k);
58  
59              if (v instanceof String && ((String) v).startsWith("$")) {
60                  v = context.get(v);
61  
62                  if ((v == null) && context.isStrictPassing()) {
63                      return 1;
64                  }
65              }
66  
67              if (f != null) {
68                  f.setParameter(k, v);
69              }
70  
71              realParams.put(k, v);
72          }
73  
74          return 0;
75      }
76  
77      /***
78       * DOCUMENT ME!
79       * 
80       * @param os DOCUMENT ME!
81       * @return DOCUMENT ME!
82       */
83      public static String listArray(Object[] os) {
84          StringBuffer sb = new StringBuffer();
85  
86          for (int i = 0; i < os.length; i++) {
87              if (i > 0) {
88                  sb.append(", ");
89              }
90  
91              sb.append(os[i]);
92          }
93  
94          return sb.toString();
95      }
96  
97      /***
98       * DOCUMENT ME!
99       * 
100      * @param m DOCUMENT ME!
101      * @return DOCUMENT ME!
102      */
103     public static String listList(List m) {
104         StringBuffer sb = new StringBuffer();
105 
106         for (Iterator i = m.iterator(); i.hasNext();) {
107             sb.append(i.next());
108 
109             if (i.hasNext()) {
110                 sb.append(", ");
111             }
112         }
113 
114         return sb.toString();
115     }
116 
117     /***
118      * DOCUMENT ME!
119      * 
120      * @param m DOCUMENT ME!
121      * @return DOCUMENT ME!
122      */
123     public static String listMap(Map m) {
124         StringBuffer sb = new StringBuffer();
125 
126         for (Iterator i = m.entrySet().iterator(); i.hasNext();) {
127             Map.Entry e = (Map.Entry) i.next();
128             Object k = e.getKey();
129             Object v = e.getValue();
130 
131             sb.append(k + "=" + v);
132 
133             if (i.hasNext()) {
134                 sb.append(", ");
135             }
136         }
137 
138         return sb.toString();
139     }
140 }
141 
142 /*
143  * The contents of this file are subject to the Mozilla Public License Version
144  * 1.1 (the "License"); you may not use this file except in compliance with the
145  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
146  * Software distributed under the License is distributed on an "AS IS" basis,
147  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
148  * the specific language governing rights and limitations under the License. The
149  * Original Code is: all this file. The Initial Developer of the Original Code
150  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
151  */