KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > context > 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.common.context;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * @author <a HREF="mailto:julien@jboss.org">Julien Viet </a>
18  * @version $Revision: 1.1.1.1 $
19  */

20 public class DelegateContext
21       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
41     * name/value pairs for "root" data
42     * @param children
43     * name/value pairs for nested or loop data
44     */

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

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

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

87    public void append(String JavaDoc name, Context ctx)
88    {
89       children.append(name, ctx);
90    }
91
92    /**
93     * @see org.jboss.portal.common.context.Context#childIterator(java.lang.String)
94     */

95    public Iterator JavaDoc childIterator(String JavaDoc name)
96    {
97       return children.childIterator(name);
98    }
99
100    /**
101     * @see org.jboss.portal.common.context.Context#get(java.lang.String)
102     */

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

117    public DelegateContext next(String JavaDoc name)
118    {
119       DelegateContext ctx = new DelegateContext();
120       append(name, ctx);
121
122       return ctx;
123    }
124
125    /**
126     * @see org.jboss.portal.common.context.Context#put(java.lang.String,
127     * java.lang.String)
128     */

129    public Context put(String JavaDoc key, String JavaDoc value)
130    {
131       delegate.put(key, value);
132
133       return this;
134    }
135
136    public Context put(String JavaDoc key, Integer JavaDoc value)
137    {
138       put(key, value.toString());
139       return this;
140    }
141 }
Popular Tags