KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > Form


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx;
34
35 import java.net.URL JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
37 import java.net.URLEncoder JavaDoc;
38
39 /**
40  * <FORM> element in an HTML page.
41  */

42 public class Form extends Link {
43
44     /**
45      * Make a LinkElement from a start tag and end tag and a base URL (for relative references).
46      * The tags must be on the same page.
47      * @param startTag Start tag of element
48      * @param endTag End tag of element
49      * @param base Base URL used for relative references
50      */

51     public Form (Tag startTag, Tag endTag, URL JavaDoc base) throws MalformedURLException JavaDoc {
52         super (startTag, endTag, base);
53     }
54
55     /**
56      * Construct the URL for this form, from its start tag and a base URL (for relative references).
57      * @param tag Start tag of form.
58      * @param base Base URL used for relative references
59      * @return URL to which the button points
60      */

61      protected URL JavaDoc urlFromHref (Tag tag, URL JavaDoc base) throws MalformedURLException JavaDoc {
62         String JavaDoc href = tag.getHTMLAttribute ("action");
63         if (href == null)
64             // without an action attribute, the URL defaults to the base
65
return base;
66         return new URL JavaDoc (base, href);
67     }
68
69     /**
70      * Get the method used to access this link.
71      * @return GET or POST.
72      */

73     public int getMethod () {
74         return getHTMLAttribute ("method", "GET").equalsIgnoreCase ("post")
75            ? POST : GET;
76     }
77
78     /**
79      * Construct the query that would be submitted if the form's SUBMIT button were pressed.
80      * @return a URL representing the submitted form, or null if the form cannot be represented as a URL.
81      */

82     public URL JavaDoc makeQuery () {
83         return makeQuery (null);
84     }
85
86     /**
87      * Construct the query that would be submitted if the specified button were pressed.
88      * @param button form button that triggers the submission.
89      * @return a URL representing the submitted form, or null if the form cannot be represented as a URL.
90      */

91     public URL JavaDoc makeQuery (FormButton button) {
92         StringBuffer JavaDoc querybuf = new StringBuffer JavaDoc ();
93         makeQuery (getChild (), querybuf);
94         
95         if (button != null) {
96             String JavaDoc type = button.getHTMLAttribute ("type", "");
97             String JavaDoc name = button.getHTMLAttribute ("name", "");
98             String JavaDoc value = button.getHTMLAttribute ("value", "");
99
100             if (type.equalsIgnoreCase ("submit")) {
101                 passArgument (querybuf, name, value);
102             }
103             else if (type.equalsIgnoreCase ("image")) {
104                 // simulate an imagemap click
105
passArgument (querybuf, name+".x", "0");
106                 passArgument (querybuf, name+".y", "0");
107             }
108         }
109         
110         String JavaDoc href = getURL().toExternalForm () + "?";
111         if (querybuf.length() > 0)
112             href += querybuf.toString().substring (1); // deletes '&' from front of querybuf
113

114         try {
115             return new URL JavaDoc (href);
116         } catch (MalformedURLException JavaDoc e) {
117             throw new RuntimeException JavaDoc ("internal error: " + e);
118         }
119     }
120
121     // appends "&name=val&name=val..." to query
122
// for all form fields found among elements and their children
123
private void makeQuery (Element elem, StringBuffer JavaDoc query) {
124       for (Element e = elem; e != null; e = e.getSibling ()) {
125             String JavaDoc tagName = e.getTagName ();
126             if (tagName == Tag.INPUT) {
127                 String JavaDoc type = e.getHTMLAttribute ("type", "text").toLowerCase ();
128
129                 if ( // always pass these fields
130
type.equals ("text")
131                     || type.equals ("password")
132                     || type.equals ("hidden")
133
134                     // pass these fields if checked
135
|| ((type.equals ("checkbox") || type.equals ("radio"))
136                         && e.hasHTMLAttribute ("checked"))) {
137                     passArgument (query,
138                                   e.getHTMLAttribute ("name", ""),
139                                   e.getHTMLAttribute ("value", ""));
140                 }
141             }
142             else if (tagName == Tag.SELECT) {
143                 String JavaDoc name = e.getHTMLAttribute ("name", "");
144                 for (Element opt = e.getChild(); opt != null; opt = opt.getSibling()) {
145                     if (opt.getTagName() == Tag.OPTION
146                         && opt.hasHTMLAttribute ("selected")) {
147                         passArgument (query, name, opt.getHTMLAttribute ("value", ""));
148                     }
149                 }
150             }
151             else if (tagName == Tag.TEXTAREA) {
152                 passArgument (query, e.getHTMLAttribute ("name", ""), e.toText ());
153             }
154             else {
155                 makeQuery (e.getChild (), query);
156             }
157         }
158     }
159     
160     private void passArgument (StringBuffer JavaDoc query, String JavaDoc name, String JavaDoc value) {
161         query.append ('&');
162         query.append (URLEncoder.encode(name)); // FIX: should name be encoded?
163
query.append ('=');
164         query.append (URLEncoder.encode(value));
165     }
166 }
167
168
Popular Tags