Switch to unified view

a/tool/src/script/Plugin.java b/tool/src/script/Plugin.java
...
...
20
20
21
import GUI.TreeNodeSPDX;
21
import GUI.TreeNodeSPDX;
22
import GUI.swingUtils;
22
import GUI.swingUtils;
23
import definitions.is;
23
import definitions.is;
24
import java.io.File;
24
import java.io.File;
25
import java.util.Enumeration;
25
import main.core;
26
import main.core;
27
import net.htmlparser.jericho.FormFields;
28
import net.htmlparser.jericho.OutputDocument;
29
import net.htmlparser.jericho.Source;
30
import utils.Settings;
26
import www.WebRequest;
31
import www.WebRequest;
27
32
28
   
33
   
29
/**
34
/**
30
 *
35
 *
31
 * @author Nuno Brito, 8th of November 2013 in Darmstadt, Germany.
36
 * @author Nuno Brito, 8th of November 2013 in Darmstadt, Germany.
32
 */
37
 */
33
public class Plugin {
38
public class Plugin {
34
39
40
    // where we will store and retrieve the settings for this page
41
    private final String settingsFileName = "settings.xml";
42
    private File settingsFile;
43
    protected Settings settings; 
44
   
45
    
35
     public File 
46
     public File 
36
             thisFile = null, // pointer to this beanshell file being executed
47
             thisFile = null, // pointer to this beanshell file being executed
37
             thisFolder = null; // pointer to the beanshell directory on disk
48
             thisFolder = null; // pointer to the beanshell directory on disk
38
49
50
     public Plugin(){
51
          settingsFile = new File(thisFolder, settingsFileName);
52
          settings = new Settings(settingsFile, "Settings for this folder");
53
     }
54
     
55
     
39
     public void startup(){
56
     public void startup(){
40
     }
57
     }
41
     
58
     
42
     public void main(WebRequest request){
59
     public void main(WebRequest request){
43
         request.setAnswer("<h1>In construction</h1>"
60
         request.setAnswer("<h1>In construction</h1>"
...
...
63
        node.scriptMethod = methodName;
80
        node.scriptMethod = methodName;
64
        
81
        
65
        // all done
82
        // all done
66
        log.write(is.COMPLETED, "Added tool: %1", title);
83
        log.write(is.COMPLETED, "Added tool: %1", title);
67
     }
84
     }
85
 
86
     
87
     /**
88
      * Loads a given template file from the local folder. This template file
89
      * will automatically inherit the saved values from the local settings
90
      * file.
91
      * @param templateFile An HTML template that we want to use
92
      * @param request The web request with the necessary information
93
      */
94
     protected void templateLoad(String templateFile, WebRequest request){
95
      //TODO what happens if settings file doesn't exist?
96
97
     // get our template page
98
       File templatePage = new File(thisFolder, templateFile);
99
       // place its contents inside a string
100
       String webText = utils.files.readAsString(templatePage);
101
       
102
        // instantiate our HTML manipulating class
103
        Source source = new net.htmlparser.jericho.Source(webText);
104
        // get all the data available right now on the form
105
        FormFields formFields = source.getFormFields();
106
        // go throught all the saved values and place them on the web page
107
        Enumeration e = settings.getAllKeys();
108
        // iterate throught all saved settings
109
        while (e.hasMoreElements()) {
110
            // current key
111
            String key = (String) e.nextElement();
112
            if (key.equals("")==false) {
113
                String value = settings.read(key);
114
                formFields.setValue(key, value);
115
            }
116
        }
117
       
118
        // save the output
119
        OutputDocument outputDocument = new OutputDocument(source);
120
        // adds all segments necessary to effect changes
121
        outputDocument.replace(formFields);
122
        String result = outputDocument.toString();
123
124
        File tempFile = new File(thisFolder, "temp.html");
125
        utils.files.SaveStringToFile(tempFile, result);
126
127
        request.setPage(tempFile);
128
     }
129
     
130
     /**
131
    * Save the settings to disk
132
    * @param request 
133
    */
134
    public void templateSave(WebRequest request){
135
        // get all parameters from our request, save them to disk
136
        for(String[] parameter : request.parameters){
137
            settings.write(parameter[0], parameter[1]);
138
        }
139
    }
68
     
140
     
69
}
141
}