.CSV
file. The methods available for this purpose are:
{@link #getColumnLabels() String[] getColumnLabels()}
{@link #getColumnValues(Map) String[] getColumnValues(Map)}
{@link #getColumnValues() String[] getColumnValues()}
The {@link Util} class contains a method called {@link Util#outputCSVLine(Writer,String[]) outputCSVLine(Writer,String[])}which writes the String[]
output of these methods to the specified Writer
in .CSV
format.
The implementation of these methods makes use of certain properties in the {@link FormField} class that describe the structure of the data in each field.These properties can be utilised directly in the event that a form data set is to be converted from its normal format into some other type of data structure.
To access a specific {@link FormControl} from a FormFields
object, use:
formFields.
{@link #get(String) get(fieldName)}.
{@link FormField#getFormControl() getFormControl()}if the control is the only one with the specified {@linkplain FormControl#getName() name}, or formFields.
{@link #get(String) get(fieldName)}.
{@link FormField#getFormControl(String) getFormControl(predefinedValue)}to retrieve the control having the speficied {@linkplain FormControl#getPredefinedValue() predefined value}if it is part of a {@linkplain FormField field} containing multiple controls. The term field data set is used in this library to refer to a data structure consisting of a set of names (in lower case), each mapped to one or more values. Generally, this is represented by a data type of java.util.Map<String,String[]>
, with the keys (names) being of type String
and the values represented by an array containing one or more items of type String
. A field data set can be used to represent the data in an HTML form data set.
FormFields
instances are obtained using the {@link #FormFields(Collection formControls)} constructoror by calling the {@link Segment#getFormFields()} method.
The case sensitivity of form field names is determined by the static {@link Config#CurrentCompatibilityMode}.
{@link Config.CompatibilityMode#isFormFieldNameCaseInsensitive() FormFieldNameCaseInsensitive} property.
Examples:
ServletRequest
to a .CSV
file, and then display the form populated with this data: Source source=new Source(htmlTextOfOriginatingForm); FormFields formFields=source.getFormFields(); File csvOutputFile=new File("FormData.csv"); boolean outputHeadings=!csvOutputFile.exists(); Writer writer=new FileWriter(csvOutputFile,true); if (outputHeadings) Util.outputCSVLine(writer,formFields.getColumnLabels()); Util.outputCSVLine(writer,formFields.getColumnValues(servletRequest.getParameterMap())); writer.close(); formFields.setDataSet(servletRequest.getParameterMap()); OutputDocument outputDocument=new OutputDocument(source); outputDocument.replace(formFields); outputDocument.writeTo(servletResponse.getWriter());
See also the sample program FormFieldCSVOutput.
Source source=new Source(htmlText); Element myForm=null; List formElements=source.getAllElements(Tag.FORM); for (Iterator i=formElements.iterator(); i.hasNext();) { Element formElement=(Element)i.next(); String formName=formElement.getAttributes().getValue("name"); if ("MyForm".equals(formName)) { myForm=form; break; } } FormFields formFields=myForm.getFormFields(); formFields.clearValues(); // clear any values that might be set in the source document formFields.addValue("Name","Humphrey Bear"); formFields.addValue("MailingList","A"); formFields.addValue("MailingList","B"); formFields.addValue("FavouriteFare","honey"); OutputDocument outputDocument=new OutputDocument(source); outputDocument.replace(formFields); String newHtmlText=outputDocument.toString();
See also the sample program FormFieldSetValues.
Source source=new Source(htmlText); FormFields formFields=source.getFormFields(); // disable some controls: formFields.get("Password").getFormControl().setDisabled(true); FormField mailingListFormField=formFields.get("MailingList"); mailingListFormField.setValue("C"); mailingListFormField.getFormControl("C").setDisabled(true); mailingListFormField.getFormControl("D").setDisabled(true); // remove some controls: formFields.get("button1").getFormControl().setOutputStyle(FormControlOutputStyle.REMOVE); FormControl rhubarbFormControl=formFields.get("FavouriteFare").getFormControl("rhubarb"); rhubarbFormControl.setOutputStyle(FormControlOutputStyle.REMOVE); // set some controls to display value: formFields.setValue("Address","The Lodge\nDeakin ACT 2600\nAustralia"); formFields.get("Address").getFormControl().setOutputStyle(FormControlOutputStyle.DISPLAY_VALUE); FormField favouriteSportsFormField=formFields.get("FavouriteSports"); favouriteSportsFormField.setValue("BB"); favouriteSportsFormField.addValue("AFL"); favouriteSportsFormField.getFormControl().setOutputStyle(FormControlOutputStyle.DISPLAY_VALUE); OutputDocument outputDocument=new OutputDocument(source); outputDocument.replace(formFields); // adds all segments necessary to effect changes String newHtmlText=outputDocument.toString();
See also the sample program FormControlDisplayCharacteristics.
|
|
|
|
|
|
|
|
|
|