1 package net.matuschek.html; 2 3 6 7 8 import java.net.MalformedURLException ; 9 import java.net.URL ; 10 import java.util.Vector ; 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 ; 18 import org.w3c.dom.NodeList ; 19 20 26 public class FormFiller { 27 28 33 private Vector formHandlers = null; 34 35 36 private Category log; 37 38 39 42 public FormFiller() { 43 log = Category.getInstance(getClass().getName()); 44 } 45 46 51 public FormFiller(Vector formHandler) { 52 this(); 53 this.formHandlers = formHandler; 54 } 55 56 57 61 public void setFormHandlers(Vector formHandlers) { 62 this.formHandlers = formHandlers; 63 } 64 65 69 public Vector getFormHandlers() { 70 return this.formHandlers; 71 } 72 73 74 75 84 public ExtendedURL fillForm(URL baseURL, Element form) { 85 ExtendedURL eurl = new ExtendedURL(); 86 String formURL = form.getAttribute("action"); 87 String type = form.getAttribute("method"); 88 FormHandler handler; 89 URL absoluteFormURL = null; 90 91 try { 92 absoluteFormURL = new URL (baseURL, formURL); 93 } catch( MalformedURLException 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 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 e) { 124 log.debug("error calculating URL: "+e.getMessage()); 125 } 126 127 handler.clearValues(); 129 130 collectInputFields(form, handler); 132 eurl.setParams(handler.getParamString()); 133 134 return eurl; 135 } 136 137 138 141 private void collectInputFields(Element element, FormHandler fh) { 142 if (element==null) { 144 log.error("got a null element"); 145 return; 146 } 147 148 if (element.getNodeName().equals("input")) { 149 150 String type = element.getAttribute("type").toLowerCase(); 151 String name = element.getAttribute("name"); 152 String value = element.getAttribute("value"); 153 154 if (! type.equals("reset")) { 156 157 if ((name != null) && (! name.equals(""))) { 159 160 if ((value != null) && (! value.equals(""))) { 162 163 fh.addValue(name, value); 165 166 } 167 168 } 169 170 } 171 172 } 173 174 175 NodeList childs = element.getChildNodes(); 177 178 for (int i=0; i<childs.getLength(); i++) { 179 if (childs.item(i) instanceof Element ) { 180 collectInputFields((Element )childs.item(i),fh); 181 } 182 } 183 184 } 185 186 187 193 protected FormHandler getFormHandler(String 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 |