/** * Constructs a new <code>FormFields</code> object consisting of the specified {@linkplain FormControl form controls}. * @param formControls a collection of {@link FormControl} objects. * @see Segment#getFormFields() */ public FormFields(final Collection<FormControl> formControls) { // Passing "this" as a parameter inside a constructor used to cause some strange problems back in java 1.0, // but it seems to work here and there is no explicit mention in the Java language spec about any potential problems. // The alternative is an ugly static FormFields constructFrom(List formControls) method. for (FormControl formControl : formControls) { if (formControl.getName()!=null && formControl.getName().length()!=0) { formControl.addToFormFields(this); this.formControls.add(formControl); } } }
/** * Returns a list of the <a href="#FieldSubmissionValues">field submission values</a> in order of appearance. * <p> * The term <i><a name="FieldSubmissionValues">field submission values</a></i> is used in this library to refer to the aggregate of all the * <a href="FormControl.html#SubmissionValue">submission values</a> of a field's constituent {@linkplain #getFormControls() form controls}. * <p> * All objects in the returned list are of type <code>String</code>, with no <code>null</code> entries. * <p> * The list may contain duplicates if the this field has multiple controls with the same value. * * @return a list of the <a href="#FieldSubmissionValues">field submission values</a> in order of appearance, guaranteed not <code>null</code>. */ public List<String> getValues() { final List<String> values=new ArrayList<String>(); for (FormControl formControl : formControls) formControl.addValuesTo(values); return values; }
/** * Clears the <a href="FormControl.html#SubmissionValue">submission values</a> of all the constituent {@linkplain #getFormControls() form controls} in this field. * @see FormControl#clearValues() */ public void clearValues() { for (FormControl formControl : formControls) formControl.clearValues(); }
/** * Returns the constituent {@link FormControl} with the specified {@linkplain FormControl#getPredefinedValue() predefined value}. * <p> * Specifying a predefined value of <code>null</code> returns the first control without a predefined value. * * @param predefinedValue the predefined value of the control to be returned, or <code>null</code> to return the first control without a predefined value. * @return the constituent {@link FormControl} with the specified {@linkplain FormControl#getPredefinedValue() predefined value}, or <code>null</code> if none exists. * @see #getFormControl() * @see #getFormControls() */ public FormControl getFormControl(final String predefinedValue) { if (predefinedValue==null) { for (FormControl formControl : formControls) { if (!formControl.getFormControlType().hasPredefinedValue()) return formControl; if (formControl.getFormControlType().getElementName()!=HTMLElementName.SELECT && formControl.getPredefinedValue()==null) return formControl; } } else { for (FormControl formControl : formControls) { if (formControl.getFormControlType().getElementName()==HTMLElementName.SELECT) { if (formControl.getPredefinedValues().contains(predefinedValue)) return formControl; } else { if (predefinedValue.equals(formControl.getPredefinedValue())) return formControl; } } } return null; }
if (formControls.size()==1) return getFirstFormControl().addValue(value); List<FormControl> userValueControls=null; for (FormControl formControl : formControls) { if (!formControl.getFormControlType().hasPredefinedValue()) { continue; if (formControl.addValue(value)) return true; // return value of true from formControl.addValue(value) means the value was taken by the control if (userFormControl.addValue(value)) return true;
fieldAttributes.putAll(field.getFormControl().getAttributesMap()); fieldAttributes.put("Control Type", field.getFormControl().getFormControlType().name()); if (field.getFormControl().getFormControlType().isSubmit()) { List<String> submitFields = new ArrayList<String>(); for (String value : field.getPredefinedValues()){ if(field.getFormControl().getAttributesMap().containsKey("value")){ defaultValue = field.getFormControl().getAttributesMap().get("value");
private boolean calculateAllowsMultipleValues(final FormControl newFormControl) { // false if only one control (unless it is a multiple select with more than one option), // or all of the controls are radio buttons, or all of the controls are submit buttons if (allowsMultipleValues || userValueCount>1) return true; if (userValueCount==1) return predefinedValues!=null; // at this stage we know userValueCount==0 && predefinedValues.size()>=1 if (predefinedValues.size()==1) return false; final FormControlType newFormControlType=newFormControl.getFormControlType(); if (formControls.size()==1) return newFormControlType==FormControlType.SELECT_MULTIPLE; // at this stage we know there are multiple predefined values in multiple controls. // if all of the controls are radio buttons or all are submit buttons, allowsMultipleValues is false, otherwise true. // checking only the first control and the new control is equivalent to checking them all because if they weren't all // the same allowsMultipleValues would already be true. final FormControlType firstFormControlType=getFirstFormControl().getFormControlType(); if (newFormControlType==FormControlType.RADIO && firstFormControlType==FormControlType.RADIO) return false; if (newFormControlType.isSubmit() && firstFormControlType.isSubmit()) return false; return true; }
public int compare(final FormControl formControl1, final FormControl formControl2) { final int formControl1Begin=formControl1.getElement().getBegin(); final int formControl2Begin=formControl2.getElement().getBegin(); if (formControl1Begin<formControl2Begin) return -1; if (formControl1Begin>formControl2Begin) return 1; return 0; } }
/** * Returns a list of the {@link FormControl} objects that are {@linkplain #encloses(Segment) enclosed} by this segment. * @return a list of the {@link FormControl} objects that are {@linkplain #encloses(Segment) enclosed} by this segment. */ public List<FormControl> getFormControls() { return FormControl.getAll(this); }
/** * Returns the {@link FormControl} defined by this element. * @return the {@link FormControl} defined by this element, or <code>null</code> if it is not a <a target="_blank" href="http://www.w3.org/TR/html401/interact/forms.html#form-controls">control</a>. */ public FormControl getFormControl() { return FormControl.construct(this); }
/** * Returns a string representation of this object useful for debugging purposes. * @return a string representation of this object useful for debugging purposes. */ public String getDebugInfo() { final StringBuilder sb=new StringBuilder(); sb.append("Field: ").append(name).append(", UserValueCount=").append(userValueCount).append(", AllowsMultipleValues=").append(allowsMultipleValues); if (predefinedValues!=null) { for (String predefinedValue : predefinedValues) sb.append(Config.NewLine).append("PredefinedValue: ").append(predefinedValue); } for (FormControl formControl : formControls) sb.append(Config.NewLine).append("FormControl: ").append(formControl.getDebugInfo()); sb.append(Config.NewLine).append(Config.NewLine); return sb.toString(); }
if (field.getFormControl().getFormControlType().isSubmit()) { currentList = submitFields;
public String getDebugInfo() { final StringBuilder sb=new StringBuilder(); sb.append(formControlType); if (name!=null) sb.append(" name=\"").append(name).append('"'); if (elementContainer.predefinedValue!=null) sb.append(" PredefinedValue=\"").append(elementContainer.predefinedValue).append('"'); sb.append(" - ").append(getElement().getDebugInfo()); return sb.toString(); }
static List<FormControl> getAll(final Segment segment) { final ArrayList<FormControl> list=new ArrayList<FormControl>(); getAll(segment,list,HTMLElementName.INPUT); getAll(segment,list,HTMLElementName.TEXTAREA); getAll(segment,list,HTMLElementName.SELECT); getAll(segment,list,HTMLElementName.BUTTON); Collections.sort(list,COMPARATOR); return list; }
private void verifyName() { if (formControlType.isSubmit()) return; String missingOrBlank; if (name==null) return; if (name.length()!=0) return; final Source source=getElement().source; if (source.logger.isErrorEnabled()) source.logger.error(getElement().source.getRowColumnVector(getElement().begin).appendTo(new StringBuilder(200)).append(": compulsory \"name\" attribute of ").append(formControlType).append(" control is empty").toString()); }
/** * Clears the <a href="FormControl.html#SubmissionValue">submission values</a> of all the constituent {@linkplain #getFormControls() form controls}. * @see FormControl#clearValues() */ public void clearValues() { for (FormControl formControl : formControls) formControl.clearValues(); }
addValuesTo(values); return values;