KickJava   Java API By Example, From Geeks To Geeks.

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


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.TagSupport;
43
44 import com.gargoylesoftware.htmlunit.Page;
45 import com.gargoylesoftware.htmlunit.WebClient;
46 import com.gargoylesoftware.htmlunit.html.HtmlPage;
47
48 /**
49  * Abstract superclass for all the HtmlUnit jelly tags.
50  *
51  * @version $Revision: 100 $
52  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53  */

54 public abstract class HtmlUnitTagSupport extends TagSupport {
55     /** The var attribute which is the name of the variable to put the result into */
56     private String JavaDoc var_;
57     private String JavaDoc page_;
58
59
60     /**
61      * Create an instance
62      */

63     public HtmlUnitTagSupport() {
64     }
65
66
67     /**
68      * Callback from Jelly to set the value of the page attribute.
69      * @param page The new value.
70      */

71     public void setPage( final String JavaDoc page ) {
72         page_ = page;
73     }
74
75
76     /**
77      * Return the page specified in the script
78      * @return The page.
79      * @throws JellyTagException If a problem occurs.
80      */

81     protected final Page getPage() throws JellyTagException {
82         if( page_ == null ) {
83             throw new JellyTagException("page is a mandatory attribute");
84         }
85
86         final Object JavaDoc object = getContext().getVariable(page_);
87         try {
88             return (Page)object;
89         }
90         catch( final ClassCastException JavaDoc e ) {
91             throw new JellyTagException("Expected page object in variable ["+page_+"] but found ["+object+"]");
92         }
93     }
94
95
96     /**
97      * Return the HtmlPage specified in the script. If the page is not an instance of HtmlPage then
98      * throw a JellyTagException
99      * @return the HtmlPage
100      * @throws JellyTagException If a problem occurs.
101      */

102     protected final HtmlPage getHtmlPage() throws JellyTagException {
103         final Page page = getPage();
104         if( page instanceof HtmlPage ) {
105             return (HtmlPage)page;
106         }
107         else {
108             throw new JellyTagException("Page isn't an instance of HtmlPage: "+page.getClass().getName());
109         }
110     }
111
112
113     /**
114      * Callback from Jelly to set the value of the var attribute.
115      * @param var The new value.
116      */

117     public void setVar( final String JavaDoc var ) {
118         var_ = var;
119     }
120
121
122     /**
123      * Return the value of the "var" attribute or throw an exception if it hasn't been set.
124      * @return The value of var
125      * @throws MissingAttributeException If var hasn't been set.
126      */

127     public String JavaDoc getVarValueOrDie() throws MissingAttributeException {
128         if( var_ == null ) {
129             throw new MissingAttributeException("var");
130         }
131         return var_;
132     }
133
134
135     /**
136      * Return the value of the "var" attribute or throw an exception if it hasn't been set.
137      * @return The value of var
138      * @throws MissingAttributeException if missing
139      */

140     public String JavaDoc getVarValueOrNull() throws MissingAttributeException {
141         return var_;
142     }
143
144
145     /**
146      * Return the web client that is currently in use.
147      * @return the web client
148      * @throws JellyTagException If a web client cannot be found.
149      */

150     public WebClient getWebClient() throws JellyTagException {
151         final WebClientTag webClientTag = (WebClientTag) findAncestorWithClass(WebClientTag.class);
152         if( webClientTag == null ) {
153             throw new JellyTagException("Unable to determine webClient");
154         }
155         else {
156             return webClientTag.getWebClient();
157         }
158     }
159 }
160
Popular Tags