⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webform.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: WebForm.java,v 1.102 2004/12/26 20:33:34 russgold Exp $** Copyright (c) 2000-2004, 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.NamedDelegate;import com.meterware.httpunit.scripting.ScriptableDelegate;import java.io.IOException;import java.io.File;import java.net.URL;import java.net.UnknownServiceException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Vector;import org.w3c.dom.Node;import org.xml.sax.SAXException;/** * This class represents a form in an HTML page. Users of this class may examine the parameters * defined for the form, the structure of the form (as a DOM), or the text of the form. They * may also create a {@link WebRequest} to simulate the submission of the form. * * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a> **/public class WebForm extends WebRequestSource {    private static final FormParameter UNKNOWN_PARAMETER = new FormParameter();    private Button[] _buttons;    /** Predicate to match a link's name. **/    public final static HTMLElementPredicate MATCH_NAME;    /**     * Submits this form using the web client from which it was originally obtained.     **/    public WebResponse submit() throws IOException, SAXException {        return submit( getDefaultButton() );    }    /**     * Submits this form using the web client from which it was originally obtained.     * Will usually return the result of that submission; however, if the submit button's 'onclick'     * or the form's 'onsubmit' event is triggered and     * inhibits the submission, will return the updated contents of the frame containing this form.     **/    public WebResponse submit( SubmitButton button ) throws IOException, SAXException {        return (button == null || button.doOnClickEvent()) ? doFormSubmit( button ) : getCurrentFrameContents();    }    /**     * Submits this form using the web client from which it was originally obtained.     * Will usually return the result of that submission; however, if the submit button's 'onclick'     * or the form's 'onsubmit' event is triggered and     * inhibits the submission, will return the updated contents of the frame containing this form.     * @since 1.6     **/    public WebResponse submit( SubmitButton button, int x, int y ) throws IOException, SAXException {        return button.doOnClickEvent() ? doFormSubmit( button, x, y ) : getCurrentFrameContents();    }    /**     * Submits this form using the web client from which it was originally obtained, ignoring any buttons defined for the form.     * @since 1.6     **/     public WebResponse submitNoButton() throws SAXException, IOException {        return submit( SubmitButton.createFakeSubmitButton( this /* fake */ ) );    }    protected WebResponse submitRequest( String event, WebRequest request ) throws IOException, SAXException {        try {            return super.submitRequest( event, request );        } catch (UnknownServiceException e) {            throw new UnsupportedActionException( "HttpUnit does not support " + request.getURL().getProtocol() + " URLs in form submissions" );        }    }    /**     * Submits the form without also invoking the button's "onclick" event.     */    WebResponse doFormSubmit( SubmitButton button ) throws IOException, SAXException {        return submitRequest( getAttribute( "onsubmit" ), getRequest( button ) );    }    WebResponse doFormSubmit( SubmitButton button, int x, int y ) throws IOException, SAXException {        return submitRequest( getAttribute( "onsubmit" ), getRequest( button, x, y ) );    }    /**     * Returns the method defined for this form.     **/    public String getMethod() {        return getAttribute( "method", "GET" );    }    /**     * Returns the action defined for this form.     **/    public String getAction() {        return getDestination();     }    /**     * Returns true if a parameter with given name exists in this form.     **/    public boolean hasParameterNamed( String soughtName ) {        return getFormParameters().containsKey( soughtName );    }    /**     * Returns true if a parameter starting with a given name exists,     **/    public boolean hasParameterStartingWithPrefix( String prefix ) {        String[] names = getParameterNames();        for (int i = 0; i < names.length; i++) {            if (names[i].startsWith( prefix )) return true;        }        return false;    }    /**     * Returns an array containing all of the buttons defined for this form.     **/    public Button[] getButtons() {        if (_buttons == null) {            FormControl[] controls = getFormControls();            ArrayList buttonList = new ArrayList();            for (int i = 0; i < controls.length; i++) {                FormControl control = controls[ i ];                if (control instanceof Button) buttonList.add( control );            }            _buttons = (Button[]) buttonList.toArray( new Button[ buttonList.size() ] );        }        return _buttons;    }    public Button getButton( HTMLElementPredicate predicate, Object criteria ) {        Button[] buttons = getButtons();        for (int i = 0; i < buttons.length; i++) {            if (predicate.matchesCriteria( buttons[i], criteria )) return buttons[i];        }        return null;    }    /**     * Convenience method which returns the button with the specified ID.     */    public Button getButtonWithID( String buttonID ) {        return getButton( Button.WITH_ID, buttonID );    }    /**     * Returns an array containing the submit buttons defined for this form.     **/    public SubmitButton[] getSubmitButtons() {        if (_submitButtons == null) {            Vector buttons = getSubmitButtonVector();            _submitButtons = new SubmitButton[ buttons.size() ];            buttons.copyInto( _submitButtons );        }        return _submitButtons;    }    /**     * Returns the submit button defined in this form with the specified name.     * If more than one such button exists, will return the first found.     * If no such button is found, will return null.     **/    public SubmitButton getSubmitButton( String name ) {        SubmitButton[] buttons = getSubmitButtons();        for (int i = 0; i < buttons.length; i++) {            if (buttons[i].getName().equals( name )) {                return buttons[i];            }        }        return null;    }    /**     * Returns the submit button defined in this form with the specified name and value.     * If more than one such button exists, will return the first found.     * If no such button is found, will return null.     **/    public SubmitButton getSubmitButton( String name, String value ) {        SubmitButton[] buttons = getSubmitButtons();        for (int i = 0; i < buttons.length; i++) {            if (buttons[i].getName().equals( name ) && buttons[i].getValue().equals( value )) {                return buttons[i];            }        }        return null;    }    /**     * Returns the submit button defined in this form with the specified ID.     * If more than one such button exists, will return the first found.     * If no such button is found, will return null.     **/    public SubmitButton getSubmitButtonWithID( String ID ) {        SubmitButton[] buttons = getSubmitButtons();        for (int i = 0; i < buttons.length; i++) {            if (buttons[i].getID().equals( ID )) {                return buttons[i];            }        }        return null;    }    /**     * Creates and returns a web request which will simulate the submission of this form with a button with the specified name and value.     **/    public WebRequest getRequest( String submitButtonName, String submitButtonValue ) {        SubmitButton sb = getSubmitButton( submitButtonName, submitButtonValue );        if (sb == null) throw new IllegalSubmitButtonException( submitButtonName, submitButtonValue );        return getRequest( sb );    }    /**     * Creates and returns a web request which will simulate the submission of this form with a button with the specified name.     **/    public WebRequest getRequest( String submitButtonName ) {        SubmitButton sb = getSubmitButton( submitButtonName );        if (sb == null) throw new IllegalSubmitButtonException( submitButtonName, "" );        return getRequest( sb );    }    /**     * Creates and returns a web request which will simulate the submission of this form by pressing the specified button.     * If the button is null, simulates the pressing of the default button.     **/    public WebRequest getRequest( SubmitButton button ) {        return getRequest( button, 0, 0 );    }    /**     * Creates and returns a web request which will simulate the submission of this form by pressing the specified button.     * If the button is null, simulates the pressing of the default button.     **/    public WebRequest getRequest( SubmitButton button, int x, int y ) {        if (button == null) button = getDefaultButton();        if (HttpUnitOptions.getParameterValuesValidated()) {            if (button == null) {                throw new IllegalUnnamedSubmitButtonException();            } else if (button.isFake()) {                // bypass checks            } else if (!getSubmitButtonVector().contains( button )) {                throw new IllegalSubmitButtonException( button );            } else if (button.isDisabled()) {                throw new DisabledSubmitButtonException( button );            }        }        SubmitButton[] buttons = getSubmitButtons();        for (int i = 0; i < buttons.length; i++) {            buttons[i].setPressed( false );        }        button.setPressed( true );        if (getMethod().equalsIgnoreCase( "post" )) {            return new PostMethodWebRequest( this, button, x, y );        } else {            return new GetMethodWebRequest( this, WebRequest.newParameterHolder( this ), button, x, y );        }    }    /**     * Creates and returns a web request which includes the specified button. If no button is specified, will include     * the default button, if any. No parameter validation will be done on the returned request and no scripts     * will be run when it is submitted.     **/    public WebRequest newUnvalidatedRequest( SubmitButton button ) {        return newUnvalidatedRequest( button, 0, 0 );    }    /**     * Creates and returns a web request which includes the specified button and position. If no button is specified,     * will include the default button, if any. No parameter validation will be done on the returned request     * and no scripts will be run when it is submitted.     **/    public WebRequest newUnvalidatedRequest( SubmitButton button, int x, int y ) {        if (button == null) button = getDefaultButton();        SubmitButton[] buttons = getSubmitButtons();        for (int i = 0; i < buttons.length; i++) {            buttons[i].setPressed( false );        }        button.setPressed( true );        if (getMethod().equalsIgnoreCase( "post" )) {            return new PostMethodWebRequest( this, new UncheckedParameterHolder( this ), button, x, y );        } else {            return new GetMethodWebRequest( this, new UncheckedParameterHolder( this ), button, x, y );        }    }    private WebRequest getScriptedSubmitRequest() {        SubmitButton[] buttons = getSubmitButtons();        for (int i = 0; i < buttons.length; i++) {            buttons[i].setPressed( false );        }        if (getMethod().equalsIgnoreCase( "post" )) {            return new PostMethodWebRequest( this );        } else {            return new GetMethodWebRequest( this );        }    }    /**     * Returns the default value of the named parameter.  If the parameter does not exist returns null.     **/    public String getParameterValue( String name ) {        String[] values = getParameterValues( name );        return values.length == 0 ? null : values[0];    }    /**     * Returns the displayed options defined for the specified parameter name.     **/    public String[] getOptions( String name ) {        return getParameter( name ).getOptions();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -