KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > CoreParamTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.jsp.PageContextImpl;
34 import com.caucho.jstl.NameValueTag;
35 import com.caucho.util.L10N;
36
37 import javax.el.ELContext;
38 import javax.servlet.jsp.JspException JavaDoc;
39 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
40 import javax.servlet.jsp.tagext.Tag JavaDoc;
41
42 /**
43  * Looks up an i18n message from a bundle and prints it.
44  */

45 public class CoreParamTag extends BodyTagSupport JavaDoc {
46   private static L10N L = new L10N(CoreParamTag.class);
47   
48   private Expr _nameExpr;
49   private Expr _valueExpr;
50
51   /**
52    * Sets the name
53    *
54    * @param name the JSP-EL expression for the name.
55    */

56   public void setName(Expr name)
57   {
58     _nameExpr = name;
59   }
60
61   /**
62    * Sets the value
63    *
64    * @param value the JSP-EL expression for the value.
65    */

66   public void setValue(Expr value)
67   {
68     _valueExpr = value;
69   }
70
71   /**
72    * Process the tag.
73    */

74   public int doStartTag()
75     throws JspException JavaDoc
76   {
77     try {
78       if (_valueExpr == null)
79     return EVAL_BODY_BUFFERED;
80
81       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
82       ELContext env = pageContext.getELContext();
83     
84       String JavaDoc name = _nameExpr.evalString(env);
85
86       if (name == null)
87     return SKIP_BODY;
88       
89       String JavaDoc value = _valueExpr.evalString(env);
90     
91       Tag parent = getParent();
92       for (; parent != null; parent = parent.getParent()) {
93     if (parent instanceof NameValueTag) {
94       NameValueTag tag = (NameValueTag) parent;
95
96       if (value == null)
97         tag.addParam(name, "");
98       else
99         tag.addParam(name, value);
100     
101       return SKIP_BODY;
102     }
103       }
104       
105       throw new JspException JavaDoc(L.l("c:param requires c:url or c:import parent."));
106     } catch (JspException JavaDoc e) {
107       throw e;
108     } catch (Exception JavaDoc e) {
109       throw new JspException JavaDoc(e);
110     }
111   }
112
113   /**
114    * Process the tag.
115    */

116   public int doEndTag()
117     throws JspException JavaDoc
118   {
119     try {
120       if (_valueExpr != null)
121     return EVAL_PAGE;
122       
123       String JavaDoc value;
124
125       if (bodyContent != null)
126     value = bodyContent.getString().trim();
127       else
128     value = "";
129     
130       PageContextImpl pageContext = (PageContextImpl) this.pageContext;
131       ELContext env = pageContext.getELContext();
132     
133       String JavaDoc name = _nameExpr.evalString(env);
134
135       Object JavaDoc parent = getParent();
136       if (! (parent instanceof NameValueTag))
137     throw new JspException JavaDoc(L.l("c:param requires c:url or c:import parent."));
138
139       if (name == null)
140     return EVAL_PAGE;
141
142       NameValueTag tag = (NameValueTag) parent;
143
144       if (value == null)
145     tag.addParam(name, "");
146       else
147     tag.addParam(name, value);
148     
149       return EVAL_PAGE;
150     } catch (Exception JavaDoc e) {
151       throw new JspException JavaDoc(e);
152     }
153   }
154 }
155
Popular Tags