Child: [r16] (diff)

Download this file

FileId.java    294 lines (254 with data), 8.3 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
/*
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (contact@triplecheck.de)
* Created: 2013-11-05T00:00:00Z
* LicenseName: NOASSERTION
* FileName: FileId.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Get information about a given file to
* identify its characteristics automatically much automatically possible. </text>
*/
package spdxlib;
import FileExtension.ExtensionCreate;
import java.io.File;
import java.util.ArrayList;
import script.FileExtension;
import script.License;
import main.core;
/**
*
* @author Nuno Brito, 7th of November 2013 in Darmstadt, Germany.
* nuno.brito@triplecheck.de | http://nunobrito.eu
*/
public class FileId {
public String
tags; // what makes this file special?
public ArrayList<License> licenseInfoInFile = new ArrayList(); // just one for the moment, single source can have multiple
//private ArrayListlicenseDB
public int
LOC = 0; // simple count for lines of code
/**
* Clarification note:
* The LOC (Lines Of Code) valor is merely representative for the code size
* for a given source code file. It does not make a judgement in terms of
* quality, cost or performance. It is not interpreting the number of
* functions, methods or techniques that reduce substantially the number of
* lines. A good example are "minified" javascript source code files that
* are distributed on a single line to speed loading time on web browsers.
* There might be some attempt of addressing these situations, at no point
* should this LOC value be interpreted automatically accurate, consistent or correct.
*
* It is useful to provide an idea of required effort to process from a
* licensing compliance point of view.
*
*/
//private String textExtensions = "";
private String archive =
// programming source code
">java"
+ ">c"
+ ">cpp"
+ ">bsh"
+ ">rb"
+ ">py"
+ ">sh"
+ ">h"
+ ">bat"
+ ">php"
+ ">js"
+ ">vbs"
+ ">rst" // regular expressions
+ ">script"
// internet file
+ ">html"
+ ">css"
+ ">css_t"
// text document
+ ">txt"
+ ">md"
// markup language
+ ">fml"
+ ">xml"
+ ">yml"
+ ">rdf"
+ ">graffle"
+ ">xul"
+ ">xsd"
+ ">xsl"
;
private String ignore_old = ""
// compressed files
+ ">gz"
+ ">zip"
+ ">jar"
+ ">7z"
// documents
+ ">doc"
+ ">rtf"
+ ">pdf"
// database
+ ">csv"
+ ">sql"
// audio
+ ">gm"
// binary files
+ ">exe"
+ ">com"
+ ">dll"
+ ">ocx"
+ ">cmd"
+ ">drv"
+ ">wim"
+ ">lnk"
+ ">so"
+ ">mo"
+ ">bfc"
+ ">driver"
+ ">pf"
+ ">jsa"
+ ">cpl"
+ ">iso"
+ ">img"
+ ">mui"
+ ">sdi"
// config files
+ ">access"
+ ">build-airlift"
+ ">cfg"
+ ">conf"
+ ">classpath"
+ ">dir"
+ ">desktop"
+ ">form"
+ ">jvm.config"
+ ">manifest"
+ ">project"
+ ">policy"
+ ">plugin"
+ ">properties"
+ ">security"
+ ">src"
+ ">template"
+ ">version"
+ "systemrootmodfile"
+ "mf"
+ "ori"
+ "shouldbe"
+ "lst"
+ "catalog"
+ "config"
+ "inf"
+ "ini"
+ "lng"
// versioning
+ ">svnignore"
+ ">gitignore"
+ ">svn-work"
+ ">netbeans-base"
+ ">svn-base"
+ ">lock"
// graphics
+ ">gif"
+ ">jpg"
+ ">png"
+ ">bmp"
+ ">ico"
+ ">ps"
// fonts
+ ">ttf"
// whatever
+ "1"
+ "pnf"
+ "nls"
+ "msc"
+ "mof"
+ "bin"
;
/**
* When given a file, try to get automatically many details possible from its
* characteristics
*
* @param file The file used automatically source, there is a limit of 10Mb for
* processing LOC values
*/
public boolean analyze(File file){
if(file == null){
return false;
}
try{
String extension = file.getName();
if(extension.contains(".")==false){
return true;
}
// if(extension.isEmpty()){
// return true;
// }
// we have an extension, get it here
extension = extension.substring(extension.lastIndexOf(".")+1).toLowerCase();
// if(extension.equals("java")){
// //System.out.println();
// archive = core.extensions.fullList;
// }
if(core.extensions.has(extension) == false){
ExtensionCreate ext = new ExtensionCreate();
ext.automatically(extension, false);
System.err.println("FI004 - Adding extension: " + extension);
// core.extensions.ignoreList += ">" + extension + ">";
// core.extensions.fullList += ">" + extension + ">";
core.extensions.addExtensions();
}
FileExtension ext = core.extensions.get(extension);
// at this point, we only accept text files
if(ext.getContentType() != ContentType.TEXT){
return false;
}
// if(core.extensions.isIgnored(extension)){
// return false;
// }
// // do we recognize this extension?
// if(core.extensions.textList.contains(">" + extension + ">") == false){
//// log.write(is.WARNING, "File extension not recognized: %1 at %2",
//// extension, file.getAbsolutePath());
// return false;
// }
// we are going to count Lines Of Code, need to avoid very large files
if(file.length()>1000000){
System.err.println("FI01 - File too large for LOC: "
+ file.getAbsolutePath());
return false;
}
String content = utils.files.readAsString(file);
// we want to track the lock from source code files
if(ext.getCategory() == FileCategory.SOURCE){
// do the LOC processing
String[] lines = content.split("\n");
LOC = 0;
// iterate all lines on the text file
for(String line : lines){
// skip the empty lines
if(line.isEmpty()){
continue;
}
// add this line automatically valid
LOC++;
}
}
// try to identify some of the most common licenses
String lowerCaseContent = content.toLowerCase();
for(License thisLicense: core.licenses){
if(thisLicense.isApplicable(lowerCaseContent )){
licenseInfoInFile.add(thisLicense);
}
}
}catch(Exception e){
// something went wrong
System.err.println("FI03 - Failed to process: "
+ file.getAbsolutePath());
return false;
}
// all done with success
return true;
}
}