Child: [r10] (diff)

Download this file

tools.java    99 lines (82 with data), 3.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
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-17T00:00:00Z
* LicenseName: NOASSERTION
* FileName: check.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> This class contains several functions that help
* to check certain details about SPDX information.</text>
*/
package spdxlib;
/**
*
* @author Nuno Brito, 17th of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class tools {
/**
* This method will pick on a given FileInfo object and extract the relevant
* licensing details.
* @param file The FileInfo object, cannot be null
* @return A string text with a list of licenses when available. If no
* license was found, it will return null
*/
public static String getLicense(FileInfo file){
String licenseOutput = "";
// priority is the case where a human concludes a license
if((file.tagLicenseConcluded != null)
&&(file.tagLicenseConcluded.toString().equals("NOASSERTION")!=true)){
// no need to proceed
return file.tagFileName.toString()
+ " ("+ file.tagLicenseConcluded.toString() +")";
}
// second priority are licenses detected inside the code
if(file.licenseInfoInFile.size()>0){
for(TagValue tag : file.licenseInfoInFile){
licenseOutput = licenseOutput.concat(", " + tag.toString());
}
// remove the first comma added throught our loop
licenseOutput = " (" + licenseOutput.substring(2) + ")";
}
return licenseOutput;
}
/**
* When given a file, this method checks if it has either a reported or
* concluded license. The method is necessary since sometimes one of these
* fields is present using infomation like "NOASSERTION" which really
* becomes a problem to normalize.
* @param file A FileInfo object with the information that we will analyze
* @return
*/
public static Boolean hasLicense(FileInfo file){
// do we have a license concluded for this file?
if(file.tagLicenseConcluded != null){
// get the text from the license that was concluded
String licenseText = file.tagLicenseConcluded.toString();
// a lot of trouble, but necessary to find out what is happening
if( (licenseText.equals("NONE")== false)
&& (licenseText.equals("NOASSERTION")== false)){
// we have at least one license concluded. Good enough
return true;
}
}
// are there any licenses inside this file?
for(TagValue licenseTag : file.licenseInfoInFile){
String licenseText = licenseTag.toString();
// excluse the cases that shouldn't count for this metric
if(licenseText.equals("NONE")){
continue;
}
if(licenseText.equals("NOASSERTION")){
continue;
}
// we just need to find one license to make our day valid! :-)
return true;
}
// no licenses, let's leave
return false;
}
}