KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 import java.net.*;
8 import java.util.*;
9
10 /**
11  * This class implements a stores settings for web forms.
12  * It is used by the WebRobot to fill form field with
13  * predefined values.
14  *
15  * <b>FormHandler is not thread safe</b>. That means you can't
16  * use the same formHandler in different tasks with different addValue() calls,
17  * because there is only one internal array to store those values.
18  *
19  * @author Daniel Matuschek
20  * @version $Id $
21  */

22 public class FormHandler {
23
24   /**
25    * initializes a new FormHandler without any settings
26    */

27   public FormHandler() {
28     defaults=new Vector<FormField>();
29     clearValues();
30   }
31
32
33
34   /**
35    * add a new default value for this form handler
36    */

37   public void addDefault(String JavaDoc fieldname, String JavaDoc value) {
38     FormField ff = new FormField();
39     ff.setFieldname(fieldname);
40     ff.setValue(value);
41     defaults.add(ff);
42   }
43
44   /**
45    * add a new value for this form handler
46    */

47   public void addValue(String JavaDoc fieldname, String JavaDoc value) {
48     FormField ff = new FormField();
49     ff.setFieldname(fieldname);
50     ff.setValue(value);
51     values.add(ff);
52   }
53
54   /**
55    * if we have added values before, this function removed all
56    * values (e.g. to retrieve a new document with a different
57    * set of values)
58    */

59   public void clearValues() {
60     values = new Vector<FormField>();
61   }
62   
63   public String JavaDoc getUrl() {
64     //return url.toString();
65
return url;
66   }
67   
68   public void setUrl(String JavaDoc u)
69     throws MalformedURLException
70   {
71     //this.url=new URL(u);
72
this.url = u;
73   }
74
75
76   /**
77    * construct an encoded string of all attributes and their values
78    * to process this form
79    */

80   public String JavaDoc getParamString() {
81     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
82
83     // first, use the defaults
84
for (int i=0; i<defaults.size(); i++) {
85       FormField ff = (FormField)defaults.elementAt(i);
86       // default overidden by another value ?
87
if (! hasValue(ff.getFieldname())) {
88     sb.append(ff.toEncodedString());
89     sb.append('&');
90       }
91     }
92
93     // now add the values
94
for (int i=0; i<values.size(); i++) {
95       FormField ff = (FormField)values.elementAt(i);
96       sb.append(ff.toEncodedString());
97       sb.append('&');
98     }
99
100     // remove the last "&"
101
sb.deleteCharAt(sb.length()-1);
102
103     return sb.toString();
104   }
105  
106   /**
107    * look, if we have a value for this attribute or if
108    * we should use the default
109    */

110   protected boolean hasValue(String JavaDoc attrib) {
111     for (int i=0; i<values.size(); i++) {
112       FormField ff = (FormField)values.elementAt(i);
113       if (ff.getFieldname().equals(attrib)) {
114     return true;
115       }
116     }
117     return false;
118   }
119
120   public int getMethod() {
121     return method;
122   }
123
124   public void setMethod(int method) {
125     this.method = method;
126   }
127   
128   /**
129    * Get the value of defaults.
130    * @return Value of defaults.
131    */

132   public Vector getDefaults() {
133     return defaults;
134   }
135   
136   /**
137    * Set the value of defaults.
138    * @param v Value to assign to defaults.
139    */

140   public void setDefaults(Vector<FormField> v) {
141     this.defaults = v;
142   }
143
144
145   /**
146    * Get the value of values.
147    * @return Value of values.
148    */

149   public Vector getValues() {
150     return values;
151   }
152
153   
154   /**
155    * Set the value of values.
156    * @param v Value to assign to values.
157    */

158   public void setValues(Vector<FormField> v) {
159     this.values = v;
160   }
161   
162
163   /**
164    * GET or POST ?
165    */

166   private int method;
167
168
169   /**
170    *@link aggregation
171    * @associates <{FormField}>*/

172   private Vector<FormField> defaults;
173
174   // since not allways a full url is given, this solution is better.
175
// a set URL with "/search" would throw a MalformatException
176
private String JavaDoc url;
177   //private URL url;
178

179   /**
180    *@link aggregation
181    * @associates <{FormField}>*/

182   private Vector<FormField> values;
183 }
184
Popular Tags