KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > format > template > DelegateContext


1 /*****************************************
2                                      *
3   JBoss Portal: The OpenSource Portal *
4                                      *
5    Distributable under LGPL license. *
6    See terms of license at gnu.org. *
7                                      *
8 **************************************/

9
10 package org.jboss.portal.format.template;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16
17 /**
18  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
19  * @version $Revision: 1.1.1.1 $
20  */

21 public class DelegateContext implements Context
22 {
23    /** context's children */
24    private ChildrenStrategy children;
25
26    /** template name/value pairs for substitution */
27    private Map JavaDoc delegate;
28
29    /**
30     * creates a new DelegateContext object.
31     */

32    public DelegateContext()
33    {
34       this(new HashMap JavaDoc(), new HashMap JavaDoc());
35    }
36
37    /**
38     * creates a new DelegateContext object.
39     *
40     * @param delegate name/value pairs for "root" data
41     * @param children name/value pairs for nested or loop data
42     */

43    public DelegateContext(Map JavaDoc delegate, Map JavaDoc children)
44    {
45       this.delegate = delegate;
46       this.children = new ChildrenStrategy(children);
47    }
48
49    /**
50     * create a new "root" data context and name/value pairs to be used as
51     * nested or loop data
52     *
53     * @param children name/value pairs for nested or loop data
54     *
55     * @return context to continue adding template data too
56     */

57    public static final DelegateContext createWithChildren(Map JavaDoc children)
58    {
59       return new DelegateContext(new HashMap JavaDoc(), children);
60    }
61
62    /**
63     * create a new data context with already existing name/value pairs
64     *
65     * @param values existing name/value pair map
66     *
67     * @return context to continue adding template data too
68     */

69    public static final DelegateContext createWithValues(Map JavaDoc values)
70    {
71       return new DelegateContext(values, new HashMap JavaDoc());
72    }
73
74    /**
75     * add an existing data context into this context for use in template loops
76     * or nested template data.
77     *
78     * @param name variable prefix name
79     * @param ctx context to add
80     */

81    public void append(String JavaDoc name, Context ctx)
82    {
83       children.append(name, ctx);
84    }
85
86    /**
87     * @see org.jboss.portal.format.template.Context#childIterator(java.lang.String)
88     */

89    public Iterator JavaDoc childIterator(String JavaDoc name)
90    {
91       return children.childIterator(name);
92    }
93
94    /**
95     * @see org.jboss.portal.format.template.Context#get(java.lang.String)
96     */

97    public String JavaDoc get(String JavaDoc key)
98    {
99       return (String JavaDoc) delegate.get(key);
100    }
101
102    /**
103     * create a new object to place data for use in template loops or nested
104     * template data. tpl var format:
105     * { prefix.VAR_NAME
106     * }
107     *
108     * @param name variable prefix name
109     *
110     * @return delegate context that will contain the loop or nested data
111     */

112    public DelegateContext next(String JavaDoc name)
113    {
114       DelegateContext ctx = new DelegateContext();
115       append(name, ctx);
116
117       return ctx;
118    }
119
120    /**
121     * @see org.jboss.portal.format.template.Context#put(java.lang.String,
122     * java.lang.String)
123     */

124    public Context put(String JavaDoc key, String JavaDoc value)
125    {
126       delegate.put(key, value);
127
128       return this;
129    }
130 }
131
Popular Tags