[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit FormControl.java,NONE,1.1 WebForm.java,1.
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-01-01 19:29:10
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv5993/src/com/meterware/httpunit
Modified Files:
WebForm.java
Added Files:
FormControl.java
Log Message:
More refactoring
***** Error reading new file[Errno 2] No such file or directory: 'FormControl.java'
Index: WebForm.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- WebForm.java 2001/12/11 21:42:34 1.34
+++ WebForm.java 2002/01/01 19:29:07 1.35
@@ -23,6 +23,7 @@
import java.util.Hashtable;
import java.util.Vector;
+import java.util.ArrayList;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
@@ -70,12 +71,6 @@
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].getName().equals( soughtName )) return true;
}
-
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- if (soughtName.equals( selections[ i ].getName() )) return true;
- }
-
return false;
}
@@ -89,12 +84,6 @@
String name = parameters[i].getName();
if (name.startsWith( prefix )) return true;
}
-
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- if (selections[ i ].getName().startsWith( prefix )) return true;
- }
-
return false;
}
@@ -112,11 +101,6 @@
if (!parameterNames.contains( name )) parameterNames.addElement( name );
}
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- parameterNames.addElement( selections[i].getName() );
- }
-
String[] result = new String[ parameterNames.size() ];
parameterNames.copyInto( result );
return result;
@@ -283,18 +267,14 @@
}
}
- NamedNodeMap nnm = getNode().getAttributes();
String action = getAction();
- if (action.trim().length() == 0) {
- action = getBaseURL().getFile();
- }
-
- WebRequest result;
+ if (action.trim().length() == 0) action = getBaseURL().getFile();
- if (getValue( nnm.getNamedItem( "method" ) ).equalsIgnoreCase( "post" )) {
- result = new PostMethodWebRequest( getBaseURL(), action, getTarget(), this, button );
+ WebRequest request;
+ if (getMethod().equalsIgnoreCase( "post" )) {
+ request = new PostMethodWebRequest( getBaseURL(), action, getTarget(), this, button );
} else {
- result = new GetMethodWebRequest( getBaseURL(), action, getTarget(), this, button );
+ request = new GetMethodWebRequest( getBaseURL(), action, getTarget(), this, button );
}
String[] parameterNames = getParameterNames();
@@ -302,14 +282,14 @@
if (parameterNames[i].length() > 0 && getParameterDefaults().get( parameterNames[i] ) != null) {
Object value = getParameterDefaults().get( parameterNames[i] );
if (value instanceof String) {
- result.setParameter( parameterNames[i], (String) value);
+ request.setParameter( parameterNames[i], (String) value);
} else {
- result.setParameter( parameterNames[i], (String[]) value);
+ request.setParameter( parameterNames[i], (String[]) value);
}
}
}
- result.setHeaderField( "Referer", getBaseURL().toExternalForm() );
- return result;
+ request.setHeaderField( "Referer", getBaseURL().toExternalForm() );
+ return request;
}
@@ -359,7 +339,7 @@
* Returns true if the named parameter accepts multiple values.
**/
public boolean isMultiValuedParameter( String name ) {
- return TYPE_MULTI_VALUED.equals( getParameterTypes().get( name ) );
+ return FormControl.TYPE_MULTI_VALUED.equals( getParameterTypes().get( name ) );
}
@@ -367,7 +347,7 @@
* Returns true if the named parameter accepts free-form text.
**/
public boolean isTextParameter( String name ) {
- return TYPE_TEXT.equals( getParameterTypes().get( name ) );
+ return FormControl.TYPE_TEXT.equals( getParameterTypes().get( name ) );
}
@@ -382,7 +362,14 @@
private Hashtable getTextParameterCounts() {
- getParameterTypes();
+ if (_textParameterCounts == null) {
+ Hashtable parameterCounts = new Hashtable();
+ FormControl[] parameters = getParameters();
+ for (int i = 0; i < parameters.length; i++) {
+ parameters[i].updateTextParameterCounts( parameterCounts );
+ }
+ _textParameterCounts = parameterCounts;
+ }
return _textParameterCounts;
}
@@ -391,7 +378,7 @@
* Returns true if the named parameter accepts files for upload.
**/
public boolean isFileParameter( String name ) {
- return TYPE_FILE.equals( getParameterTypes().get( name ) );
+ return FormControl.TYPE_FILE.equals( getParameterTypes().get( name ) );
}
@@ -428,22 +415,6 @@
//---------------------------------- private members --------------------------------
- /** The type of a parameter which accepts any text. **/
- private final static Integer TYPE_TEXT = new Integer(1);
-
- /** The type of a parameter which accepts single predefined values. **/
- private final static Integer TYPE_SCALAR = new Integer(2);
-
- /** The type of a parameter which accepts multiple predefined values. **/
- private final static Integer TYPE_MULTI_VALUED = new Integer(3);
-
- /** The type of a parameter which accepts files for upload. **/
- private final static Integer TYPE_FILE = new Integer(4);
-
- /** A constant set to indicate one parameter. **/
- private final static Integer ONE_PARAMETER = new Integer(1);
-
-
/** The attributes of the form parameters. **/
private FormControl[] _parameters;
@@ -465,50 +436,16 @@
/** The submit buttons in this form. **/
private SubmitButton[] _submitButtons;
- /** The selections in this form. **/
- private HTMLSelectElement[] _selections;
-
/** The character set in which the form will be submitted. **/
private String _characterSet;
- private String getValue( Node node ) {
- return (node == null) ? "" : emptyIfNull( node.getNodeValue() );
- }
-
-
- private String emptyIfNull( String value ) {
- return (value == null) ? "" : value;
- }
-
-
private Hashtable getParameterDefaults() {
if (_defaults == null) {
FormControl[] parameters = getParameters();
Hashtable defaults = new Hashtable();
for (int i = 0; i < parameters.length; i++) {
- String name = parameters[i].getName();
- String value = parameters[i].getValue();
- String type = getValue( parameters[i].getNamedItem( "type" ) ).toUpperCase();
- if (type == null || type.length() == 0) type = "TEXT";
-
- if (type.equals( "TEXT" ) || type.equals( "HIDDEN" ) || type.equals( "PASSWORD" )) {
- defaults.put( name, value );
- } else if (type.equals( "RADIO" ) && parameters[i].getNamedItem( "checked" ) != null) {
- defaults.put( name, value );
- } else if (type.equals( "CHECKBOX" ) && parameters[i].getNamedItem( "checked" ) != null) {
- if (value.length() == 0) value = "on";
- String[] currentDefaults = (String[]) defaults.get( name );
- if (currentDefaults == null) {
- defaults.put( name, new String[] { value } );
- } else {
- defaults.put( name, withNewValue( currentDefaults, value ) );
- }
- }
- }
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- defaults.put( selections[i].getName(), selections[i].getSelected() );
+ parameters[i].updateParameterDefaults( defaults );
}
_defaults = defaults;
}
@@ -522,29 +459,7 @@
FormControl[] parameters = getParameters();
Hashtable required = new Hashtable();
for (int i = 0; i < parameters.length; i++) {
- String name = parameters[i].getName();
- String value = parameters[i].getValue();
- String type = getValue( parameters[i].getNamedItem( "type" ) ).toUpperCase();
- if (type == null || type.length() == 0) type = "TEXT";
- if (!parameters[i].isReadOnly()) continue;
-
- if (type.equals( "TEXT" ) || type.equals( "HIDDEN" ) || type.equals( "PASSWORD" )) {
- required.put( name, value );
- } else if (type.equals( "RADIO" ) && parameters[i].getNamedItem( "checked" ) != null) {
- required.put( name, value );
- } else if (type.equals( "CHECKBOX" ) && parameters[i].getNamedItem( "checked" ) != null) {
- if (value.length() == 0) value = "on";
- String[] currentDefaults = (String[]) required.get( name );
- if (currentDefaults == null) {
- required.put( name, new String[] { value } );
- } else {
- required.put( name, withNewValue( currentDefaults, value ) );
- }
- }
- }
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- if (selections[i].isDisabled()) required.put( selections[i].getName(), selections[i].getSelected() );
+ parameters[i].updateRequiredValues( required );
}
_required = required;
}
@@ -555,9 +470,9 @@
private Hashtable getParameterOptions() {
if (_options == null) {
Hashtable options = new Hashtable();
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- options.put( selections[i].getName(), selections[i].getOptions() );
+ FormControl[] parameters = getParameters();
+ for (int i = 0; i < parameters.length; i++) {
+ parameters[i].updateParameterOptions( options );
}
_options = options;
}
@@ -570,26 +485,7 @@
Hashtable options = new Hashtable();
FormControl[] parameters = getParameters();
for (int i = 0; i < parameters.length; i++) {
- String name = parameters[i].getName();
- String value = parameters[i].getValue();
- String type = getValue( parameters[i].getNamedItem( "type" ) ).toUpperCase();
- if (type == null || type.length() == 0) type = "TEXT";
-
- if ((type.equals( "RADIO" ) || type.equals( "CHECKBOX" ))
- && (!parameters[i].isReadOnly()
- || parameters[i].getNamedItem( "checked" ) != null)) {
- if (value.length() == 0 && type.equals( "CHECKBOX" )) value = "on";
- String[] radioOptions = (String[]) options.get( name );
- if (radioOptions == null) {
- options.put( name, new String[] { value } );
- } else {
- options.put( name, withNewValue( radioOptions, value ) );
- }
- }
- }
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- options.put( selections[i].getName(), selections[i].getOptionValues() );
+ parameters[i].updateParameterOptionValues( options );
}
_optionValues = options;
}
@@ -597,46 +493,12 @@
}
- /**
- * Adds a string to an array of strings and returns the result.
- **/
- private String[] withNewValue( String[] group, String value ) {
- String[] result = new String[ group.length+1 ];
- System.arraycopy( group, 0, result, 0, group.length );
- result[ group.length ] = value;
- return result;
- }
-
-
private Hashtable getParameterTypes() {
if (_dataTypes == null) {
FormControl[] parameters = getParameters();
Hashtable types = new Hashtable();
- _textParameterCounts = new Hashtable();
for (int i = 0; i < parameters.length; i++) {
- String name = parameters[i].getName();
- String type = getValue( parameters[i].getNamedItem( "type" ) ).toUpperCase();
- if (type == null || type.length() == 0) type = "TEXT";
-
- if (type.equals( "RADIO" )) {
- types.put( name, TYPE_SCALAR );
- } else if (type.equals( "CHECKBOX" )) {
- types.put( name, TYPE_MULTI_VALUED );
- } else if (type.equals( "FILE" )) {
- types.put( name, TYPE_FILE );
- } else if (type.equals( "TEXT" ) || type.equals( "HIDDEN" ) || type.equals( "PASSWORD" )) {
- types.put( name, TYPE_TEXT );
- Integer oldCount = (Integer) _textParameterCounts.get( name );
- if (oldCount == null) {
- _textParameterCounts.put( name, ONE_PARAMETER );
- } else {
- _textParameterCounts.put( name, new Integer( oldCount.intValue() + 1 ) );
- }
- }
- }
- HTMLSelectElement[] selections = getSelections();
- for (int i = 0; i < selections.length; i++) {
- types.put( selections[i].getName(), selections[i].isMultiSelect() ? TYPE_MULTI_VALUED : TYPE_SCALAR );
+ parameters[i].updateParameterTypes( types );
}
_dataTypes = types;
}
@@ -647,21 +509,6 @@
/**
* Returns an array of select control descriptors for this form.
**/
- private HTMLSelectElement[] getSelections() {
- if (_selections == null) {
- NodeList nl = ((Element) getNode()).getElementsByTagName( "select" );
- HTMLSelectElement[] result = new HTMLSelectElement[ nl.getLength() ];
- for (int i = 0; i < result.length; i++) {
- result[i] = new HTMLSelectElement( nl.item(i) );
- }
- _selections = result;
- }
- return _selections;
- }
-
- /**
- * Returns an array of select control descriptors for this form.
- **/
/**
* Returns an array of form parameter attributes for this form.
**/
@@ -686,180 +533,27 @@
private void addFormParametersToList( Node child, Vector list ) {
final FormControl formParameter = FormControl.newFormParameter( child );
- if (formParameter != null) {
+ if (formParameter != null) {
list.addElement( formParameter );
} else if (child.hasChildNodes()) {
addFormParametersToList( child.getChildNodes(), list );
}
}
-
- class HTMLSelectElement {
- HTMLSelectElement( Node node ) {
- if (!node.getNodeName().equalsIgnoreCase( "select" )) {
- throw new RuntimeException( "Not a select element" );
- }
- _node = node;
- }
-
-
- private Node _node;
-
-
- String getName() {
- NamedNodeMap nnm = _node.getAttributes();
- return getValue( nnm.getNamedItem( "name" ) );
- }
-
-
- String[] getSelected() {
- Vector selected = new Vector();
- NodeList nl = ((Element) _node).getElementsByTagName( "option" );
- for (int i = 0; i < nl.getLength(); i++) {
- if (nl.item(i).getAttributes().getNamedItem( "selected" ) != null) {
- selected.addElement( getOptionValue( nl.item(i) ) );
- }
- }
-
- if (!isMultiSelect() && selected.size() == 0 && nl.getLength() > 0) {
- selected.addElement( getOptionValue( nl.item(0) ) );
- }
-
- String[] result = new String[ selected.size() ];
- selected.copyInto( result );
- return result;
- }
-
-
- String[] getOptions() {
- Vector options = new Vector();
- NodeList nl = ((Element) _node).getElementsByTagName( "option" );
- for (int i = 0; i < nl.getLength(); i++) {
- options.addElement( getValue( nl.item(i).getFirstChild() ) );
- }
- String[] result = new String[ options.size() ];
- options.copyInto( result );
- return result;
- }
-
-
- String[] getOptionValues() {
- Vector options = new Vector();
- NodeList nl = ((Element) _node).getElementsByTagName( "option" );
- for (int i = 0; i < nl.getLength(); i++) {
- options.addElement( getOptionValue( nl.item(i) ) );
- }
- String[] result = new String[ options.size() ];
- options.copyInto( result );
- return result;
- }
-
-
- boolean isMultiSelect() {
- return _node.getAttributes().getNamedItem( "multiple" ) != null;
- }
-
-
- boolean isDisabled() {
- return _node.getAttributes().getNamedItem( "disabled" ) != null;
- }
-
-
- private String getOptionValue( Node optionNode ) {
- NamedNodeMap nnm = optionNode.getAttributes();
- if (nnm.getNamedItem( "value" ) != null) {
- return getValue( nnm.getNamedItem( "value" ) );
- } else {
- return getValue( optionNode.getFirstChild() );
- }
- }
-
-
- }
-
}
-
-class FormControl {
- protected Node _node;
+class FormParameter {
-
- FormControl( Node node ) {
- _node = node;
+ void addControl( FormControl control ) {
+ _controls.add( control )
}
- Node getNamedItem( String attributeName ) {
- return _node.getAttributes().getNamedItem( attributeName );
- }
-
-
- /**
- * Returns the name of this control. If no name is specified, defaults to the empty string.
- **/
- String getName() {
- return NodeUtils.getNodeAttribute( _node, "name" );
- }
-
-
- /**
- * Returns the value of this control. If no value is specified, defaults to the empty string.
- **/
- String getValue() {
- return NodeUtils.getNodeAttribute( _node, "value" );
- }
-
-
- /**
- * Returns true if this control is read-only.
- **/
- boolean isReadOnly() {
- return _node.getAttributes().getNamedItem( "readonly" ) != null;
- }
-
-
- static FormControl newFormParameter( Node node ) {
- if (node.getNodeType() != Node.ELEMENT_NODE) {
- return null;
- } else if (node.getNodeName().equals( "textarea" )) {
- return new TextAreaFormControl( node );
- } else if (!node.getNodeName().equals( "input" )) {
- return null;
- } else {
- NamedNodeMap nnm = node.getAttributes();
- Node n = nnm.getNamedItem( "type" );
- if (n == null) {
- return new FormControl( node );
- } else if (n.getNodeValue().equalsIgnoreCase( "submit" )) {
- return null;
- } else if (n.getNodeValue().equalsIgnoreCase( "reset" )) {
- return null;
- } else {
- return new FormControl( node );
- }
- }
- }
-
+ private ArrayList _controls = new ArrayList();
}
-class TextAreaFormControl extends FormControl {
-
- public TextAreaFormControl( Node node ) {
- super( node );
-
- if (!node.getNodeName().equalsIgnoreCase( "textarea" )) {
- throw new RuntimeException( "Not a textarea element" );
- }
- }
-
-
- String getValue() {
- return NodeUtils.asText( _node.getChildNodes() );
- }
-
-}
//============================= exception class IllegalUnnamedSubmitButtonException ======================================
|