|
a/tool/src/spdxlib/tools.java |
|
b/tool/src/spdxlib/tools.java |
|
... |
|
... |
11 |
* to check certain details about SPDX information.</text>
|
11 |
* to check certain details about SPDX information.</text>
|
12 |
*/
|
12 |
*/
|
13 |
|
13 |
|
14 |
package spdxlib;
|
14 |
package spdxlib;
|
15 |
|
15 |
|
|
|
16 |
import java.io.File;
|
|
|
17 |
import main.core;
|
|
|
18 |
import www.WebRequest;
|
|
|
19 |
|
16 |
|
20 |
|
17 |
/**
|
21 |
/**
|
18 |
*
|
22 |
*
|
19 |
* @author Nuno Brito, 17th of November 2013 in Darmstadt, Germany.
|
23 |
* @author Nuno Brito, 17th of November 2013 in Darmstadt, Germany.
|
20 |
* nuno.brito@triplecheck.de | http://nunobrito.eu
|
24 |
* nuno.brito@triplecheck.de | http://nunobrito.eu
|
21 |
*/
|
25 |
*/
|
22 |
public class tools {
|
26 |
public class tools {
|
23 |
|
27 |
|
24 |
|
|
|
25 |
/**
|
28 |
/**
|
26 |
* This method will pick on a given FileInfo object and extract the relevant
|
29 |
* Verifies if a given SPDX document exists inside our archive or or not
|
27 |
* licensing details.
|
30 |
* @param spdxTarget The file inside the SPDX Archive
|
28 |
* @param file The FileInfo object, cannot be null
|
31 |
* @param request
|
29 |
* @return A string text with a list of licenses when available. If no
|
32 |
* @return null if the file does not exists, otherwise return a pointer
|
30 |
* license was found, it will return null
|
|
|
31 |
*/
|
33 |
*/
|
32 |
public static String getLicense(FileInfo file){
|
34 |
public static File getFile(String spdxTarget, WebRequest request){
|
33 |
String licenseOutput = "";
|
35 |
if(spdxTarget == null){
|
34 |
// priority is the case where a human concludes a license
|
36 |
request.setAnswer("No file specified");
|
35 |
if((file.tagLicenseConcluded != null)
|
37 |
return null;
|
36 |
&&(file.tagLicenseConcluded.toString().equals("NOASSERTION")!=true)){
|
|
|
37 |
// no need to proceed
|
|
|
38 |
return file.tagFileName.toString()
|
|
|
39 |
+ " ("+ file.tagLicenseConcluded.toString() +")";
|
|
|
40 |
}
|
38 |
}
|
41 |
|
39 |
// does this file exists?
|
42 |
// second priority are licenses detected inside the code
|
40 |
File file = new File(core.getProductsFolder(), spdxTarget);
|
43 |
if(file.licenseInfoInFile.size()>0){
|
41 |
// this file needs to exist
|
44 |
|
42 |
if((file.exists() == false) || (file.isDirectory())){
|
45 |
for(TagValue tag : file.licenseInfoInFile){
|
43 |
request.setAnswer("Sorry, the file was not found: " + spdxTarget);
|
46 |
licenseOutput = licenseOutput.concat(", " + tag.toString());
|
44 |
return null;
|
47 |
}
|
|
|
48 |
// remove the first comma added throught our loop
|
|
|
49 |
licenseOutput = " (" + licenseOutput.substring(2) + ")";
|
|
|
50 |
}
|
45 |
}
|
51 |
return licenseOutput;
|
46 |
// all done
|
|
|
47 |
return file;
|
52 |
}
|
48 |
}
|
53 |
|
49 |
|
54 |
|
50 |
|
55 |
/**
|
|
|
56 |
* When given a file, this method checks if it has either a reported or
|
|
|
57 |
* concluded license. The method is necessary since sometimes one of these
|
|
|
58 |
* fields is present using infomation like "NOASSERTION" which really
|
|
|
59 |
* becomes a problem to normalize.
|
|
|
60 |
* @param file A FileInfo object with the information that we will analyze
|
|
|
61 |
* @return
|
|
|
62 |
*/
|
|
|
63 |
public static Boolean hasLicense(FileInfo file){
|
|
|
64 |
|
|
|
65 |
// do we have a license concluded for this file?
|
|
|
66 |
if(file.tagLicenseConcluded != null){
|
|
|
67 |
// get the text from the license that was concluded
|
|
|
68 |
String licenseText = file.tagLicenseConcluded.toString();
|
|
|
69 |
// a lot of trouble, but necessary to find out what is happening
|
|
|
70 |
if( (licenseText.equals("NONE")== false)
|
|
|
71 |
&& (licenseText.equals("NOASSERTION")== false)){
|
|
|
72 |
// we have at least one license concluded. Good enough
|
|
|
73 |
return true;
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// are there any licenses inside this file?
|
|
|
78 |
for(TagValue licenseTag : file.licenseInfoInFile){
|
|
|
79 |
String licenseText = licenseTag.toString();
|
|
|
80 |
// excluse the cases that shouldn't count for this metric
|
|
|
81 |
if(licenseText.equals("NONE")){
|
|
|
82 |
continue;
|
|
|
83 |
}
|
|
|
84 |
if(licenseText.equals("NOASSERTION")){
|
|
|
85 |
continue;
|
|
|
86 |
}
|
|
|
87 |
// we just need to find one license to make our day valid! :-)
|
|
|
88 |
return true;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
// no licenses, let's leave
|
|
|
95 |
return false;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
}
|
51 |
}
|