Child: [r24] (diff)

Download this file

show.java    303 lines (263 with data), 10.7 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (http://triplecheck.de)
* Created: 2013-12-15T00:00:00Z
* LicenseName: NOASSERTION
* FileName: show.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright (c) 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Allows handling the details with people inside SPDX
* documents. </text>
*/
package people;
import definitions.is;
import java.io.File;
import main.core;
import main.param;
import net.htmlparser.jericho.FormFields;
import net.htmlparser.jericho.OutputDocument;
import net.htmlparser.jericho.Source;
import script.Plugin;
import script.log;
import spdxlib.Person;
import spdxlib.SPDXfile;
import spdxlib.TagValue;
import utils.html;
import www.WebRequest;
/**
*
* @author Nuno Brito, 15th of December 2013 in Darsmtadt, Germany
*/
public class show extends Plugin{
String
name = "name",
email = "email",
organization = "organization",
organizationEmail = "organizationemail",
tool = "tool",
toolVersion = "toolversion",
sourceTag = "source";
/**
* Show the details about a given person
* @param request
* @expectedparameters: param.SPDX, "person"
*/
public void details(WebRequest request){
// get our template file
File page = new File(thisFolder, "person.html");
// create the template
templateCreate(request, page);
}
/**
* Change the details on the template page with the details of the person
* that was currently selected
*/
private void templateCreate(WebRequest request, File templatePage){
// we need the "file" parameter to tell us what to detail
String spdxTarget = request.getParameter(param.spdx);
// does this file exists?
File file = getFile(spdxTarget, request);
if(file == null){
return;
}
// get the SPDX file from the root node
SPDXfile spdx = new SPDXfile(file);
// get the filter, from where we will get our data to replace
String filter = request.getParameter(param.filter);
// the place where we will hold the person details
Person person = null;
// iterate through all known people
for(Person thisPerson : spdx.creatorSection.people){
if(thisPerson.getTitle().equals(filter)){
// we found a match (ignore any duplicates)
person = thisPerson;
break;
}
}
// need to check if we do have someone..
if(person == null){
request.setAnswer("Person was not found");
return;
}
// place the contents of our template 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();
formFields.setValue(name, person.getPerson());
formFields.setValue(email, person.getPersonEmail());
formFields.setValue(organization, person.getOrganization());
formFields.setValue(organizationEmail, person.getOrganizationEmail());
formFields.setValue(tool, person.getTool());
formFields.setValue(toolVersion, person.getToolVersion());
// point where is the SPDX with this information
formFields.setValue(sourceTag, spdxTarget);
formFields.setValue(param.filter, filter);
// 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);
//request.setAnswer(result);
}
/**
* Save the settings to disk
* @param request
*/
public void save(WebRequest request){
// we need the "file" parameter to tell us what to detail
String spdxTarget = request.getParameter(sourceTag);
// does this file exists?
File file = getFile(spdxTarget, request);
if(file == null){
return;
}
// get the SPDX file from the root node
SPDXfile spdx = new SPDXfile(file);
// get the filter, from where we will get our data to replace
String filter = request.getParameter(param.filter);
// the place where we will hold the person details
Person person = null;
// iterate through all known people
for(Person thisPerson : spdx.creatorSection.people){
if(thisPerson.getTitle().equals(filter)){
// we found a match (ignore any duplicates)
person = thisPerson;
break;
}
}
// need to check if we do have someone..
if(person == null){
request.setAnswer("Person was not found");
return;
}
// name of the SPDX author
processItem(person.getTagPerson(), person.getPerson(),
person.getAnchor(), spdx, request, name, "Creator: ");
// email of the SPDX author
// processItemEmail(person.getTagPerson(), person.getPerson(),
// person.getAnchor(), spdx, request, email);
//
// does the name need to be changed?
String thisPersonEmail = getParam(email, request);
if(thisPersonEmail.endsWith(person.getPersonEmail())==false){
// we need to change things
spdx.changeTag(person.getTagPerson(), person.getPersonEmail(), thisPersonEmail);
}
// organization
processItem(person.getTagOrganization(), person.getOrganization(),
person.getAnchor(), spdx, request, organization, "Creator: Organization:");
// does the name need to be changed?
String thisOrganizationEmail = getParam(organizationEmail, request);
if(thisOrganizationEmail.endsWith(person.getOrganizationEmail())==false){
// we need to change things
spdx.changeTag(person.getTagOrganization(), person.getOrganizationEmail(), thisOrganizationEmail);
}
// tool
processItem(person.getTagTool(), person.getTool(),
person.getAnchor(), spdx, request, tool, "Creator: Tool:");
// does the name need to be changed?
String thisToolVersion = getParam(toolVersion, request);
if(thisToolVersion.endsWith(person.getToolVersion())==false){
// we need to change things
spdx.changeTag(person.getTagTool(), person.getToolVersion(), thisToolVersion);
}
//System.out.println(spdx.rawText);
spdx.commitChanges();
// all done
String message = html.h3("Settings saved!");
request.setAnswer(
message
// utils.html.redirect("/webserver/server", 2, message)
);
}
/**
* Process the organization details
* @param person the object with the details from the person
* @param spdx the SPDX document
* @param request the web request from the end user
*/
private void processItem(TagValue oldTag, String oldValue,
int linePosition,
SPDXfile spdx, WebRequest request,
String formParam, String newTagText){
// get the proposed value
String newValue = getParam(formParam, request);
// do we have an organization tag available?
if(oldTag == null){
// none, this means we need to create one
String text = newTagText + " " + newValue;
spdx.addTag(linePosition, text);
log.write(is.ADDING, "Added new tag as %1", text);
return;
}
// does the name exists but just needs to be changed?
if(newValue.endsWith(oldValue)==false){
// we need to change things
spdx.changeTag(oldTag, oldValue, newValue);
}
}
// /**
// * Process the organization details
// * @param person the object with the details from the person
// * @param spdx the SPDX document
// * @param request the web request from the end user
// */
// private void processItemEmail(TagValue originalTag,
// SPDXfile spdx, WebRequest request, String formParam){
// // get the proposed value
// String newValue = getParam(formParam, request);
// // do we have an organization tag available?
// if(originalTag == null){
// // none, this means we need to create one
// String text = newTagText + " " + newValue;
// spdx.addTag(linePosition, text);
// log.write(is.ADDING, "Added new tag as %1", text);
// return;
// }
//
// // does the name exists but just needs to be changed?
// if(newValue.endsWith(oldValue)==false){
// // we need to change things
// spdx.changeTag(oldTag, oldValue, newValue);
// }
// }
/**
*
* @param parameter
* @param request
*/
private String getParam(String parameter, WebRequest request){
String result = "";
try{
result = request.getParameter(parameter);
}catch (Exception e){
}
return result;
}
/**
* Verifies if a given SPDX document exists inside our archive or or not
* @param spdxTarget The file inside the SPDX Archive
* @return null if the file does not exists, otherwise return a pointer
*/
private File getFile(String spdxTarget, WebRequest request){
if(spdxTarget == null){
request.setAnswer("No file specified");
return null;
}
// does this file exists?
File file = new File(core.getProductsFolder(), spdxTarget);
// this file needs to exist
if((file.exists() == false) || (file.isDirectory())){
request.setAnswer("File was not found in our archive, sorry");
return null;
}
// all done
return file;
}
}