View Javadoc

1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/11/25 17:58:58
6    */
7   package org.asyrinx.joey.gen.core;
8   
9   import java.util.Collection;
10  import java.util.HashSet;
11  import java.util.Iterator;
12  import java.util.Map;
13  import java.util.Set;
14  
15  import org.apache.velocity.context.Context;
16  import org.asyrinx.joey.gen.velocity.VelocityUtils;
17  
18  /***
19   * @author takeshi
20   */
21  public class VelocityHelper {
22  
23      /***
24       *  
25       */
26      public VelocityHelper(Context context) {
27          super();
28          this.context = context;
29      }
30  
31      private final Context context;
32  
33      public Object newInstance(String className) {
34          try {
35              return Class.forName(className).newInstance();
36          } catch (Exception e) {
37              return null;
38          }
39      }
40  
41      public Set toSet(Collection collection) {
42          final Set result = new HashSet();
43          for (Iterator i = collection.iterator(); i.hasNext();)
44              result.add(i.next());
45          return result;
46      }
47  
48      public Object put(String key, Object value) {
49          return context.put(key, value);
50      }
51  
52      public String debugContext() {
53          final StringBuffer result = new StringBuffer();
54          result.append("hashCode=").append(this.context.hashCode()).append("\n");
55          final Map map = VelocityUtils.toMap(this.context);
56          for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
57              final Map.Entry entry = (Map.Entry) i.next();
58              result.append(entry.getKey()).append("=").append(entry.getValue()).append("\n");
59          }
60          return result.toString();
61      }
62  
63  }