📄 formcontrol.java
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: FormControl.java,v 1.50 2004/12/26 20:33:34 russgold Exp $** Copyright (c) 2001-2003, Russell Gold** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including without limitation* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and* to permit persons to whom the Software is furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or substantial portions* of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER* DEALINGS IN THE SOFTWARE.********************************************************************************************************************/import com.meterware.httpunit.scripting.*;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;import java.util.Hashtable;import java.util.ArrayList;import java.util.List;import java.util.Arrays;import java.io.IOException;/** * Represents a control in an HTML form. * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> **/abstract class FormControl extends HTMLElementBase { final static String[] NO_VALUE = new String[0]; private String _valueAttribute; private final boolean _readOnly; private boolean _disabled; private final String _onChangeEvent; private final String _onClickEvent; private final WebForm _form; public static final String UNDEFINED_TYPE = "undefined"; public static final String BUTTON_TYPE = "button"; public static final String RESET_BUTTON_TYPE = "reset"; public static final String SUBMIT_BUTTON_TYPE = "submit"; public static final String IMAGE_BUTTON_TYPE = "image"; public static final String RADIO_BUTTON_TYPE = "radio"; public static final String CHECKBOX_TYPE = "checkbox"; public static final String TEXT_TYPE = "text"; public static final String PASSWORD_TYPE = "password"; public static final String HIDDEN_TYPE = "hidden"; public static final String TEXTAREA_TYPE = "textarea"; public static final String FILE_TYPE = "file"; public static final String SINGLE_TYPE = "select-one"; public static final String MULTIPLE_TYPE = "select-multiple"; /** * Return the type of the control, as seen from JavaScript. */ abstract public String getType(); static ScriptableDelegate newSelectionOption() { return new SelectionFormControl.Option(); } FormControl( WebForm form ) { super( newEmptyNode( form ) ); _form = form; _valueAttribute = ""; _readOnly = false; _disabled = false; _onChangeEvent = ""; _onClickEvent = ""; } private static Node newEmptyNode( WebForm form ) { return form.getNode().getOwnerDocument().createElement( "httpunit-supplied" ); } FormControl( WebForm form, Node node ) { super( node ); _form = form; _valueAttribute = NodeUtils.getNodeAttribute( node, "value" ); _readOnly = NodeUtils.isNodeAttributePresent( node, "readonly" ); _disabled = NodeUtils.isNodeAttributePresent( node, "disabled" ); _onChangeEvent = NodeUtils.getNodeAttribute( node, "onchange" ); _onClickEvent = NodeUtils.getNodeAttribute( node, "onclick" ); supportAttribute( "tabindex" ); supportAttribute( "disabled" ); } /** * Returns the current value(s) associated with this control. These values will be transmitted to the server * if the control is 'successful'. **/ abstract String[] getValues(); /** * Returns either a single delegate object or potentially an array of delegates as needed, given the form control. * This default implementation returns the scriptable delegate for the control. */ Object getDelegate() { return getScriptableDelegate(); } final protected WebForm getForm() { return _form; } protected ScriptableDelegate getParentDelegate() { return getForm().getScriptableDelegate(); } /** * Returns the values permitted in this control. Does not apply to text or file controls. **/ public String[] getOptionValues() { return NO_VALUE; } /** * Returns the list of values displayed by this control, if any. **/ String[] getDisplayedOptions() { return NO_VALUE; } /** * Returns true if this control is read-only. **/ boolean isReadOnly() { return _readOnly || _disabled; } /** * Returns true if this control is hidden. **/ boolean isHidden() { return false; } void setDisabled( boolean disabled ) { _disabled = disabled; } /** * Returns true if this control is disabled, meaning that it will not send a value to the server as part of a request. **/ boolean isDisabled() { return _disabled; } /** * Returns true if this control accepts free-form text. **/ boolean isTextControl() { return false; } /** * Returns true if only one control of this kind with this name can have a value. This is true for radio buttons. **/ boolean isExclusive() { return false; } /** * Returns true if a single control can have multiple values. **/ boolean isMultiValued() { return false; } /** * Returns true if this control accepts a file for upload. **/ boolean isFileParameter() { return false; } abstract void addValues( ParameterProcessor processor, String characterSet ) throws IOException; /** * Remove any required values for this control from the list, throwing an exception if they are missing. **/ void claimRequiredValues( List values ) { } /** * Sets this control to the next compatible value from the list, removing it from the list. **/ void claimValue( List values ) { } /** * Sets this control to the next compatible value from the list, removing it from the list. **/ void claimUniqueValue( List values ) { } /** * Specifies a file to be uploaded via this control. **/ void claimUploadSpecification( List files ) { } /** * Resets this control to its initial value. **/ void reset() { } /** * Toggles the value of this control. */ public void toggle() { throw new FormParameter.IllegalCheckboxParameterException( getName(), "toggleCheckbox" ); } /** * Sets the state of this boolean control. */ public void setState( boolean state ) { throw new FormParameter.IllegalCheckboxParameterException( getName(), "setCheckbox" ); } /** * Performs the 'onChange' event defined for this control. */ protected void sendOnChangeEvent() { if (_onChangeEvent.length() > 0) getScriptableDelegate().doEvent( _onChangeEvent ); } /** * Performs the 'onClick' event defined for this control. */ protected void sendOnClickEvent() { if (_onClickEvent.length() > 0) getScriptableDelegate().doEvent( _onClickEvent ); } /** * Creates and returns a scriptable object for this control. Subclasses should override this if they use a different * implementation of Scriptable. */ protected ScriptableDelegate newScriptable() { return new Scriptable(); } /** * Returns the default value of this control in the form. If no value is specified, defaults to the empty string. **/ protected String getValueAttribute() { return _valueAttribute; } /** * Removes the specified required value from the list of values, throwing an exception if it is missing. **/ final protected void claimValueIsRequired( List values, final String value ) { if (!values.contains( value )) throw new MissingParameterValueException( getName(), value, (String[]) values.toArray( new String[ values.size() ]) ); values.remove( value ); } static String[] getControlElementTags() { return new String[] { "textarea", "select", "button", "input" }; } static FormControl newFormParameter( WebForm form, Node node ) { if (node.getNodeType() != Node.ELEMENT_NODE) { return null; } else if (node.getNodeName().equalsIgnoreCase( "textarea" )) { return new TextAreaFormControl( form, node ); } else if (node.getNodeName().equalsIgnoreCase( "select" )) { return new SelectionFormControl( form, node ); } else if (node.getNodeName().equalsIgnoreCase( "button" )) { final String type = NodeUtils.getNodeAttribute( node, "type", SUBMIT_BUTTON_TYPE ); if (type.equalsIgnoreCase( SUBMIT_BUTTON_TYPE )) { return new SubmitButton( form, node ); } else if (type.equalsIgnoreCase( RESET_BUTTON_TYPE )) { return new ResetButton( form, node ); } else { return new Button( form, node ); } } else if (!node.getNodeName().equalsIgnoreCase( "input" )) { return null; } else { final String type = NodeUtils.getNodeAttribute( node, "type", TEXT_TYPE ); if (type.equalsIgnoreCase( TEXT_TYPE )) { return new TextFieldFormControl( form, node ); } else if (type.equalsIgnoreCase( PASSWORD_TYPE )) { return new PasswordFieldFormControl( form, node ); } else if (type.equalsIgnoreCase( HIDDEN_TYPE )) { return new HiddenFieldFormControl( form, node ); } else if (type.equalsIgnoreCase( RADIO_BUTTON_TYPE )) { return new RadioButtonFormControl( form, node ); } else if (type.equalsIgnoreCase( CHECKBOX_TYPE )) { return new CheckboxFormControl( form, node ); } else if (type.equalsIgnoreCase( SUBMIT_BUTTON_TYPE ) || type.equalsIgnoreCase( IMAGE_BUTTON_TYPE )) { return new SubmitButton( form, node ); } else if (type.equalsIgnoreCase( BUTTON_TYPE )) { return new Button( form, node ); } else if (type.equalsIgnoreCase( RESET_BUTTON_TYPE )) { return new ResetButton( form, node ); } else if (type.equalsIgnoreCase( FILE_TYPE )) { return new FileSubmitFormControl( form, node ); } else { return new TextFieldFormControl( form, node ); } } } class Scriptable extends HTMLElementScriptable implements Input { public String getName() { return FormControl.this.getName(); } public String getID() { return FormControl.this.getID(); } public Scriptable() { super( FormControl.this ); } public Object get( String propertyName ) { if (propertyName.equalsIgnoreCase( "name" )) { return FormControl.this.getName(); } else if (propertyName.equalsIgnoreCase( "type" )) { return FormControl.this.getType(); } else { return super.get( propertyName ); } } public void set( String propertyName, Object value ) { if (propertyName.equalsIgnoreCase( "value" )) { _valueAttribute = value.toString(); } else if (propertyName.equalsIgnoreCase( "disabled" )) { setDisabled( value instanceof Boolean && ((Boolean) value).booleanValue() ); } else { super.set( propertyName, value ); } } public void click() throws IOException, SAXException { } }}abstractclass BooleanFormControl extends FormControl { private boolean _isChecked; private String[] _value = new String[1]; private final boolean _isCheckedDefault; private String[] _displayedValue; protected ScriptableDelegate newScriptable() { return new Scriptable(); } class Scriptable extends FormControl.Scriptable { public Object get( String propertyName ) { if (propertyName.equalsIgnoreCase( "value" )) { return getQueryValue(); } else if (propertyName.equalsIgnoreCase( "checked" )) { return isChecked() ? Boolean.TRUE : Boolean.FALSE; } else if (propertyName.equalsIgnoreCase( "defaultchecked" )) { return _isCheckedDefault ? Boolean.TRUE : Boolean.FALSE; } else { return super.get( propertyName ); } } public void set( String propertyName, Object value ) { if (propertyName.equalsIgnoreCase( "checked" )) { setChecked( value instanceof Boolean && ((Boolean) value).booleanValue() ); } else { super.set( propertyName, value ); } } } public BooleanFormControl( WebForm form, Node node ) { super( form, node ); _displayedValue = new String[] { readDisplayedValue( node ) }; _isChecked = _isCheckedDefault = NodeUtils.isNodeAttributePresent( node, "checked" ); } private String readDisplayedValue( Node node ) { Node nextSibling = node.getNextSibling(); while (nextSibling != null && nextSibling.getNodeType() != Node.TEXT_NODE && nextSibling.getNodeType() != Node.ELEMENT_NODE) nextSibling = nextSibling.getNextSibling(); if (nextSibling == null || nextSibling.getNodeType() != Node.TEXT_NODE) return ""; return nextSibling.getNodeValue(); } boolean isChecked() { return _isChecked; } public void setChecked( boolean checked ) { _isChecked = checked; } void reset() { _isChecked = _isCheckedDefault; } /** * Returns the current value(s) associated with this control. These values will be transmitted to the server * if the control is 'successful'. **/ public String[] getValues() { return isChecked() ? toArray( getQueryValue() ) : NO_VALUE; } /** * Returns the values permitted in this control. **/ public String[] getOptionValues() { return (isReadOnly() && !isChecked()) ? NO_VALUE : toArray( getQueryValue() ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -