001: package net.matuschek.html;
002:
003: /*********************************************
004: Copyright (c) 2001 by Daniel Matuschek
005: *********************************************/
006:
007: import java.net.*;
008: import java.util.*;
009:
010: /**
011: * This class implements a stores settings for web forms.
012: * It is used by the WebRobot to fill form field with
013: * predefined values.
014: *
015: * <b>FormHandler is not thread safe</b>. That means you can't
016: * use the same formHandler in different tasks with different addValue() calls,
017: * because there is only one internal array to store those values.
018: *
019: * @author Daniel Matuschek
020: * @version $Id $
021: */
022: public class FormHandler {
023:
024: /**
025: * initializes a new FormHandler without any settings
026: */
027: public FormHandler() {
028: defaults = new Vector<FormField>();
029: clearValues();
030: }
031:
032: /**
033: * add a new default value for this form handler
034: */
035: public void addDefault(String fieldname, String value) {
036: FormField ff = new FormField();
037: ff.setFieldname(fieldname);
038: ff.setValue(value);
039: defaults.add(ff);
040: }
041:
042: /**
043: * add a new value for this form handler
044: */
045: public void addValue(String fieldname, String value) {
046: FormField ff = new FormField();
047: ff.setFieldname(fieldname);
048: ff.setValue(value);
049: values.add(ff);
050: }
051:
052: /**
053: * if we have added values before, this function removed all
054: * values (e.g. to retrieve a new document with a different
055: * set of values)
056: */
057: public void clearValues() {
058: values = new Vector<FormField>();
059: }
060:
061: public String getUrl() {
062: //return url.toString();
063: return url;
064: }
065:
066: public void setUrl(String u) throws MalformedURLException {
067: //this.url=new URL(u);
068: this .url = u;
069: }
070:
071: /**
072: * construct an encoded string of all attributes and their values
073: * to process this form
074: */
075: public String getParamString() {
076: StringBuffer sb = new StringBuffer();
077:
078: // first, use the defaults
079: for (int i = 0; i < defaults.size(); i++) {
080: FormField ff = (FormField) defaults.elementAt(i);
081: // default overidden by another value ?
082: if (!hasValue(ff.getFieldname())) {
083: sb.append(ff.toEncodedString());
084: sb.append('&');
085: }
086: }
087:
088: // now add the values
089: for (int i = 0; i < values.size(); i++) {
090: FormField ff = (FormField) values.elementAt(i);
091: sb.append(ff.toEncodedString());
092: sb.append('&');
093: }
094:
095: // remove the last "&"
096: sb.deleteCharAt(sb.length() - 1);
097:
098: return sb.toString();
099: }
100:
101: /**
102: * look, if we have a value for this attribute or if
103: * we should use the default
104: */
105: protected boolean hasValue(String attrib) {
106: for (int i = 0; i < values.size(); i++) {
107: FormField ff = (FormField) values.elementAt(i);
108: if (ff.getFieldname().equals(attrib)) {
109: return true;
110: }
111: }
112: return false;
113: }
114:
115: public int getMethod() {
116: return method;
117: }
118:
119: public void setMethod(int method) {
120: this .method = method;
121: }
122:
123: /**
124: * Get the value of defaults.
125: * @return Value of defaults.
126: */
127: public Vector getDefaults() {
128: return defaults;
129: }
130:
131: /**
132: * Set the value of defaults.
133: * @param v Value to assign to defaults.
134: */
135: public void setDefaults(Vector<FormField> v) {
136: this .defaults = v;
137: }
138:
139: /**
140: * Get the value of values.
141: * @return Value of values.
142: */
143: public Vector getValues() {
144: return values;
145: }
146:
147: /**
148: * Set the value of values.
149: * @param v Value to assign to values.
150: */
151: public void setValues(Vector<FormField> v) {
152: this .values = v;
153: }
154:
155: /**
156: * GET or POST ?
157: */
158: private int method;
159:
160: /**
161: *@link aggregation
162: * @associates <{FormField}>*/
163: private Vector<FormField> defaults;
164:
165: // since not allways a full url is given, this solution is better.
166: // a set URL with "/search" would throw a MalformatException
167: private String url;
168: //private URL url;
169:
170: /**
171: *@link aggregation
172: * @associates <{FormField}>*/
173: private Vector<FormField> values;
174: }
|