--- a/tool/src/script/Plugin.java
+++ b/tool/src/script/Plugin.java
@@ -22,7 +22,12 @@
import GUI.swingUtils;
import definitions.is;
import java.io.File;
+import java.util.Enumeration;
import main.core;
+import net.htmlparser.jericho.FormFields;
+import net.htmlparser.jericho.OutputDocument;
+import net.htmlparser.jericho.Source;
+import utils.Settings;
import www.WebRequest;
@@ -32,10 +37,22 @@
*/
public class Plugin {
+ // where we will store and retrieve the settings for this page
+ private final String settingsFileName = "settings.xml";
+ private File settingsFile;
+ protected Settings settings;
+
+
public File
thisFile = null, // pointer to this beanshell file being executed
thisFolder = null; // pointer to the beanshell directory on disk
+ public Plugin(){
+ settingsFile = new File(thisFolder, settingsFileName);
+ settings = new Settings(settingsFile, "Settings for this folder");
+ }
+
+
public void startup(){
}
@@ -65,5 +82,60 @@
// all done
log.write(is.COMPLETED, "Added tool: %1", title);
}
+
+
+ /**
+ * Loads a given template file from the local folder. This template file
+ * will automatically inherit the saved values from the local settings
+ * file.
+ * @param templateFile An HTML template that we want to use
+ * @param request The web request with the necessary information
+ */
+ protected void templateLoad(String templateFile, WebRequest request){
+ //TODO what happens if settings file doesn't exist?
+
+ // get our template page
+ File templatePage = new File(thisFolder, templateFile);
+ // place its contents inside a string
+ String webText = utils.files.readAsString(templatePage);
+
+ // instantiate our HTML manipulating class
+ Source source = new net.htmlparser.jericho.Source(webText);
+ // get all the data available right now on the form
+ 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();
+ if (key.equals("")==false) {
+ String value = settings.read(key);
+ formFields.setValue(key, value);
+ }
+ }
+
+ // save the output
+ OutputDocument outputDocument = new OutputDocument(source);
+ // adds all segments necessary to effect changes
+ outputDocument.replace(formFields);
+ String result = outputDocument.toString();
+
+ File tempFile = new File(thisFolder, "temp.html");
+ utils.files.SaveStringToFile(tempFile, result);
+
+ request.setPage(tempFile);
+ }
+
+ /**
+ * Save the settings to disk
+ * @param request
+ */
+ public void templateSave(WebRequest request){
+ // get all parameters from our request, save them to disk
+ for(String[] parameter : request.parameters){
+ settings.write(parameter[0], parameter[1]);
+ }
+ }
}