KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > host > Form


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.javascript.host;
39
40 import com.gargoylesoftware.htmlunit.Assert;
41 import com.gargoylesoftware.htmlunit.html.HtmlElement;
42 import com.gargoylesoftware.htmlunit.html.HtmlForm;
43 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath;
44 import com.gargoylesoftware.htmlunit.javascript.ElementArray;
45
46 import java.io.IOException JavaDoc;
47
48 import org.jaxen.JaxenException;
49 import org.jaxen.XPath;
50 import org.mozilla.javascript.Context;
51 import org.mozilla.javascript.Function;
52 import org.mozilla.javascript.Scriptable;
53
54 /**
55  * A JavaScript object for a Form.
56  *
57  * @version $Revision: 100 $
58  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
59  * @author Daniel Gredler
60  * @author Kent Tong
61  * @author Chris Erskine
62  *
63  * @see <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/form.asp">MSDN documentation</a>
64  */

65 public class Form extends HTMLElement {
66
67     private static final long serialVersionUID = -1860993922147246513L;
68     private ElementArray elements_; // has to be a member to have equality (==) working
69

70     /**
71      * Create an instance. A default constructor is required for all javascript objects.
72      */

73     public Form() { }
74
75
76     /**
77      * Javascript constructor. This must be declared in every javascript file because
78      * the rhino engine won't walk up the hierarchy looking for constructors.
79      */

80     public final void jsConstructor() {
81     }
82
83
84     /**
85      * @see com.gargoylesoftware.htmlunit.javascript.SimpleScriptable#setHtmlElement(HtmlElement)
86      */

87     public void setHtmlElement( final HtmlElement htmlElement ) {
88         super.setHtmlElement( htmlElement );
89         final HtmlForm htmlForm = getHtmlForm();
90         htmlForm.setScriptObject( this );
91     }
92
93
94     /**
95      * Return the value of the javascript attribute "name".
96      * @return The value of this attribute.
97      */

98     public String JavaDoc jsxGet_name() {
99         return getHtmlForm().getNameAttribute();
100     }
101
102
103     /**
104      * Return the value of the javascript attribute "elements".
105      * @return The value of this attribute.
106      */

107     public ElementArray jsxGet_elements() {
108         if (elements_ == null) {
109             final HtmlForm htmlForm = getHtmlForm();
110             elements_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME);
111             try {
112                 final XPath xpath = new HtmlUnitXPath("//*[(name() = 'input' or name() = 'button'"
113                         + " or name() = 'select' or name() = 'textarea')]",
114                         HtmlUnitXPath.buildSubtreeNavigator(htmlForm));
115                 elements_.init(htmlForm, xpath);
116             }
117             catch (final JaxenException e) {
118                 throw Context.reportRuntimeError("Failed to initialize collection form.elements: " + e.getMessage());
119             }
120         }
121         return elements_;
122     }
123
124
125     /**
126      * Return the value of the javascript attribute "length".
127      * Does not count input type=image elements as browsers (IE6, Mozilla 1.7) do
128      * (cf <a HREF="http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/length.asp">MSDN doc</a>)
129      * @return The value of this attribute.
130      */

131     public int jsxGet_length() {
132         final int all = jsxGet_elements().jsGet_length();
133         final int images = getHtmlForm().getHtmlElementsByAttribute("input", "type", "image").size();
134         return all - images;
135     }
136
137
138     /**
139      * Return the value of the javascript attribute "action".
140      * @return The value of this attribute.
141      */

142     public String JavaDoc jsxGet_action() {
143         return getHtmlForm().getActionAttribute();
144     }
145
146
147     /**
148      * Set the value of the javascript attribute "action".
149      * @param action The new value.
150      */

151     public void jsxSet_action( final String JavaDoc action ) {
152         Assert.notNull("action", action);
153         getHtmlForm().setActionAttribute(action);
154     }
155
156
157     /**
158      * Return the value of the javascript attribute "method".
159      * @return The value of this attribute.
160      */

161     public String JavaDoc jsxGet_method() {
162         return getHtmlForm().getMethodAttribute();
163     }
164
165
166     /**
167      * Set the value of the javascript attribute "method".
168      * @param method The new value.
169      */

170     public void jsxSet_method( final String JavaDoc method ) {
171         Assert.notNull("method", method);
172         getHtmlForm().setMethodAttribute(method);
173     }
174
175
176     /**
177      * Return the value of the javascript attribute "target".
178      * @return The value of this attribute.
179      */

180     public String JavaDoc jsxGet_target() {
181         return getHtmlForm().getTargetAttribute();
182     }
183
184     /**
185      * Get the onsubmit event handler for this element.
186      * @return <code>org.mozilla.javascript.Function</code>
187      */

188     public Function jsxGet_onsubmit() {
189         return getHtmlForm().getEventHandler("onsubmit");
190     }
191
192     /**
193      * Set the onsubmit event handler for this element.
194      * @param onsubmit the new handler
195      */

196     public void jsxSet_onsubmit(final Function onsubmit) {
197         getHtmlForm().setEventHandler("onsubmit", onsubmit);
198     }
199
200     /**
201      * Set the value of the javascript attribute "target".
202      * @param target The new value.
203      */

204     public void jsxSet_target( final String JavaDoc target ) {
205         Assert.notNull("target", target);
206         getHtmlForm().setTargetAttribute(target);
207     }
208
209
210     /**
211      * Return the value of the javascript attribute "encoding".
212      * @return The value of this attribute.
213      */

214     public String JavaDoc jsxGet_encoding() {
215         return getHtmlForm().getEnctypeAttribute();
216     }
217
218
219     /**
220      * Set the value of the javascript attribute "encoding".
221      * @param encoding The new value.
222      */

223     public void jsxSet_encoding( final String JavaDoc encoding ) {
224         Assert.notNull("encoding", encoding);
225         getHtmlForm().setEnctypeAttribute(encoding);
226     }
227
228
229     private HtmlForm getHtmlForm() {
230         return (HtmlForm)getHtmlElementOrDie();
231     }
232
233     /**
234      * Submit the form.
235      *
236      * @throws IOException if an io error occurs
237      */

238     public void jsxFunction_submit() throws IOException JavaDoc {
239         getHtmlForm().submit();
240     }
241
242
243     /**
244      * Reset this form
245      */

246     public void jsxFunction_reset() {
247         getHtmlForm().reset();
248     }
249
250
251     /**
252      * Return the specified property or NOT_FOUND if it could not be found.
253      * @param name The name of the property
254      * @param start The scriptable object that was originally queried for this property
255      * @return The property.
256      */

257     public Object JavaDoc get( final String JavaDoc name, final Scriptable start ) {
258         return ((Form) start).get(name);
259     }
260
261     /**
262      * Return the specified property or NOT_FOUND if it could not be found.
263      * @param name The name of the property
264      * @return The property.
265      */

266     Object JavaDoc get(final String JavaDoc name) {
267         // Try to get the element or elements specified from the form element array
268
// (except input type="image" that can't be accessed this way)
269
final ElementArray elements = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME);
270         final HtmlForm htmlForm = getHtmlForm();
271         try {
272             final XPath xpath = new HtmlUnitXPath("//*[(@name = '" + name + "' or @id = '" + name + "')"
273                     + " and ((name() = 'input' and translate(@type, 'IMAGE', 'image') != 'image') or name() = 'button'"
274                     + " or name() = 'select' or name() = 'textarea')]",
275                     HtmlUnitXPath.buildSubtreeNavigator(htmlForm));
276             elements.init(htmlForm, xpath);
277         }
278         catch (final JaxenException e) {
279             throw Context.reportRuntimeError("Failed to initialize collection: " + e.getMessage());
280         }
281         
282         Object JavaDoc result = elements;
283         final int nbElements = elements.jsGet_length();
284         if (nbElements == 0) {
285             result = NOT_FOUND;
286         }
287         else if (nbElements == 1) {
288             result = elements.get(0, elements);
289         }
290         
291         if (result == NOT_FOUND) {
292             result = super.get( name, this );
293         }
294         
295         return result;
296     }
297
298
299     /**
300      * Return the specified indexed property
301      * @param index The index of the property
302      * @param start The scriptable object that was originally queried for this property
303      * @return The property.
304      */

305     public Object JavaDoc get( final int index, final Scriptable start ) {
306         return jsxGet_elements().get(index, ((Form) start).jsxGet_elements());
307     }
308 }
309
Popular Tags