Parent: [r5] (diff)

Child: [r23] (diff)

Download this file

Plugin.java    167 lines (141 with data), 5.9 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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-01T00:00:00Z
* LicenseName: NOASSERTION
* FileName: Plugin.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Used as the skeleton for beanshell/java hybrid
* plugins. This part is what beanshell is actually running, albeit in the
* NetBeans IDE and code of each script we are declaring each plugin
* to implement Plugin.java
*
* This trick was necessary to overcome some differences between Java and
* the Beanshell scripting" </text>
*/
package script;
import GUI.TreeNodeSPDX;
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;
/**
*
* @author Nuno Brito, 8th of November 2013 in Darmstadt, Germany.
*/
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 TreeNodeSPDX
thisNode = null; // our pointer for the treeview node (if used)
public void startup(){
}
public void main(WebRequest request){
request.setAnswer("<h1>In construction</h1>"
+ "There is nothing (yet) to be displayed on this page.");
}
/**
* Adds a tree node on the default User Interface when desired by the
* plugin
* @param title Title for the tree node that is shown to user
* @param iconName Icon from our archive that is used for the node
* @param methodName The method that is called when the node is clicked
*/
protected void addTreeNode(String title, String iconName,
String methodName){
// get the saved node on our temporary storage
TreeNodeSPDX nodeRoot = (TreeNodeSPDX) core.temp.get("nodeTree");
// add it up to our tree
thisNode = swingUtils.addNode(title, iconName, nodeRoot);
// set the associated script that we want to run
thisNode.scriptFile = thisFile;
thisNode.scriptFolder = thisFolder;
thisNode.scriptMethod = methodName;
// all done
log.write(is.COMPLETED, "Added tool: %1", title);
}
/**
* Adds a child node on the default User Interface when desired by the
* plugin
* @param title Title for the tree node that is shown to user
* @param iconName Icon from our archive that is used for the node
* @param methodName The method that is called when the node is clicked
*/
protected void addChildNode(String title, String iconName,
String methodName){
// we need that a root node exists
if(thisNode == null){
return;
}
// add it up
TreeNodeSPDX node = swingUtils.addNode(title, iconName, thisNode);
// set the associated script that we want to run
node.scriptFile = thisFile;
node.scriptFolder = thisFolder;
node.scriptMethod = methodName;
}
/**
* 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]);
}
}
}