1 package net.matuschek.html; 2 3 6 7 import java.net.*; 8 import java.util.*; 9 10 22 public class FormHandler { 23 24 27 public FormHandler() { 28 defaults=new Vector<FormField>(); 29 clearValues(); 30 } 31 32 33 34 37 public void addDefault(String fieldname, String value) { 38 FormField ff = new FormField(); 39 ff.setFieldname(fieldname); 40 ff.setValue(value); 41 defaults.add(ff); 42 } 43 44 47 public void addValue(String fieldname, String value) { 48 FormField ff = new FormField(); 49 ff.setFieldname(fieldname); 50 ff.setValue(value); 51 values.add(ff); 52 } 53 54 59 public void clearValues() { 60 values = new Vector<FormField>(); 61 } 62 63 public String getUrl() { 64 return url; 66 } 67 68 public void setUrl(String u) 69 throws MalformedURLException 70 { 71 this.url = u; 73 } 74 75 76 80 public String getParamString() { 81 StringBuffer sb = new StringBuffer (); 82 83 for (int i=0; i<defaults.size(); i++) { 85 FormField ff = (FormField)defaults.elementAt(i); 86 if (! hasValue(ff.getFieldname())) { 88 sb.append(ff.toEncodedString()); 89 sb.append('&'); 90 } 91 } 92 93 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 sb.deleteCharAt(sb.length()-1); 102 103 return sb.toString(); 104 } 105 106 110 protected boolean hasValue(String 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 132 public Vector getDefaults() { 133 return defaults; 134 } 135 136 140 public void setDefaults(Vector<FormField> v) { 141 this.defaults = v; 142 } 143 144 145 149 public Vector getValues() { 150 return values; 151 } 152 153 154 158 public void setValues(Vector<FormField> v) { 159 this.values = v; 160 } 161 162 163 166 private int method; 167 168 169 172 private Vector<FormField> defaults; 173 174 private String url; 177 179 182 private Vector<FormField> values; 183 } 184 | Popular Tags |