Parent: [r19] (diff)

Child: [r24] (diff)

Download this file

create.java    683 lines (584 with data), 23.5 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-16T00:00:00Z
* LicenseName: NOASSERTION
* FileName: pluginCreateSPDX.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Add the node of tools inside the tree view. </text>
*/
package spdx;
import GUI.swingUtils;
import spdxlib.DocumentCreate;
import definitions.Messages;
import definitions.is;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import script.Plugin;
import script.log;
import main.core;
import main.param;
import org.rauschig.jarchivelib.ArchiveFormat;
import org.rauschig.jarchivelib.Archiver;
import org.rauschig.jarchivelib.ArchiverFactory;
import org.rauschig.jarchivelib.CompressionType;
import script.DownloadBigFile;
import script.RunningTask;
import utils.html;
import www.RequestOrigin;
import www.WebRequest;
/**
*
* @author Nuno Brito, 16th of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class create extends Plugin{
final String
id = "Create SPDX",
LastFolderNewSPDX = "LastFolderNewSPDX";
final String acceptedExtension =
".tar.gz"
+ ">>"
+ "zip",
templateFolderHTML = "spdxFolder.html";
int interval = 3;
@Override
public void startup(){
// add our node to the tree right after the "Tools" node is added
log.hooks.addAction(Messages.AddingTools, thisFile, "addNode");
}
/**
* Add our toolbox node to the treeview.
*/
public void addNode(){
addTreeNode(id, "box--plus.png", "main");
addChildNode("from web", "application-dock-270.png", "mainWeb");
addChildNode("from folder", "folder-smiley.png", "mainFolder");
}
/**
* Displays the menu for creating new SPDX documents
* @param request the request for this method
*/
@Override
public void main(WebRequest request){
request.setPage("spdxMain.html");
// String result = "<html>\n" +
//// "<head>\n" +
//// "<style type=\"text/css\">body {font-family:verdana,arial,"
//// + "sans-serif;font-size:10pt;margin:30px;}</style>"
//// + "</head>\n" +
//// "\n" +
// "<body>"
// + ""
// + "Testing stuff!"
// + ""
// + "</body>"
// + "</html>";
// request.setAnswer(result);
}
public void mainWeb(WebRequest request){
request.setPage("spdxDialog.html");
}
public void mainZip(WebRequest request){
//request.setPage("spdxDialog.html");
String result = "Hello there Zip!";
request.setAnswer(result);
}
/**
* Dialog for creating a new SPDX document from a folder on disk
* @param request
*/
public void mainFolder(WebRequest request){
// load our template
request.setTemplate(templateFolderHTML);
// get the value used before
String selectedFolder = settings.read(LastFolderNewSPDX, "");
// if nothign is chosen, just show it as "none"
if(selectedFolder.isEmpty()){
selectedFolder = "NONE";
}
// add the value of our directory
request.changeTemplate("none-template", selectedFolder);
// all done
request.close();
//
// String result =
// html.div()
// + html.h2("Create a new SPDX from folder on disk")
// + "Use this page to choose a folder and create an SPDX document"
// + html.br
// + html.br
// + html.h3("Selected Folder")
// + selectedFolder
// + html.br
//
// + html.br
// + html.link("Choose a folder", "create?x=folder")
//
// + html.br
// + html.br
// + html.link("Start", "create?x=foldercreate")
//
//
// + html._div
// ;
//
// request.setAnswer(result);
}
/**
* Chooses a folder to be used as source for creating a new SPDX
* @param request
*/
public void foldercreate(WebRequest request){
// what should we use as source?
String selectedFolder = settings.read(LastFolderNewSPDX);
// start our action
log.write(is.INFO, "Creating an SPDX document using as source: %1",
selectedFolder );
// transform the thing into a file pointer
final File source = new File(selectedFolder);
if(source.exists() == false){
// we have a problem, exit here
log.write(is.ERROR, "SPDX create - Source folder doesn't exist: %1",
source.getAbsolutePath());
return;
}
final WebRequest what = request;
RunningTask task = new RunningTask(){
@Override
public void doTask(){
setTitle("Creating SPDX from source folder");
// start the SPDX class
final DocumentCreate newSPDX = new DocumentCreate();
// launch a small thread to keep the progress updated
Thread thread = new Thread(){
@Override
public void run(){
// wait a little bit for things to start
utils.time.wait(4);
// keep repearing while things are being processed
while(newSPDX.isProcessing){
utils.time.wait(4);
setStatus("%1 files processed out of %1",
"" + newSPDX.filesProcessed,
"" + newSPDX.files.size());
}
}
};
thread.start();
// third step, create the SPDX document from the extracted files
String result = createDocument(source, this, newSPDX);
// all done here, explain where the SPDX document can be found
nextStep =
html.link("SPDX summary",
"/spdx/show?x=summary&"
+ param.spdx + "=" + result)
+ " | " +
html.link("Show full text",
"/spdx/show?x=full&"
+ param.spdx + "=" + result)
;
}
};
task.launch();
//request.setAnswer("Process launched as " + task.getUID());
request.setAnswer(utils.html.redirect("/basic/status"
+ "?ID=" + task.getUID(), 0, ""));
}
/**
* Chooses a folder to be used as source for creating a new SPDX
* @param request
*/
public void folder(WebRequest request){
// no support for requests from the browser
if(request.requestOrigin == RequestOrigin.BROWSER){
log.write(is.ERROR, "Support to SPDX creation from folder on disk "
+ "is not yet implemented");
request.setAnswer("Not supported from browser");
return;
}
// do we want an older located defined?
String selectedFolder = settings.read(LastFolderNewSPDX);
// show the dialog
File result = swingUtils.chooseFolder(new File(selectedFolder));
// place the result in our settings
if(result != null){
settings.write(LastFolderNewSPDX, result.getAbsolutePath());
}
String output = html.redirect("/spdx/create?x=mainFolder", 2,
" Returning to previous page..");
request.setAnswer(output);
}
/**
* The part where we create the SPDX document
* @param request
*/
public void start(WebRequest request){
final WebRequest what = request;
RunningTask task = new RunningTask(){
@Override
public void doTask(){
setTitle("Creating an SPDX document");
// first step, download our file from the Internet (or wherever)
File downloadedFile = downloadFile(what, this);
if(downloadedFile == null){
return;
}
// second step, decompress the downloaded archive
File extractedFolder = extractFile(downloadedFile, this);
if(extractedFolder == null){
return;
}
final DocumentCreate newSPDX = new DocumentCreate();
// third step, create the SPDX document from the extracted files
String result = createDocument(extractedFolder, this, newSPDX);
}
};
task.launch();
//request.setAnswer("Process launched as " + task.getUID());
request.setAnswer(utils.html.redirect("/basic/status"
+ "?ID=" + task.getUID(), 0, ""));
}
/**
* Creates the SPDX document from the folder of extracted source code files
* @param extractedFolder The folder where source code files are located
* @param task the running task that will hold the status update
*/
private String createDocument(File extractedFolder, RunningTask task,
DocumentCreate newSPDX){
task.setStatus("Creating the SPDX document");
// do all the heavy work
String result = newSPDX.create(extractedFolder);
// count the time it took to run this task
long time = System.currentTimeMillis() - task.getUID();
String timeCount = utils.time.timeNumberToHumanReadable(time) + " were "
+ "necessary to complete this task";
task.setStatus(timeCount);
// all done
task.setStatus("All done!");
task.setPercentageComplete(100);
// all done here, explain where the SPDX document can be found
task.nextStep =
html.link("SPDX summary",
"/spdx/show?x=summary&"
+ param.spdx + "=" + result)
+ " | " +
html.link("Show full text",
"/spdx/show?x=full&"
+ param.spdx + "=" + result)
;
// when all things said and done
concludeCreation(newSPDX);
return result;
}
private File extractFile(File downloadedFile, RunningTask task){
String fileName = downloadedFile.getName().toLowerCase();
task.setStatus("Extracting files from %1", downloadedFile.getName());
// create an output folder
File mainFolder = new File(downloadedFile.getParentFile(), "extracted");
// create the specific folder for this file
File destination = new File(mainFolder, fileName);
utils.files.mkdirs(destination);
// get the name portion
Archiver archiver = null;
// handle different compressed files
if(fileName.endsWith(".tar.gz"))
archiver = ArchiverFactory.createArchiver(ArchiveFormat.TAR,
CompressionType.GZIP);
if(fileName.endsWith("zip"))
archiver = ArchiverFactory.createArchiver(ArchiveFormat.ZIP);
if(fileName.endsWith("jar"))
archiver = ArchiverFactory.createArchiver(ArchiveFormat.JAR);
if(archiver == null){
task.setIsProcessing(false);
task.setStatus("File %1 is not recognized as something that "
+ "we can decompress");
return null;
}
try {
// do the extraction of this file
archiver.extract(downloadedFile, destination);
archiver = null;
} catch (IOException ex) {
Logger.getLogger(create.class.getName()).log(Level.SEVERE, null, ex);
}
// all done
task.setStatus("All files from %1 have been extracted",
downloadedFile.getName());
task.setPercentageComplete(20);
return destination;
}
/**
* Downloads the file in case it hasn't been downloaded in the past
* @param request
*/
private File downloadFile(WebRequest request, RunningTask task){
// get whatever we are supposed to download
String sourceURL = request.getParameter("source");
if(sourceURL == null){
return null;
}
File downloadFolder = new File(core.getWorkFolder(), "downloads");
// we might need to create this folder
if(downloadFolder.exists() == false){
utils.files.mkdirs(downloadFolder);
}
// now, get the filename portion of this file
int lastSlash = sourceURL.lastIndexOf("/");
String targetFilename = sourceURL.substring(lastSlash + 1);
// we need files to have an extension
if(targetFilename.contains(".")==false){
// nothing else to do, just leave
task.setIsProcessing(false);
task.setStatus("Download file is not valid: %1", sourceURL);
return null;
}
// where will this downloaded file be placed?
File targetFile = new File(downloadFolder, targetFilename);
// get the big file
task.setStatus("Downloading the file from %1", sourceURL);
final DownloadBigFile download = new DownloadBigFile(sourceURL, targetFile);
// Thread thread = new Thread(){
// @Override
// public void run(){
download.getFile();
// }
// };
// thread.start();
//
// // just wait while things are being downloaded
// while(download.getIsDownloading()){
// utils.time.wait(5);
// String targetSize = utils.files.humanReadableSize(targetFile.length());
// task.setStatus("Downloading %1, %2 have been downloaded",
// targetFilename, targetSize);
// }
//
// get the file size
String targetSize = utils.files.humanReadableSize(targetFile.length());
task.setStatus("File was downloaded as \"%1\" with an approximate"
+ " size of %2"
,targetFilename, targetSize);
// download part is done, increase to 10% of overall task
task.setPercentageComplete(10);
return targetFile;
}
/**
* Do the things that are necessary after creating a new SPDX document, such as
* updating the tree view
*/
private void concludeCreation(DocumentCreate newSPDX){
String UID = newSPDX.UID;
// refresh the tree
swingUtils.refreshAll(core.studio.getTree(), UID);
}
}
//
// /**
// * Opens the choose a folder dialog
// */
// public void chooseFolderNewSPDX(){
// String folderName = core.settings.read(LastFolderNewSPDX, "");
// File folder = null;
// if(folderName != null){
// folder = new File (folderName);
// }
// // open the folder choosing dialog
// File result = swingUtils.chooseFolder(folder);
// // was some folder selected?
// if(result == null){
// return;
// }
// // all done, let's write things up and then exit
// core.settings.write(LastFolderNewSPDX, result.getAbsolutePath());
// swingUtils.refreshTextBox();
// }
//
//
// /**
// * All set and done, let's create a new SPDX document!
// */
// public void doCreateSPDX(){
// final String folderName = core.settings.read(LastFolderNewSPDX, "");
// // is a folder selected?
// if(folderName.length() == 0){
// swingUtils.showMessage("You need to select a folder to use as source");
// return;
// }
//
//
// Thread thread = new Thread(){
//
// //@Override
// public void run(){
//
// // create the new SPDX object
// final File newDocument = new File(folderName);
// final DocumentCreate newSPDX = new spdx.DocumentCreate();
// Thread thread = new Thread(){
// @Override
// public void run(){
// newSPDX.create(newDocument);
//
// }
// };
// thread.start();
// utils.time.wait(1);
// while(newSPDX.isProcessing){
// // write our progress
// doProgressScreen(newSPDX);
// utils.time.wait(interval);
// }
//
// // all done
// String UID = newSPDX.UID;
// // if things have worked, reload all the scripts
// if(newSPDX.isOk()){
// //swingUtils.showMessage("Success, your document is found at "
// // + newSPDX.file.getAbsolutePath());
// swingUtils.refreshAll(core.studio.getTree(), UID);
// //swingUtils.setSelectedNode(UID);
// }else{
// swingUtils.showMessage("Sorry, something went wrong.");
// }
//
// }
//
//
// };
// thread.start();
// }
//
// /**
// * Writes a nice HTML screen with output of current progress
// * @param newSPDX
// */
// private void doProgressScreen(DocumentCreate newSPDX) {
//
// String textValue = "";
//
// // don't show progress if nothing was done yet
// if(newSPDX.filesToProcess != 0){
//
// int rate = newSPDX.filesProcessed - newSPDX.lastCounter;
// rate = rate / interval;
//
// String textRate = ""
// + html.getIcon("stickman-run.png")
// + "Running at a speed of " + rate + " files per second";
// // handle cases of zero
// if(rate == 0){
// textRate = ""
// + html.getIcon("stickman-smiley-angry.png")
// + "Turbulence. We're stuck on something"
// ;
// }
//
// if(rate > 150){
// textRate = ""
// + html.getIcon("stickman-smiley-love.png")
// + " Looking good at over " + rate + " files per second"
// ;
// }
//
//
//
// // how long?
//// int estimation =
//// (newSPDX.filesToProcess - newSPDX.filesProcessed) / rate;
// textValue = ""
// + html.getIcon("application-monitor.png")
// + " Processed "
// + newSPDX.filesProcessed
// + " out of "
// + newSPDX.filesToProcess
// + " files"
// + html.br
// + textRate
//// + html.br
//// + html.getIcon("stickman-smiley-question.png")
//// + "We estimate this it take some "
//// + estimation
//// + " seconds"
// ;
// // update the counter
// newSPDX.lastCounter = newSPDX.filesProcessed;
// }
//
// // Do the message
// String inProgress = ""
// + html.div()
// + "<h2>"
// + html.getIcon("wand.png")
// + "In progress!</h2>"
// + ""
// + "Your SPDX document will be ready soon, please wait.."
// + html.br
// + html.br
// //+ html.getIcon("stickman-run.png")
// + textValue
// + html._div;
//
// // update the text box
// core.studio.editorPane(is.contentHTML, false, 0, inProgress);
// }
//}
//request.setAnswer("All done");
// // initially, we don't have a folder selected
// String folderLocation = ""
// + html.div()
// + html.linkScript("Choose", thisFile,
// "chooseFolderNewSPDX")
// + " a folder to use as source"
// //+ html.getIcon("folder-horizontal-open.png")
// + html._div
// ;
// // if we already have a previously selected folder, choose it here
// if(core.settings.hasKey(LastFolderNewSPDX)){
// folderLocation = ""
// + folderLocation
// + html.div(20)
// + " "
// + "<i>"
// + core.settings.read(LastFolderNewSPDX)
// + "</i>"
// + html._div
// //+ html.br
// ;
// }
//
//
// String description = "This tool allows you to create a new"
// + " SPDX document that will be available inside your"
// + " Products folder. The only required setting is"
// + " the folder where your source code files are"
// + " located";
//
// String result = ""
// + html.div()
// + "<h2>"
// + html.getIcon("wooden-box-label.png")
// + "Create a new SPDX</h2>"
// + description
// + html.br
// //+ "<b>"
// // + "Folder to use as source"
// //+ "</b>"
// + html.br
// + folderLocation
// + html.br
// + html.br
// + "Ready? After clicking on the link below you should"
// + " see available the new SPDX on the product list tree view "
// + html.br
// + html.br
// + html.getIcon("wand.png")
// + html.linkScript("Create SPDX", thisFile, "doCreateSPDX")
// + html._div
// + "";
// all done
// request.setAnswer(result);