Parent: [r5] (diff)

Child: [r9] (diff)

Download this file

show.java    743 lines (670 with data), 26.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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-12-11T00:00:00Z
* LicenseName: NOASSERTION
* FileName: show.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> The class that shows details about a given SPDX document
available on our archive our shows all the available SPDX documents when no
specific document is mentioned.
* </text>
*/
package spdx;
import GUI.swingUtils;
import definitions.is;
import java.io.File;
import www.Table;
import java.util.ArrayList;
import script.Plugin;
import spdxlib.tools;
import spdxlib.FileInfo;
import spdxlib.SPDXfile;
import main.core;
import main.param;
import script.log;
import utils.html;
import www.WebRequest;
/**
*
* @author Nuno Brito, 11th of December 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class show extends Plugin{
/**
* Shows a summary of details about the selected package
* @param request
*/
@Override
public void main(WebRequest request) {
int
counterFiles = 0,
counterCreators = 0,
counterLicensesDeclared = 0
;
// get some statistical data
for(Object object : core.products){
SPDXfile spdx = (SPDXfile) object;
counterFiles += spdx.fileSection.files.size();
counterCreators += spdx.creatorSection.people.size();
counterLicensesDeclared += doStatistics(spdx);
}
// calculate percentage of files with a license declared
String percentage =
" ("
+ (counterLicensesDeclared * 100)/counterFiles
+ "% in total)" + html.br;
String result =
html.div()
+ "<h2>"
//+ html.getCommonFolderIcon("wooden-box-label.png")
+ "SPDX archive"
+ "</h2>"
+ html._div
+ html.div(20)
//+ html.getCommonFolderIcon("documents-stack.png")
+ counterFiles + " files in total"
+ html.br
//+ html.getCommonFolderIcon("calculator.png")
+ counterLicensesDeclared + " files with declared licenses"
+ percentage
//+ html.getCommonFolderIcon("stickman.png")
+ counterCreators + " SPDX creators" + html.br
//+ html.br
+ html._div
+ html.br
+ "";
// provide an answer
request.setAnswer(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;
}
/**
* Shows the full text of an SPDX document
* @param request
*/
public void full(WebRequest request) {
// 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;
}
// read the whole file
String result = utils.files.readAsString(file);
// output the file
request.setAnswer(result);
}
/**
* Show the summary for a given SPDX document
* @param request
*/
public void summary(WebRequest request) {
// 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);
// compute some of our useful statistics about the SPDX document
int counterLicensesDeclared = doStatistics(spdx);
int counterFiles = spdx.fileSection.files.size();
// calculate percentage of files with a license declared
String percentage =
" ("
+ (counterLicensesDeclared * 100)/counterFiles
+ "% in total)" + html.br;
String searchEngines =
html.div(10)
+ html.linkToSearchGoogle(spdx.getId())
// + html.divider
// + html.linkToSearchAntepedia(node.id)
+ html.divider
+ html.linkToSearchGitHub(spdx.getId())
+ html._div
;
String evaluation = doEvaluation(spdx);
// prepare the answer
String result = ""
//swingUtils.getBreadcrumb(node)
+ html.div(20)
+ html.h2(spdx.getId())
//+ html.getCommonFolderIcon("documents-stack.png")
+ counterFiles + " files inside the package"
+ html.br
//+ html.getCommonFolderIcon("calculator.png")
+ counterLicensesDeclared + " files with declared licenses"
+ percentage
+ evaluation
+ html._div
+ html.div()
+ html.br
+ swingUtils.addIfNotEmpty(""
//html.getCommonFolderIcon("magnifier-zoom-fit.png")
+ "Look around the web"
, searchEngines)
+ html._div
+ html.div()
+ "<b>" + "Other actions" + "</b>" + html.br
+ html._div
+ html.div(20)
//+ html.getCommonFolderIcon("receipt--pencil.png")
+ html.link("Show full text",
"?x=full&"
+ param.spdx + "=" + spdxTarget)
+ html._div
+ html.br
//+ html.br
+ html.div()
+ "<b>" + "List files" + "</b>" + html.br
+ html._div
+ html.div(20)
//+ html.getCommonFolderIcon("folder-smiley-sad.png")
//+ html.linkScript("Without a license", thisFile, "showFilesWithoutLicense")
+ html.link("Without license specified", "?x=list&"
+ param.filter + "=nolicense&"
+ param.spdx + "=" + spdxTarget)
+ html.br
//+ html.getCommonFolderIcon("folder-smiley.png")
//+ html.linkScript("With a license reported", thisFile, "showFilesWithLicense")
+ html.link("With license", "?x=list&"
+ param.filter + "=withlicense&"
+ param.spdx + "=" + spdxTarget)
+ html._div
+ "";
// write everything on our UI text area
request.setAnswer(result);
}
/**
* list some information according to a filter
* @param request
*/
public void list(WebRequest request) {
// 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 what we want to show
String thisFilter = request.getParameter(param.filter);
// value can't be empty
if(thisFilter == null){
request.setAnswer("No filter specified");
return;
}
// start the processing
SPDXfile spdx = new SPDXfile(file);
// create the place holder for the results
ArrayList<FileInfo> list = new ArrayList();
// get only the files without a declared license
if(thisFilter.equalsIgnoreCase(param.noLicense)){
// iterate through all files
for(FileInfo fileInfo : spdx.fileSection.files){
// if there is a license, no need to continue
if(tools.hasLicense(fileInfo)){
continue;
}
list.add(fileInfo);
}
}
// get the unlicensed files
if(thisFilter.equalsIgnoreCase(param.withLicense)){
// iterate through all files
for(FileInfo fileInfo : spdx.fileSection.files){
// if there is a license, no need to continue
if(tools.hasLicense(fileInfo)==false){
continue;
}
list.add(fileInfo);
}
}
// create the HTML data where our info will be placed
String[] columns = new String[]{"File name", "", "Path"};
Table table = new Table(columns);
// iterate through all files
for(FileInfo fileInfo : list){
// create a column with our file information
String[] column = new String[]{
// utils.text.shortText(fileInfo.toString(), 25),
// utils.text.shortText(fileInfo.tagFilePath.toString(), 30),
fileInfo.toString(),
html.link("Detail", "?x=specific&"
+ param.spdx + "=" + spdxTarget + "&"
+ param.file + "=" + fileInfo.getName()
),
fileInfo.tagFilePath.toString()
};
table.add(column);
}
// do the output that is displayed to the end-user
String result = "" //swingUtils.getBreadcrumb(node)
+ html.div(20)
+ html.h2(spdx.getId())
//+ this.doFileSummary(spdx)
+ doEvaluation(spdx)
+ "Showing " + list.size() + " files from a total of "
+ spdx.fileSection.files.size() + " files"
+ "" + html.br+ html.br
+ table.output()
+ html._div
;
request.setAnswer(result);
}
/**
* list some information according to a filter
* @param request
*/
public void specific(WebRequest request) {
// we need the "file" parameter to tell us what to detail
String spdxTarget = request.getParameter(param.spdx);
// does this file exists?
File spdxFile = getFile(spdxTarget, request);
if(spdxFile == null){
return;
}
// get what we want to show
String targetFile = request.getParameter(param.file);
// value can't be empty
if(targetFile == null){
request.setAnswer("No filter specified");
return;
}
// start the processing
SPDXfile spdx = new SPDXfile(spdxFile);
// go through all files inside the document
for(FileInfo file : spdx.fileSection.files){
// have we (finally) found a match?
if(targetFile.equals(file.getName())){
String result = getFileDetails(file);
request.setAnswer(result);
return;
}
}
request.setAnswer("Didn't found " + targetFile);
}
/**
* Show the specific file details about a given file name
* @param file
*/
private String getFileDetails(FileInfo file){
String googleTerm;
String filename = file.tagFileName.toString();
// get the file extension if available
//String extension = getFileExtension(filename);
try{
// we need to remove misleading terms from the filename
String filteredName = file.getName().toLowerCase();
filteredName = filteredName.replace(".tar.gz", "");
filteredName = filteredName.replace(".zip", "");
filteredName = filteredName.replace(".jar", "");
filteredName = filteredName.replace(".7z", "");
filteredName = filteredName.replace(".tar", "");
filteredName = filteredName.replace(".bz2", "");
// build up our google query
googleTerm = filteredName
+ " %22"
+ filename
+ "%22 "
//+ extension
;
} catch (Exception e){
return log.write(is.ERROR, "SFD01 - Error occurred when trying to build"
+ " a search keyword for google");
}
// do the introduction of this file with a breadcrumb
// String[] fields = node.getUID().split(">>");
// String breadCrumb =
// fields[4]
// + ">"
// + fields[3]
// + ">"
// + fields[1]
// ;
// add a short summary about the file
String summary = "";
if(file.tagFileSize != null){
summary += "This file is sized in " + file.tagFileSize.toString();
}
if(file.tagFileLOC != null){
summary += " and has " + file.tagFileLOC + " lines of code"
+ html.br + html.br;
}
if(spdxlib.tools.hasLicense(file)){
summary += "Applicable license(s): " + spdxlib.tools.getLicense(file);
}
// do the date creation
//TODO We need to archive file date information
//summary += "Created in " + file.;
String resultIntroduction = ""
+ html.h2(filename)
// + "<h2>"
// + breadCrumb
// + "</h2>"
+ html.div()
+ summary
+ html._div
// + html.br
;
// Add links to find more info on the Internet about the file name
String resultFilename =
html.div()
+ html.linkToSearchYandex("\"" + filename + "\"")
+ html.divider
+ html.linkToSearchGoogle(googleTerm)
// + html.divider
// + html.linkToSearchOhloh(file.tagFileName.toString())
+ html.divider
+ html.linkToSearchGitHub(file.tagFileName.toString())
+ html._div;
String resultSHA1 = "";
if(file.tagFileChecksumSHA1!= null){
resultSHA1 = file.tagFileChecksumSHA1.toString()
+ html.br
+ html.div()
+ html.linkSearch("Find duplicates", "SHA1: "
+ file.tagFileChecksumSHA1.toString())
+ html.divider
+ html.linkToSearchGoogle(
"%22"+ file.tagFileChecksumSHA1.toString() + "%22" )
+ html._div;
}
String resultSHA256 = "";
if(file.tagFileChecksumSHA256!= null){
resultSHA256 = file.tagFileChecksumSHA256.toString()
+ html.div()
// only possible when we have SHA256 hashes available
+ html.linkToSearchVirusTotal(file.tagFileChecksumSHA256.toString())
+ html._div;
}
String resultMD5 = "";
if(file.tagFileChecksumMD5!= null){
resultMD5 = file.tagFileChecksumMD5.toString()
+ html.div()
// We might be lucky and find an MD5 with info
+ html.linkToSearchGoogle(
"%22"+ file.tagFileChecksumMD5.toString() + "%22" )
+ html._div;
}
String resultSSDEEP = "";
if(file.tagFileChecksumSSDEEP!= null){
String text = file.tagFileChecksumSSDEEP.raw;
// remove the tag header
text = text.replace("FileChecksum: SSDEEP: ", "");
resultSSDEEP = text
+ html.div()
// only possible when we have SHA256 hashes available
+ html.linkSearch("Find similar files", "SSDEEP: "
+ text)
+ html._div;
}
String result = html.div()
+ resultIntroduction
+ html.br
+ swingUtils.addIfNotEmpty("SHA1", resultSHA1)
+ swingUtils.addIfNotEmpty("SHA256", resultSHA256)
+ swingUtils.addIfNotEmpty("MD5",resultMD5)
//+ html.br
+ swingUtils.addIfNotEmpty("SSDEEP", resultSSDEEP)
+ swingUtils.addIfNotEmpty("Look for \""
+filename
+"\" in search engines"
, resultFilename)
+ html._div
// + swingUtils.addSSDEEP("SSDEEP", file)
// + swingUtils.addIfNotEmpty("Copyright text",file.tagFileCopyrightText.toString())
;
/** missing to add:
* - File path
* - Applicable licenses (when available)
*/
return result;
}
// /**
// * This operation might take a while, so we first launch a menu asking
// * people to wait for a while and then launch a thread with the results
// */
// void showFilesWithoutLicense(){
// String inProgress = ""
// + html.div()
// + "<h2>"
// + html.getCommonFolderIcon("wand.png")
// + "In progress!</h2>"
// + ""
// + "We are creating a list, please wait.."
// + html.br
// + html._div;
// // update the text box
// core.studio.editorPane(is.contentHTML, false, 0, inProgress);
// Thread thread = new Thread(){
// @Override
// public void run(){
// processFilesWithoutLicense();
// }
// };
// thread.start();
// }
//
// /**
// * This operation might take a while, so we first launch a menu asking
// * people to wait for a while and then launch a thread with the results
// */
// void showFilesWithLicense(){
// String inProgress = ""
// + html.div()
// + "<h2>"
// + html.getCommonFolderIcon("wand.png")
// + "In progress!</h2>"
// + ""
// + "We are creating a list, please wait.."
// + html.br
// + html._div;
// // update the text box
// core.studio.editorPane(is.contentHTML, false, 0, inProgress);
// Thread thread = new Thread(){
// @Override
// public void run(){
// processFilesWithLicense();
// }
// };
// thread.start();
// }
//
//
//
//
// /**
// * Shows a list of files for the selected package that do not have a license
// */
// void processFilesWithoutLicense(){
//
// //node = swingUtils.getSelectedNode();
// node = (TreeNodeSPDX) core.temp.get("showPackageDetails");
// // no need to continue if there is nothing selected
// if(node == null){
// return;
// }
// // get the SPDX file from the root node
// SPDXfile spdx = (SPDXfile) node.getUserObject();
//
// // create the HTML data where our info will be placed
// String[] columns = new String[]{"File name", "Path", ""};
// Table table = new Table(columns);
//
//
// // iterate through all files
// for(FileInfo file : spdx.fileSection.files){
//
// // if there is a license, no need to continue
// if(tools.hasLicense(file)){
// continue;
// }
//
// // create a column with our file information
// String[] column = new String[]{
// utils.text.shortText(file.toString(), 25),
// utils.text.shortText(file.tagFilePath.toString(), 30),
// //file.tagFilePath.toString(),
// html.linkNode("details", ">> "
// + file.tagFileName.toString()
// + " >> Files >> "
// + node.id
// + " >> Products ")
// };
// table.add(column);
// }
//
// // do the output that is displayed to the end-user
// String result = swingUtils.getBreadcrumb(node)
// + html.div(20)
// + table.output()
// + html._div
// ;
//
// // write everything on our UI text area
// core.studio.editorPane(is.contentHTML, false, 0, result, false, null);
// }
//
// /**
// * Shows a list of files for the selected package that do not have a license
// */
// void processFilesWithLicense(){
//
// //node = swingUtils.getSelectedNode();
// node = (TreeNodeSPDX) core.temp.get("showPackageDetails");
// // no need to continue if there is nothing selected
// if(node == null){
// return;
// }
// // get the SPDX file from the root node
// SPDXfile spdx = (SPDXfile) node.getUserObject();
//
// // create the HTML data where our info will be placed
// String[] columns = new String[]{"File name", "Path", ""};
// Table table = new Table(columns);
//
//
// // iterate through all files
// for(FileInfo file : spdx.fileSection.files){
//
// // if there is a license, no need to continue
// if(tools.hasLicense(file) == false){
// continue;
// }
//
// // create a column with our file information
// String[] column = new String[]{
// utils.text.shortText(file.toString(), 25),
// utils.text.shortText(file.tagFilePath.toString(), 30),
// //file.tagFilePath.toString(),
// html.linkNode("details", ">> "
// + file.tagFileName.toString()
// + " >> Files >> "
// + node.id
// + " >> Products ")
// };
// table.add(column);
// }
//
// // do the output that is displayed to the end-user
// String result = swingUtils.getBreadcrumb(node)
// + html.div(20)
// + table.output()
// + html._div
// ;
//
// // write everything on our UI text area
// core.studio.editorPane(is.contentHTML, false, 0, result, false, null);
// }
//
//
// /**
// * Show the SPDX document on the text area
// */
// public void showSPDX(){
// node = swingUtils.getSelectedNode();
// // no need to continue if there is nothing selected
// if(node == null){
// return;
// }
// // get the SPDX file from the root node
// SPDXfile spdx = (SPDXfile) node.getUserObject();
//
//
// // write everything on our UI text area
// String content = spdx.rawText;
// core.studio.editorPane(is.contentText, true, 0, content);
// }
//
//
/**
* When given a SPDX object, provide some statistics
* about the number of licenses that are not declared
*/
int doStatistics(SPDXfile spdx){
int counterLicensesDeclared = 0;
// get the number of licenses declared
for(Object fileObject : spdx.fileSection.files){
FileInfo file = (FileInfo) fileObject;
// go deep into the license information
if(tools.hasLicense(file)){
counterLicensesDeclared++;
}
}
return counterLicensesDeclared;
}
/**
* Provides an evaluation if the SPDX file has quality or not
* @param spdx an object from where we will read all data
* @return A string used for the HTML output
*/
private String doEvaluation(SPDXfile spdx) {
String result = "";
Boolean hasSVNfiles = false;
ArrayList<FileInfo> list = spdx.fileSection.files;
for(FileInfo file : list){
if(file.tagFileName.toString().contains(".svn")){
hasSVNfiles = true;
break;
}
}
if(hasSVNfiles){
result = html.br
+ html.h2(""
//html.getCommonFolderIcon("exclamation.png")
+ "Suggestions"
+ "")
+ "Source code has SVN files that could be removed from the"
+ " distribution"
+ html.br
+ html.br
;
}
return result;
}
}