Child: [r23] (diff)

Download this file

FormControlDisplayCharacteristics.java    86 lines (70 with data), 4.3 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package experiment;
import net.htmlparser.jericho.*;
import java.util.*;
import java.io.*;
import java.net.*;
import main.core;
import utils.Settings;
public class FormControlDisplayCharacteristics {
public static void main(String[] args) throws Exception {
// where we will store and retrieve the settings for this page
String fileName = "settings.xml";
File thisFolder = new File(core.getPluginsFolder(), "/webserver");
File settingsFile = new File(thisFolder, fileName);
Settings settings = new Settings(settingsFile, "Settings for the "
+ "server");
String sourceUrlString="data/form.html";
File webPage = new File(thisFolder, "serverSettings.html");
sourceUrlString = webPage.getAbsolutePath();
String webText = utils.files.readAsString(webPage);
// instantiate our HTML manipulating class
Source source = new Source(webText);
FormFields formFields=source.getFormFields();
// go throught all the saved values and place them on the web page
Enumeration e = settings.getAllKeys();
// iterate throught all saved settings
while (e.hasMoreElements()) {
// current key
String key = (String) e.nextElement();
String value = settings.read(key);
formFields.setValue(key, value);
}
// disable some controls:
// formFields.get("Password").getFormControl().setDisabled(true);
// formFields.setValue("MailingList","C");
// formFields.get("MailingList").getFormControl("C").setDisabled(true);
// formFields.get("MailingList").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);
// formFields.setValue("FavouriteSports","BB");
// formFields.addValue("FavouriteSports","AFL");
//// formFields.get("FavouriteSports").getFormControl().setOutputStyle(FormControlOutputStyle.DISPLAY_VALUE);
// OutputDocument outputDocument=new OutputDocument(source);
// outputDocument.replace(formFields); // adds all segments necessary to effect changes
// also need to remove label for the removed "rhubarb" radio button:
// label segment begins at the end of the rhubarb control, and ends at the start of the next control:
// Segment rhubarbLabelSegment=new Segment(source,rhubarbFormControl.getEnd(),source.getNextTag(rhubarbFormControl.getEnd()).getBegin());
// outputDocument.remove(rhubarbLabelSegment);
// also need to remove instructions for favourite sports control which has been set to output display value:
// Segment instructionsSegment=source.getFirstElement("class","instructions",false).getContent();
// outputDocument.replace(instructionsSegment,"A comma separated list of favourite sports is shown above");
//
// // modify stylesheet link since the output file is in a different directory to the input file
// int cssPathPos=source.toString().indexOf("main.css");
// outputDocument.insert(cssPathPos,"data/");
// Writer out=new FileWriter("NewForm.html");
OutputDocument outputDocument = new OutputDocument(source);
outputDocument.replace(formFields); // adds all segments necessary to effect changes
String result = outputDocument.toString();
System.out.println(result);
// outputDocument.writeTo(out);
// out.close();
System.err.println("\nThe form containing new default values has been output to NewForm.html");
System.err.println("This will open automatically in a web browser after you press a key.");
}
}