Child: [r10] (diff)

Download this file

DocumentCreate.java    258 lines (221 with data), 9.1 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
/*
* SPDXVersion: SPDX-1.1
* DocumentCreate: Person: Nuno Brito (nuno.brito@triplecheck.de)
* DocumentCreate: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-01T00:00:00Z
* LicenseName: NOASSERTION
* FileName: DocumentCreate.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Creates an SPDX document from a given source </text>
*/
package spdxlib;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import script.License;
import ssdeep.ssdeep;
import main.core;
/**
*
* @author Nuno Brito, 1st of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class DocumentCreate {
public String
// official field names
PackageName = null,
PackageLicenseConcluded = "NOASSERTION",
packageDownloadLocation = "NOASSERTION",
// other things
output, // where we store the content written to a text file
UID
;
boolean isOk = false;
public boolean isProcessing;
public int
filesToProcess = 0,
filesProcessed = 0,
lastCounter = 0;
public File file;
/**
* Has this document been correctly generated?
* @return Yes or No
*/
public Boolean isOk(){
return isOk;
}
/**
* Creates a new SPDX document based on a given source code folder
* @param folderSourceCode where the source code files are located
* @return the name of the resulting SPDX document. Null is this fails.
*/
public String create(File folderSourceCode){
// signal our work start
isProcessing = true;
isOk = false;
// preflight checks
if(folderSourceCode == null){
System.err.println("ND01 - No folder defined as source for SPDX");
isProcessing = false;
return null;
}
// get the files from the target source code folder
ArrayList<File> files = utils.files.findFiles(folderSourceCode);
// no need to continue if no files were found
if((files == null) || (files.isEmpty())){
System.err.println("ND02 - No files were found for creating SPDX");
isProcessing = false;
return null;
}
// update our counter
filesToProcess = files.size();
// where we shall store this SPDX document
File folderProduct = core.getProductsFolder();
// assign the PackageName for this document
String filename = folderSourceCode.getName();
if(PackageName != null){
filename = PackageName;
}
String textDate = getDateSPDX();
// save this title as UID
UID = ">> " + filename + " >> Products ";
filename = filename + ".spdx";
output =
addParagraph("SPDX Document Information")
+ addText("SPDXVersion: SPDX-1.2")
+ addText("DataLicense: CC-BY-4.0")
+ "\n"
+ addParagraph("Creation Information")
+ addText("Creator: " + System.getProperty("user.name"))
+ addText("Creator: Tool: TripleCheck Studio " + core.version)
+ addText("Created: " + textDate)
+ "\n"
+ addParagraph("Package Information")
+ "PackageName: " + folderSourceCode.getName()
+ "\nPackageFileName: " + folderSourceCode.getName()
+ "\nPackageDownloadLocation: " + packageDownloadLocation
+ "\nPackageLicenseConcluded: " + PackageLicenseConcluded
+ "\n"
+ "\n"
+ addParagraph("File Information")
;
// iterate through each file
for(File thisFile : files){
// compute our SSDEEP hashes
ssdeep test = new ssdeep();
String ssdeep;
try {
ssdeep = test.fuzzy_hash_file(thisFile);
} catch (IOException ex) {
ssdeep = "NOASSERTION";
}
// path of this file
String filePath = thisFile.getAbsolutePath();
filePath = filePath.replace(folderSourceCode.getAbsolutePath(), "");
filePath = filePath.replace("\\", "/");
filePath = "." + filePath.replace(thisFile.getName(), "");
// the size of this file
String fileSize = utils.files.humanReadableSize(thisFile.length())
+ " ("+ thisFile.length() +" bytes)";
// do the file identification
String LOC = "";
FileId fileId = new FileId();
Boolean resulted = fileId.analyze(thisFile);
// get the value for lines of COD
if(resulted){
if(fileId.LOC > 0){
LOC = "FileLOC: " + fileId.LOC;
}else{
//LOC = "FileLOC: N/A";
}
}
// add the licenseInfoInFile information in file
String licenseInfoInFile = "";
if((resulted)&&(fileId.licenseInfoInFile.size()>0)){
for(License licenseInfo : fileId.licenseInfoInFile)
licenseInfoInFile += addText(""
+ "LicenseInfoInFile: "
+ licenseInfo.getShortIdentifier()
);
}
//TODO There is no OS independent way to get date of file creation
// at least one Windows and Linux should be added when available
// String fileModified = utils.time.getTimeFromLong(file.lastModified());
String temp =
addSection("File")
+ addText("FileName: " + thisFile.getName())
+ addText("FilePath: " + filePath)
+ addText("FileType: OTHER")
+ addText("FileChecksum: SHA1: "
+ utils.Checksum.generateFileChecksum("SHA-1", thisFile))
+ addText("FileChecksum: SHA256: "
+ utils.Checksum.generateFileChecksum("SHA-256", thisFile))
+ addText("FileChecksum: MD5: "
+ utils.Checksum.generateFileChecksum("MD5", thisFile))
+ addText("FileChecksum: SSDEEP: " + ssdeep)
+ addText("FileSize: " + fileSize)
+ addText(LOC)
//+ addText("FileLastModified: " + fileModified)
+ licenseInfoInFile
//+ "\nLicenseConcluded: NOASSERTION"
//+ "\nLicenseInfoInFile: NONE"
//+ "\nFileCopyrightText: NONE"
;
output = output + temp;
// increase the counter
filesProcessed++;
}
// create the file pointer
File document = new File(folderProduct, filename);
utils.files.SaveStringToFile(document, output);
file = document; // keep this around for future reference
// done
isProcessing = false;
isOk = true;
return filename;
}
/**
* Provides an ASCII paragraph
* @param title the header text of the title
* @return the ASCII text to be included on the final output
*/
private String addParagraph(String title){
return "##-------------------------\n"
+ "## " + title + "\n"
+ "##-------------------------\n"
+ "\n";
}
private String addSection(String title){
return "\n"
+ "## " + title + "\n"
+ "\n"
;
}
/**
* Adds a simple line of text with a carriage return at the end
* @param text the text to be included
* @return a formatted string ready to be written at an ASCII file
*/
private String addText(String text){
if(text.isEmpty()){
return "";
}
return text + "\n";
}
/**
* Get the current time and date in SPDX format
* @return the properly formatted SPDX time format
*/
public static String getDateSPDX() {
// do the time calculation such as 2012-09-03T13:32:12Z
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String textDate = dateFormat.format(date);
//TODO for some reason "T" and "Z" are not accepted as parameters
return textDate.replace(" ", "T") + "Z";
}
}