KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > ParamTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.util.Entry;
14 import javax.servlet.jsp.*;
15 import java.util.*;
16 import org.mmbase.util.logging.*;
17
18 /**
19  * Adds an extra parameter to the parent URL tag.
20  *
21  * @author Michiel Meeuwissen
22  * @version $Id: ParamTag.java,v 1.13 2006/07/17 15:38:47 johannes Exp $
23  */

24
25 public class ParamTag extends ContextReferrerTag implements ParamHandler {
26     private static final Logger log = Logging.getLoggerInstance(ParamTag.class);
27
28     protected List entries = null;
29
30     private Attribute name = Attribute.NULL;
31     private Attribute value = Attribute.NULL;
32     private Attribute referid = Attribute.NULL;
33     private ParamHandler paramHandler;
34     private boolean handled;
35
36     public void setName(String JavaDoc n) throws JspTagException {
37         name = getAttribute(n);
38     }
39     public void setValue(String JavaDoc v) throws JspTagException {
40         value = getAttribute(v);
41     }
42     /**
43      * @since MMBase-1.8
44      */

45     public void setReferid(String JavaDoc r) throws JspTagException {
46         referid = getAttribute(r);
47     }
48
49     public void addParameter(String JavaDoc key, Object JavaDoc value) throws JspTagException {
50         if (entries == null) entries = new ArrayList();
51         entries.add(new Entry(key, value));
52         if (log.isDebugEnabled()) {
53             log.debug("entries " + entries);
54         }
55     }
56
57     public int doStartTag() throws JspException {
58         findWriter(false); // just to call haveBody, mainly for mm:link.
59
paramHandler = (ParamHandler) findParentTag(ParamHandler.class, null);
60         handled = false;
61         return super.doStartTag();
62     }
63
64     public int doAfterBody() throws JspException {
65         if (value == Attribute.NULL && referid == Attribute.NULL && entries == null) {
66             if (bodyContent != null) {
67                 // the value is the body context.
68
helper.setValueOnly(bodyContent.getString(), WriterHelper.IMPLICITLIST); // to deal with 'vartype' casting
69
paramHandler.addParameter(name.getString(this), helper.getValue());
70                 handled = true;
71             }
72         }
73         return super.doAfterBody();
74     }
75
76     public int doEndTag() throws JspTagException {
77         if (! handled) {
78             if (value != Attribute.NULL) {
79                 if (referid != Attribute.NULL || entries != null) throw new JspTagException("Must specify either 'value', 'referid' or sub-param-tags, not both");
80                 helper.setValueOnly(value.getString(this), WriterHelper.IMPLICITLIST); // to deal with 'vartype' casting
81
paramHandler.addParameter(name.getString(this), helper.getValue());
82
83             } else if (referid != Attribute.NULL) {
84                 if (entries != null) throw new JspTagException("Must specify either 'value', 'referid' or sub-param-tags, not both");
85                 paramHandler.addParameter(name.getString(this), getObject(referid.getString(this)));
86             } else if (entries != null) {
87                 paramHandler.addParameter(name.getString(this), entries);
88                 entries = null;
89             } else {
90                 paramHandler.addParameter(name.getString(this), "");
91             }
92         }
93         paramHandler = null;
94         return super.doEndTag();
95     }
96
97     public void doFinally() {
98         paramHandler = null;
99         entries = null;
100         super.doFinally();
101     }
102 }
103
Popular Tags