KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > xml > SetTag


1 /*
2  * Copyright 1999-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
17 package org.apache.taglibs.standard.tag.common.xml;
18
19 import java.util.List JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 import org.apache.taglibs.standard.tag.common.core.Util;
26
27 /**
28  * <p>Tag handler for &lt;set&gt; in JSTL's XML library.</p>
29  *
30  * @author Shawn Bayern
31  */

32 public class SetTag extends TagSupport JavaDoc {
33
34     //*********************************************************************
35
// Internal state
36

37     private String JavaDoc select; // tag attribute
38
private String JavaDoc var; // tag attribute
39
private int scope; // processed tag attribute
40

41     //*********************************************************************
42
// Construction and initialization
43

44     /**
45      * Constructs a new handler. As with TagSupport, subclasses should
46      * not provide other constructors and are expected to call the
47      * superclass constructor.
48      */

49     public SetTag() {
50         super();
51         init();
52     }
53
54     // resets local state
55
private void init() {
56     var = null;
57     select = null;
58         scope = PageContext.PAGE_SCOPE;
59     }
60
61
62     //*********************************************************************
63
// Tag logic
64

65     // applies XPath expression from 'select' and stores the result in 'var'
66
public int doStartTag() throws JspException JavaDoc {
67         // process the query
68
XPathUtil xu = new XPathUtil(pageContext);
69         List JavaDoc result =
70         xu.selectNodes(XPathUtil.getContext(this), select);
71         Object JavaDoc ret = result;
72         
73         // unwrap primitive types if that's what we received
74
if (result.size() == 1) {
75             Object JavaDoc o = result.get(0);
76             if (o instanceof String JavaDoc || o instanceof Boolean JavaDoc
77             || o instanceof Number JavaDoc)
78                 ret = o;
79         }
80         
81         // expose the final result
82
pageContext.setAttribute(var, ret, scope);
83         return SKIP_BODY;
84     }
85
86     // Releases any resources we may have (or inherit)
87
public void release() {
88         super.release();
89         init();
90     }
91
92
93     //*********************************************************************
94
// Attribute accessors
95

96     public void setSelect(String JavaDoc select) {
97     this.select = select;
98     }
99
100     public void setVar(String JavaDoc var) {
101     this.var = var;
102     }
103
104     public void setScope(String JavaDoc scope) {
105     this.scope = Util.getScope(scope);
106     }
107 }
108
Popular Tags