KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > jelly > SelectTag


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.jelly;
39
40 import org.apache.commons.jelly.JellyTagException;
41 import org.apache.commons.jelly.MissingAttributeException;
42 import org.apache.commons.jelly.XMLOutput;
43 import org.apache.commons.jelly.TagSupport;
44
45 import org.jaxen.JaxenException;
46 import org.jaxen.VariableContext;
47 import org.jaxen.UnresolvableException;
48
49 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
50
51 /**
52  * A tag which evaluates an XPath expression against a {@link com.gargoylesoftware.htmlunit.html.DomNode}
53  * (e.g. a HtmlPage) and stores the result in a script context variable.<p>
54  * <em>Note that the expression must be self-contained, i.e. it must provide the evaluation
55  * context by referencing a script variable. See the example below</em>
56  *
57  * <p>Example:<br><center>
58  * <code>
59  * &lt;htmlunit:select var="title" xpath="$page/head/title"/&gt;
60  * </code>
61  *
62  * @author Christian Sell
63  * @version $Revision: 100 $
64  */

65 public class SelectTag extends TagSupport implements VariableContext {
66
67     /** The variable name to export. */
68     private String JavaDoc var_;
69
70     /** The XPath expression to evaluate. */
71     private String JavaDoc xpath_;
72
73     /** constructor. or what? */
74     public SelectTag() {
75     }
76
77     /**
78      * Process the tag
79      * @param output The xml output
80      * @throws MissingAttributeException if attribute is missing
81      * @throws JellyTagException If a problem occurs
82      */

83     public void doTag(final XMLOutput output) throws MissingAttributeException, JellyTagException {
84         if (var_ == null) {
85             throw new MissingAttributeException( "var" );
86         }
87         if (xpath_ == null) {
88             throw new MissingAttributeException( "xpath" );
89         }
90
91         Object JavaDoc value = null;
92         try {
93             final HtmlUnitXPath xpath = new HtmlUnitXPath(xpath_);
94             xpath.setVariableContext(this);
95             value = xpath.evaluate(null);
96         }
97         catch (final JaxenException e) {
98             throw new JellyTagException(e);
99         }
100
101         context.setVariable(var_, value);
102     }
103
104     /**
105      * @param var the variable name to define for this expression
106      */

107     public void setVar(final String JavaDoc var) {
108         var_ = var;
109     }
110
111     /**
112      * @param xpath the XPath expression to evaluate.
113      */

114     public void setXpath(final String JavaDoc xpath) {
115         xpath_ = xpath;
116     }
117
118     /**
119      * fetch the variable during evaluation of an XPath expression
120      * @param namespaceURI namespace URI
121      * @param prefix prefix
122      * @param localName local name
123      * @return the variable value
124      * @throws UnresolvableException variable not defined
125      */

126     public Object JavaDoc getVariableValue(final String JavaDoc namespaceURI, final String JavaDoc prefix, final String JavaDoc localName)
127         throws UnresolvableException {
128
129         final Object JavaDoc var = context.getVariable(localName);
130         if(var == null) {
131             throw new UnresolvableException(localName);
132         }
133         return var;
134     }
135 }
136
Popular Tags