KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > html > FormFiller


1 package net.matuschek.html;
2
3 /*************************************************
4     Copyright (c) 2001/2002 by Daniel Matuschek
5 **************************************************/

6
7
8 import java.net.MalformedURLException JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 import net.matuschek.http.ExtendedURL;
13 import net.matuschek.http.HttpConstants;
14
15 import org.apache.log4j.Category;
16
17 import org.w3c.dom.Element JavaDoc;
18 import org.w3c.dom.NodeList JavaDoc;
19
20 /**
21  * This class fills out form fields by predefined values
22  *
23  * @author Daniel Matuschek
24  * @version $Id: FormFiller.java,v 1.9 2003/01/30 14:54:51 oliver_schmidt Exp $
25  */

26 public class FormFiller {
27
28   /**
29    * Form Handlers
30    * @link aggregation
31    * @associates <{FormHandler}>
32    */

33   private Vector JavaDoc formHandlers = null;
34
35   /** Log4J category for logging purposes */
36   private Category log;
37   
38
39   /**
40    * Basic initialization
41    */

42   public FormFiller() {
43     log = Category.getInstance(getClass().getName());
44   }
45
46   /**
47    * Initializes the form filler with a given list of form handlers
48    *
49    * @param formHandlers a Vector containing FormHandler objects
50    */

51   public FormFiller(Vector JavaDoc formHandler) {
52     this();
53     this.formHandlers = formHandler;
54   }
55
56
57   /**
58    * Sets the list of form handlers
59    * @param formHandlers a Vector containing FormHandler objects
60    */

61   public void setFormHandlers(Vector JavaDoc formHandlers) {
62     this.formHandlers = formHandlers;
63   }
64
65   /**
66    * Gets the list of form handlers
67    * @return a Vector containing FormHandler objects
68    */

69   public Vector JavaDoc getFormHandlers() {
70     return this.formHandlers;
71   }
72
73
74
75   /**
76    * Tries to fill the given form with values
77    *
78    * @param baseURL the URL of the form itself. needed for relative adressing
79    * @param form a element node containing a DOM description of a form
80    * (e.g. from a DOM parser or HTML Tidy)
81    *
82    * @return a form filled with values or null, if no form handler was found
83    */

84   public ExtendedURL fillForm(URL JavaDoc baseURL, Element JavaDoc form) {
85     ExtendedURL eurl = new ExtendedURL();
86     String JavaDoc formURL = form.getAttribute("action");
87     String JavaDoc type = form.getAttribute("method");
88     FormHandler handler;
89     URL JavaDoc absoluteFormURL = null;
90
91     try {
92       absoluteFormURL = new URL JavaDoc(baseURL, formURL);
93     } catch( MalformedURLException JavaDoc e) {
94       log.info("MalformedURLException in fillForm(): "+e.getMessage());
95     }
96
97     if (! form.getNodeName().equals("form")) {
98       log.error("not a form !");
99       return null;
100     }
101
102     handler = getFormHandler(absoluteFormURL.toString());
103     if (handler == null) {
104       log.debug("found no form handler for URL "+formURL);
105       return null;
106     }
107
108     if (type.equalsIgnoreCase("get")) {
109       eurl.setRequestMethod(HttpConstants.GET);
110     } else if (type.equalsIgnoreCase("post")) {
111       eurl.setRequestMethod(HttpConstants.POST);
112     } else if (type.equals("")) {
113       // workaround for sites that have no "action" attribute
114
// in their forms, like Google :-(
115
eurl.setRequestMethod(HttpConstants.GET);
116     } else {
117       log.debug("method "+type+" unknown");
118       return null;
119     }
120
121     try {
122       eurl.setURL(absoluteFormURL);
123     } catch (Exception JavaDoc e) {
124       log.debug("error calculating URL: "+e.getMessage());
125     }
126
127     // clear the old data in this form handler
128
handler.clearValues();
129
130     // okay, now fill the form fields ...
131
collectInputFields(form, handler);
132     eurl.setParams(handler.getParamString());
133     
134     return eurl;
135   }
136
137
138   /**
139    * Get
140    */

141   private void collectInputFields(Element JavaDoc element, FormHandler fh) {
142     // this should not happen !
143
if (element==null) {
144       log.error("got a null element");
145       return;
146     }
147
148     if (element.getNodeName().equals("input")) {
149       
150       String JavaDoc type = element.getAttribute("type").toLowerCase();
151       String JavaDoc name = element.getAttribute("name");
152       String JavaDoc value = element.getAttribute("value");
153       
154       // ignore reset tags
155
if (! type.equals("reset")) {
156
157     // must have a name
158
if ((name != null) && (! name.equals(""))) {
159
160       // must have a value
161
if ((value != null) && (! value.equals(""))) {
162
163         // add this value to the form handler
164
fh.addValue(name, value);
165         
166       }
167
168     }
169
170       }
171
172     }
173
174
175     // recursive travel through all childs
176
NodeList JavaDoc childs = element.getChildNodes();
177
178     for (int i=0; i<childs.getLength(); i++) {
179       if (childs.item(i) instanceof Element JavaDoc) {
180     collectInputFields((Element JavaDoc)childs.item(i),fh);
181       }
182     }
183     
184   }
185
186
187   /**
188    * Gets a form handler for a given URL
189    * @param u an URL
190    * @return a FormHandler object, if there is a registered FormHandler
191    * for this URL, null otherwise
192    */

193   protected FormHandler getFormHandler(String JavaDoc url) {
194     if (url == null) {
195       return null;
196     }
197
198     if (formHandlers == null) {
199       return null;
200     }
201
202     for (int i=0; i<formHandlers.size(); i++) {
203       FormHandler fh = (FormHandler)formHandlers.elementAt(i);
204       if (fh.getUrl().toString().equals(url)) {
205     return fh;
206       }
207     }
208
209     return null;
210   }
211
212 }
213
Popular Tags