001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.html;
020:
021: // java imports
022:
023: import org.apache.beehive.netui.tags.AbstractClassicTag;
024: import org.apache.beehive.netui.util.Bundle;
025:
026: import javax.servlet.jsp.JspException;
027: import javax.servlet.jsp.tagext.Tag;
028: import java.util.Iterator;
029: import java.util.Map;
030:
031: /**
032: * Writes each in a map of URL parameters to a URL on its parent tag.
033: * The parent tag must implement IUrlParams.
034: * @jsptagref.tagdescription <p>Writes a group of name/value pairs to the URL or the parent tag.
035: *
036: * <p>The <netui:parameterMap> can be nested inside of the
037: * {@link org.apache.beehive.netui.tags.html.Anchor},
038: * {@link org.apache.beehive.netui.tags.html.Button},
039: * {@link org.apache.beehive.netui.tags.html.Form}, and
040: * {@link org.apache.beehive.netui.tags.html.Image} tags.
041: *
042: * <p>You can dynamically determine the value of the <netui:parameterMap> tag by pointing
043: * the <code>map</code> attribute at a {@link java.util.HashMap java.util.HashMap} object.
044: * @example Assume that there is a java.util.HashMap
045: * object in the Controller file.
046: *
047: * <pre> public HashMap hashMap = new HashMap();
048: * hashMap.put("q", "Socrates");
049: * hashMap.put("lr", "lang_el");
050: * hashMap.put("as_qdr", "m3");</pre>
051: *
052: * <p>The following set of tags will read the HashMap object and generate a
053: * link with a set of URL parameters.
054: *
055: * <pre> <netui:anchor href="http://www.google.com/search">
056: * Search Greek language web sites updated in the last three months with the query "Socrates".
057: * <netui:parameterMap map="${pageFlow.hashMap}"/>
058: * </netui:anchor></pre>
059: *
060: * <p>The URL produced appears as follows:
061: *
062: * <pre> http://www.google.com/search?lr=lang_el&q=Socrates&as_qdr=m3</pre>
063: * @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."
064: */
065: public class ParameterMap extends AbstractClassicTag {
066: private Map _map = null;
067:
068: /**
069: * Return the name of the Tag.
070: */
071: public String getTagName() {
072: return "ParameterMap";
073: }
074:
075: /**
076: * Sets the map expression.
077: * @param map the map expression.
078: * @jsptagref.attributedescription A data binding expression pointing to a {@link java.util.Map java.util.Map} of parameters.
079: * The expression can point at any implementation of the java.util.Map interface,
080: * including {@link java.util.AbstractMap java.util.AbstractMap},
081: * {@link java.util.HashMap java.util.HashMap},
082: * {@link java.util.Hashtable java.util.Hashtable}, etc.
083: * @jsptagref.databindable Read Only
084: * @jsptagref.attributesyntaxvalue <i>string_mapObject</i>
085: * @netui:attribute required="true" rtexprvalue="true" type="java.util.Map"
086: * description="A JSP 2.0 EL expression pointing to a java.util.Map of parameters."
087: */
088: public void setMap(Map map) throws JspException {
089: if (map == null) {
090: String s = Bundle.getString("Tags_MapAttrValueRequired",
091: new Object[] { "map" });
092: registerTagError(s, null);
093: }
094: _map = map;
095: }
096:
097: /**
098: * Add each parameter in the URL parameter map to the Parameter's parent.
099: * @throws JspException if a JSP exception has occurred
100: */
101: public int doStartTag() throws JspException {
102: if (hasErrors())
103: return reportAndExit(SKIP_BODY);
104:
105: Tag parentTag = findAncestorWithClass(this , IUrlParams.class);
106: if (parentTag != null) {
107: // this map shouldn't be null because the attribute is required.
108: assert (_map != null);
109: IUrlParams parent = (IUrlParams) parentTag;
110: Iterator it = _map.entrySet().iterator();
111: while (it.hasNext()) {
112: Map.Entry key = (Map.Entry) it.next();
113: parent.addParameter(key.getKey().toString(), key
114: .getValue(), null);
115: }
116: } else {
117: String msg = Bundle
118: .getString("Tags_InvalidParameterMapParent");
119: registerTagError(msg, null);
120: reportErrors();
121: }
122:
123: localRelease();
124: return SKIP_BODY;
125: }
126:
127: /**
128: * Release any acquired resources.
129: */
130: protected void localRelease() {
131: super.localRelease();
132: _map = null;
133: }
134: }
|