KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > ParameterMap


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 // java imports
21

22 import org.apache.beehive.netui.tags.AbstractClassicTag;
23 import org.apache.beehive.netui.util.Bundle;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.tagext.Tag JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Writes each in a map of URL parameters to a URL on its parent tag.
32  * The parent tag must implement IUrlParams.
33  * @jsptagref.tagdescription <p>Writes a group of name/value pairs to the URL or the parent tag.
34  *
35  * <p>The &lt;netui:parameterMap> can be nested inside of the
36  * {@link org.apache.beehive.netui.tags.html.Anchor},
37  * {@link org.apache.beehive.netui.tags.html.Button},
38  * {@link org.apache.beehive.netui.tags.html.Form}, and
39  * {@link org.apache.beehive.netui.tags.html.Image} tags.
40  *
41  * <p>You can dynamically determine the value of the &lt;netui:parameterMap> tag by pointing
42  * the <code>map</code> attribute at a {@link java.util.HashMap java.util.HashMap} object.
43  * @example Assume that there is a java.util.HashMap
44  * object in the Controller file.
45  *
46  * <pre> public HashMap hashMap = new HashMap();
47  * hashMap.put("q", "Socrates");
48  * hashMap.put("lr", "lang_el");
49  * hashMap.put("as_qdr", "m3");</pre>
50  *
51  * <p>The following set of tags will read the HashMap object and generate a
52  * link with a set of URL parameters.
53  *
54  * <pre> &lt;netui:anchor HREF="http://www.google.com/search">
55  * Search Greek language web sites updated in the last three months with the query "Socrates".
56  * &lt;netui:parameterMap map="${pageFlow.hashMap}"/>
57  * &lt;/netui:anchor></pre>
58  *
59  * <p>The URL produced appears as follows:
60  *
61  * <pre> http://www.google.com/search?lr=lang_el&q=Socrates&as_qdr=m3</pre>
62  * @netui:tag name="parameterMap" description="Uses a JSP 2.0 expression that points to a map of parameters. Each entry in the map provides a URL parameter that will be added to the parent tag's URL."
63  */

64 public class ParameterMap
65         extends AbstractClassicTag
66 {
67     private Map JavaDoc _map = null;
68
69     /**
70      * Return the name of the Tag.
71      */

72     public String JavaDoc getTagName()
73     {
74         return "ParameterMap";
75     }
76
77     /**
78      * Sets the map expression.
79      * @param map the map expression.
80      * @jsptagref.attributedescription A data binding expression pointing to a {@link java.util.Map java.util.Map} of parameters.
81      * The expression can point at any implementation of the java.util.Map interface,
82      * including {@link java.util.AbstractMap java.util.AbstractMap},
83      * {@link java.util.HashMap java.util.HashMap},
84      * {@link java.util.Hashtable java.util.Hashtable}, etc.
85      * @jsptagref.databindable Read Only
86      * @jsptagref.attributesyntaxvalue <i>string_mapObject</i>
87      * @netui:attribute required="true" rtexprvalue="true" type="java.util.Map"
88      * description="A JSP 2.0 EL expression pointing to a java.util.Map of parameters."
89      */

90     public void setMap(Map JavaDoc map) throws JspException JavaDoc
91     {
92         if (map == null) {
93             String JavaDoc s = Bundle.getString("Tags_MapAttrValueRequired", new Object JavaDoc[]{"map"});
94             registerTagError(s, null);
95         }
96         _map = map;
97     }
98
99     /**
100      * Add each parameter in the URL parameter map to the Parameter's parent.
101      * @throws JspException if a JSP exception has occurred
102      */

103     public int doStartTag() throws JspException JavaDoc
104     {
105         if (hasErrors())
106             return reportAndExit(SKIP_BODY);
107
108         Tag parentTag = findAncestorWithClass(this, IUrlParams.class);
109         if (parentTag != null) {
110             // this map shouldn't be null because the attribute is required.
111
assert(_map != null);
112             IUrlParams parent = (IUrlParams) parentTag;
113             Iterator JavaDoc it = _map.entrySet().iterator();
114             while (it.hasNext()) {
115                 Map.Entry JavaDoc key = (Map.Entry JavaDoc) it.next();
116                 parent.addParameter(key.getKey().toString(), key.getValue(), null);
117             }
118         }
119         else {
120             String JavaDoc msg = Bundle.getString("Tags_InvalidParameterMapParent");
121             registerTagError(msg, null);
122             reportErrors();
123         }
124
125         localRelease();
126         return SKIP_BODY;
127     }
128
129     /**
130      * Release any acquired resources.
131      */

132     protected void localRelease()
133     {
134         super.localRelease();
135         _map = null;
136     }
137 }
138
Popular Tags