1 21 24 package org.lobobrowser.html.domimpl; 25 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.html2.HTMLCollection; 33 import org.w3c.dom.html2.HTMLFormElement; 34 import org.lobobrowser.html.FormInput; 35 import org.lobobrowser.html.HtmlRendererContext; 36 import org.lobobrowser.html.js.*; 37 import org.mozilla.javascript.*; 38 39 public class HTMLFormElementImpl extends HTMLAbstractUIElement implements 40 HTMLFormElement { 41 public HTMLFormElementImpl(String name) { 42 super(name); 43 } 44 45 public HTMLFormElementImpl() { 46 super("FORM"); 47 } 48 49 public Object namedItem(final String name) { 50 try { 51 this.visit(new NodeVisitor() { 53 public void visit(Node node) { 54 if(HTMLFormElementImpl.isInput(node)) { 55 if(name.equals(((Element ) node).getAttribute("name"))) { 56 throw new StopVisitorException(node); 57 } 58 } 59 } 60 }); 61 } catch(StopVisitorException sve) { 62 return sve.getTag(); 63 } 64 return null; 65 } 66 67 public Object item(final int index) { 68 try { 69 this.visit(new NodeVisitor() { 70 private int current = 0; 71 72 public void visit(Node node) { 73 if(HTMLFormElementImpl.isInput(node)) { 74 if(this.current == index) { 75 throw new StopVisitorException(node); 76 } 77 this.current++; 78 } 79 } 80 }); 81 } catch(StopVisitorException sve) { 82 return sve.getTag(); 83 } 84 return null; 85 } 86 87 public HTMLCollection getElements() { 88 return new DescendentHTMLCollection(this, new InputFilter()); 89 } 90 91 public int getLength() { 92 return new DescendentHTMLCollection(this, new InputFilter()).getLength(); 93 } 94 95 public String getName() { 96 return this.getAttribute("name"); 97 } 98 99 public void setName(String name) { 100 this.setAttribute("name", name); 101 } 102 103 public String getAcceptCharset() { 104 return this.getAttribute("acceptCharset"); 105 } 106 107 public void setAcceptCharset(String acceptCharset) { 108 this.setAttribute("acceptCharset", acceptCharset); 109 } 110 111 public String getAction() { 112 return this.getAttribute("action"); 113 } 114 115 public void setAction(String action) { 116 this.setAttribute("action", action); 117 } 118 119 public String getEnctype() { 120 return this.getAttribute("enctype"); 121 } 122 123 public void setEnctype(String enctype) { 124 this.setAttribute("enctype", enctype); 125 } 126 127 public String getMethod() { 128 String method = this.getAttribute("method"); 129 if(method == null) { 130 method = "GET"; 131 } 132 return method; 133 } 134 135 public void setMethod(String method) { 136 this.setAttribute("method", method); 137 } 138 139 public String getTarget() { 140 return this.getAttribute("target"); 141 } 142 143 public void setTarget(String target) { 144 this.setAttribute("target", target); 145 } 146 147 public void submit() { 148 this.submit(null); 149 } 150 151 private Function onsubmit; 152 153 public void setOnsubmit(Function value) { 154 this.onsubmit = value; 155 } 156 157 public Function getOnsubmit() { 158 return this.getEventFunction(this.onsubmit, "onsubmit"); 159 } 160 161 167 public final void submit(final FormInput[] extraFormInputs) { 168 Function onsubmit = this.getOnsubmit(); 169 if(onsubmit != null) { 170 if(!Executor.executeFunction(this, onsubmit, null)) { 172 return; 173 } 174 } 175 HtmlRendererContext context = this.getHtmlRendererContext(); 176 if(context != null) { 177 final ArrayList formInputs = new ArrayList (); 178 if(extraFormInputs != null) { 179 for(int i = 0; i < extraFormInputs.length; i++) { 180 formInputs.add(extraFormInputs[i]); 181 } 182 } 183 this.visit(new NodeVisitor() { 184 public void visit(Node node) { 185 if(node instanceof HTMLElementImpl) { 186 FormInput[] fis = ((HTMLElementImpl) node).getFormInputs(); 187 if(fis != null) { 188 for(int i = 0; i < fis.length; i++) { 189 FormInput fi = fis[i]; 190 if(fi.getName() == null) { 191 throw new IllegalStateException ("Form input does not have a name: " + node); 192 } 193 formInputs.add(fi); 194 } 195 } 196 } 197 } 198 }); 199 FormInput[] fia = (FormInput[]) formInputs.toArray(FormInput.EMPTY_ARRAY); 200 String href = this.getAction(); 201 if(href == null) { 202 href = this.getBaseURI(); 203 } 204 try { 205 URL url = this.getFullURL(href); 206 context.submitForm(this.getMethod(), url, this.getTarget(), this.getEnctype(), fia); 207 } catch(MalformedURLException mfu) { 208 this.warn("submit()", mfu); 209 } 210 } 211 } 212 213 public void reset() { 214 this.visit(new NodeVisitor() { 215 public void visit(Node node) { 216 if(node instanceof HTMLBaseInputElement) { 217 ((HTMLBaseInputElement) node).resetInput(); 218 } 219 } 220 }); 221 } 222 223 static boolean isInput(Node node) { 224 String name = node.getNodeName().toLowerCase(); 225 return name.equals("input") || name.equals("textarea") || name.equals("select"); 226 } 227 228 private class InputFilter implements NodeFilter { 229 232 public boolean accept(Node node) { 233 return HTMLFormElementImpl.isInput(node); 234 } 235 } 236 } 237 | Popular Tags |