View Javadoc

1   /*
2    * Created on 24.8.2004
3    *
4    */
5   package net.sf.tomp.xtcl.command;
6   
7   import java.io.FileInputStream;
8   import java.util.HashSet;
9   import java.util.Enumeration;
10  import java.util.Iterator;
11  import java.util.Properties;
12  import java.util.Set;
13  
14  import net.sf.tomp.xtcl.Context;
15  import net.sf.tomp.xtcl.ParametrizedCommand;
16  
17  /***
18   * The base for parametrized VarCommands.
19   *   
20   * @author tomp
21   *
22   */
23  public abstract class AbstractParametrizedVarCommand extends VarCommand
24          implements ParametrizedCommand {
25  
26      /*** set of Properties references */
27      private Set propsReferences = new HashSet();
28  
29      /*** 
30       * Adds a reference to Properties that will be used as parameters.
31       * 
32       * The reference may refer to a Properties object in the context
33       * (if the ref starts with a '$')
34       * or otherwise a filename.
35       * 
36       */
37      public void addParameterPropertiesReference(String ref) {
38  //        System.out.println("addParameterPropertiesReference in "+this+" ref="+ref);
39          propsReferences.add(ref); 
40      }
41  
42      /***
43       * @see net.sf.tomp.general.Parametrized#setParameter(java.lang.String, java.lang.Object)
44       */
45      public abstract void setParameter(String k, Object v);
46  
47      /***
48       * Resolves all the properties found in the Set of propsReferences within
49       * the Context and sets it via setParameter(k, v).
50       * 
51       * @param context
52       * @throws Exception
53       */
54      protected void propertiesToParameters(Context context) throws Exception {
55      	
56          for (Iterator i = propsReferences.iterator(); i.hasNext();) {
57              String propRef = (String) i.next();
58              Properties p = null;
59  //            System.out.println("in "+this+" propRef="+propRef);
60              if (propRef.startsWith("$")) {
61                  // ref to Properties object
62                  p = context.refToProperties(propRef);
63              } else {
64                  // filename of the Properties file,
65                  // resolve within context
66                  p = new Properties();
67                  p.load(new FileInputStream(context.getFile(propRef)));
68              }
69              // have Properties, insert all its content via setParameter
70              for (Enumeration e = p.propertyNames(); e.hasMoreElements();) {
71                  String key = (String) e.nextElement();
72                  setParameter(key, p.getProperty(key));
73              }
74          }
75      }
76  }