KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xtags > xpath > VariableTag


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.xtags.xpath;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.JspWriter JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import org.dom4j.Node;
29 import org.dom4j.XPath;
30
31 /** A tag which defines a variable from an XPath expression
32   *
33   * @author James Strachan
34   */

35 public class VariableTag extends AbstractTag {
36
37     /** Holds the XPath selection instance. */
38     private XPath xpath;
39     /** Holds value of property id. */
40     private String JavaDoc id;
41     /** Holds value of property type. */
42     private String JavaDoc type;
43     
44     
45     //-------------------------------------------------------------------------
46
public VariableTag() {
47     }
48
49     // Tag interface
50
//-------------------------------------------------------------------------
51
public int doStartTag() throws JspException JavaDoc {
52         if ( xpath != null ) {
53             Object JavaDoc value = null;
54             Object JavaDoc inputNodes = getInputNodes(false);
55             if ( type == null ) {
56                 // default to string value
57
value = xpath.valueOf( inputNodes );
58             }
59             else if ( type.equalsIgnoreCase( "string" ) || type.equals( "java.lang.String" ) ) {
60                 value = xpath.valueOf( inputNodes );
61             }
62             else if (type.equals( "node" ) || type.equals( "org.dom4j.Node") ) {
63                 value = xpath.selectSingleNode( inputNodes );
64             }
65             else if (type.equals( "list" ) || type.equals( "java.util.List") ) {
66                 value = xpath.selectNodes( inputNodes );
67             }
68             else if ( type.equalsIgnoreCase( "number" ) || type.equals( "java.lang.Number" ) || type.equals( "java.lang.Double" ) ) {
69                 Number JavaDoc n = xpath.numberValueOf( inputNodes );
70                 value = n;
71                 if ( type.equals( "java.lang.Double" ) && ! (value instanceof Double JavaDoc) ) {
72                     value = new Double JavaDoc( n.doubleValue() );
73                 }
74             }
75             else {
76                 value = xpath.selectObject( inputNodes );
77             }
78             if ( value == null ) {
79                 pageContext.removeAttribute( getId() );
80             }
81             else {
82                 pageContext.setAttribute( getId(), value );
83             }
84         }
85         return SKIP_BODY;
86     }
87
88     public void release() {
89         super.release();
90         xpath = null;
91         id = null;
92         type = null;
93     }
94
95     // Properties
96
//-------------------------------------------------------------------------
97

98     /** Sets the select XPath expression
99       */

100     public void setSelect(String JavaDoc select) {
101         this.xpath = createXPath( select );
102     }
103
104     /** Sets the XPath selection expression
105       */

106     public void setSelectXPath(XPath xpath) {
107         this.xpath = xpath;
108     }
109     
110     /** Getter for property id.
111      * @return Value of property id.
112      */

113     public String JavaDoc getId() {
114         return id;
115     }
116     
117     /** Setter for property id.
118      * @param id New value of property id.
119      */

120     public void setId(String JavaDoc id) {
121         this.id = id;
122     }
123     /** Getter for property type.
124      * @return Value of property type.
125      */

126     public String JavaDoc getType() {
127         return type;
128     }
129     /** Setter for property type.
130      * @param type New value of property type.
131      */

132     public void setType(String JavaDoc type) {
133         this.type = type;
134     }
135 }
136
Popular Tags