KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > HTMLFormElementImpl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Jan 14, 2006
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.html2.HTMLCollection;
33 import org.w3c.dom.html2.HTMLFormElement;
34 import org.lobobrowser.html.FormInput;
35 import org.lobobrowser.html.HtmlRendererContext;
36 import org.lobobrowser.html.js.*;
37 import org.mozilla.javascript.*;
38
39 public class HTMLFormElementImpl extends HTMLAbstractUIElement implements
40         HTMLFormElement {
41     public HTMLFormElementImpl(String JavaDoc name) {
42         super(name);
43     }
44
45     public HTMLFormElementImpl() {
46         super("FORM");
47     }
48
49     public Object JavaDoc namedItem(final String JavaDoc name) {
50         try {
51             //TODO: This could use document.namedItem.
52
this.visit(new NodeVisitor() {
53                 public void visit(Node JavaDoc node) {
54                     if(HTMLFormElementImpl.isInput(node)) {
55                         if(name.equals(((Element JavaDoc) node).getAttribute("name"))) {
56                             throw new StopVisitorException(node);
57                         }
58                     }
59                 }
60             });
61         } catch(StopVisitorException sve) {
62             return sve.getTag();
63         }
64         return null;
65     }
66     
67     public Object JavaDoc item(final int index) {
68         try {
69             this.visit(new NodeVisitor() {
70                 private int current = 0;
71                 
72                 public void visit(Node JavaDoc node) {
73                     if(HTMLFormElementImpl.isInput(node)) {
74                         if(this.current == index) {
75                             throw new StopVisitorException(node);
76                         }
77                         this.current++;
78                     }
79                 }
80             });
81         } catch(StopVisitorException sve) {
82             return sve.getTag();
83         }
84         return null;
85     }
86
87     public HTMLCollection getElements() {
88         return new DescendentHTMLCollection(this, new InputFilter());
89     }
90
91     public int getLength() {
92         return new DescendentHTMLCollection(this, new InputFilter()).getLength();
93     }
94
95     public String JavaDoc getName() {
96         return this.getAttribute("name");
97     }
98
99     public void setName(String JavaDoc name) {
100         this.setAttribute("name", name);
101     }
102
103     public String JavaDoc getAcceptCharset() {
104         return this.getAttribute("acceptCharset");
105     }
106
107     public void setAcceptCharset(String JavaDoc acceptCharset) {
108         this.setAttribute("acceptCharset", acceptCharset);
109     }
110
111     public String JavaDoc getAction() {
112         return this.getAttribute("action");
113     }
114
115     public void setAction(String JavaDoc action) {
116         this.setAttribute("action", action);
117     }
118
119     public String JavaDoc getEnctype() {
120         return this.getAttribute("enctype");
121     }
122
123     public void setEnctype(String JavaDoc enctype) {
124         this.setAttribute("enctype", enctype);
125     }
126
127     public String JavaDoc getMethod() {
128         String JavaDoc method = this.getAttribute("method");
129         if(method == null) {
130             method = "GET";
131         }
132         return method;
133     }
134
135     public void setMethod(String JavaDoc method) {
136         this.setAttribute("method", method);
137     }
138
139     public String JavaDoc getTarget() {
140         return this.getAttribute("target");
141     }
142
143     public void setTarget(String JavaDoc target) {
144         this.setAttribute("target", target);
145     }
146
147     public void submit() {
148         this.submit(null);
149     }
150
151     private Function onsubmit;
152     
153     public void setOnsubmit(Function value) {
154         this.onsubmit = value;
155     }
156     
157     public Function getOnsubmit() {
158         return this.getEventFunction(this.onsubmit, "onsubmit");
159     }
160     
161     /**
162      * This method should be called when form submission is
163      * done by a submit button.
164      * @param extraFormInputs Any additional form inputs that
165      * need to be submitted, e.g. the submit button parameter.
166      */

167     public final void submit(final FormInput[] extraFormInputs) {
168         Function onsubmit = this.getOnsubmit();
169         if(onsubmit != null) {
170             //TODO: onsubmit event object?
171
if(!Executor.executeFunction(this, onsubmit, null)) {
172                 return;
173             }
174         }
175         HtmlRendererContext context = this.getHtmlRendererContext();
176         if(context != null) {
177             final ArrayList JavaDoc formInputs = new ArrayList JavaDoc();
178             if(extraFormInputs != null) {
179                 for(int i = 0; i < extraFormInputs.length; i++) {
180                     formInputs.add(extraFormInputs[i]);
181                 }
182             }
183             this.visit(new NodeVisitor() {
184                 public void visit(Node JavaDoc node) {
185                     if(node instanceof HTMLElementImpl) {
186                         FormInput[] fis = ((HTMLElementImpl) node).getFormInputs();
187                         if(fis != null) {
188                             for(int i = 0; i < fis.length; i++) {
189                                 FormInput fi = fis[i];
190                                 if(fi.getName() == null) {
191                                     throw new IllegalStateException JavaDoc("Form input does not have a name: " + node);
192                                 }
193                                 formInputs.add(fi);
194                             }
195                         }
196                     }
197                 }
198             });
199             FormInput[] fia = (FormInput[]) formInputs.toArray(FormInput.EMPTY_ARRAY);
200             String JavaDoc href = this.getAction();
201             if(href == null) {
202                 href = this.getBaseURI();
203             }
204             try {
205                 URL JavaDoc url = this.getFullURL(href);
206                 context.submitForm(this.getMethod(), url, this.getTarget(), this.getEnctype(), fia);
207             } catch(MalformedURLException JavaDoc mfu) {
208                 this.warn("submit()", mfu);
209             }
210         }
211     }
212
213     public void reset() {
214         this.visit(new NodeVisitor() {
215             public void visit(Node JavaDoc node) {
216                 if(node instanceof HTMLBaseInputElement) {
217                     ((HTMLBaseInputElement) node).resetInput();
218                 }
219             }
220         });
221     }
222
223     static boolean isInput(Node JavaDoc node) {
224         String JavaDoc name = node.getNodeName().toLowerCase();
225         return name.equals("input") || name.equals("textarea") || name.equals("select");
226     }
227     
228     private class InputFilter implements NodeFilter {
229         /* (non-Javadoc)
230          * @see org.xamjwg.html.domimpl.NodeFilter#accept(org.w3c.dom.Node)
231          */

232         public boolean accept(Node JavaDoc node) {
233             return HTMLFormElementImpl.isInput(node);
234         }
235     }
236 }
237
Popular Tags