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
|
/* *******************************************************************
* Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
* All rights reserved.
* This program and the accompanying materials are made available
* under the terms of the Eclipse Public License v 2.0
* which accompanies this distribution and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
*
* Contributors:
* Xerox/PARC initial implementation
* ******************************************************************/
package org.aspectj.internal.tools.ant.taskdefs;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import java.io.*;
import java.util.*;
/**
* Check that included .java files contain license and copyright strings for MPL 1.0 (default), Apache, or CPL. Use list="true" to
* get a list of known license variants {license}-{copyrightHolder} todo reimplement with regexp and jdiff FileLine utilities
*/
@SuppressWarnings("deprecation")
public class Checklics extends MatchingTask {
/*
* This does not enforce that copyrights are correct/current, only that they exist. E.g., the default behavior requires MPL but
* permits either Xerox or PARC copyright holders and any valid year.
*/
public static final String MPL_TAG = "mpl";
public static final String APACHE_TAG = "apache";
public static final String CPL_IBM_PARC_TAG = "cpl-ibm|parc";
public static final String CPL_IBM_TAG = "cpl-ibm";
public static final String MPL_XEROX_PARC_TAG = "mpl-parc|xerox";
public static final String MPL_ONLY_TAG = "mpl-only";
public static final String MPL_PARC_TAG = "mpl-parc";
public static final String PARC_COPYRIGHT_TAG = "parc-copy";
public static final String CPL_IBM_PARC_XEROX_TAG = "cpl-ibm|parc|xerox";
public static final String CPL_IBM_PARC_XEROX_OTHERS_TAG = "cpl-ibm|parc|xerox|others";
public static final String EPL_CPL_IBM_PARC_XEROX_OTHERS_TAG = "epl-cpl-ibm|parc|xerox|vmware|others";
public static final String DEFAULT = EPL_CPL_IBM_PARC_XEROX_OTHERS_TAG;
static final Map<String,License> LICENSES; // unmodifiable Map
static {
final String CONTRIBUTORS = "Contributors";
final String XEROX = "Xerox";
final String PARC = "Palo Alto Research Center";
final String APACHE = "The Apache Software Foundation";
final String IBM = "IBM";
final String VMWARE = "VMware";
final String IBM_LONG = "International Business Machines";
final String LIC_APL = "Apache Software Foundation (https://www.apache.org/)";
final String LIC_MPL = "https://aspectj.org/MPL/";
final String LIC_CPL = "Eclipse Public License";
final String LIC_ECPL = " Public License";
License APL = new License(APACHE_TAG, LIC_APL, APACHE);
License MPL = new License(MPL_TAG, LIC_MPL, XEROX);
License MPL_XEROX_PARC = new License(DEFAULT, LIC_MPL, XEROX, PARC);
License CPL_IBM_PARC = new License(CPL_IBM_PARC_TAG, LIC_CPL, new String[] { IBM_LONG, IBM, PARC });
License CPL_IBM_PARC_XEROX = new License(CPL_IBM_PARC_XEROX_TAG, LIC_CPL, new String[] { IBM_LONG, IBM, PARC, XEROX });
License CPL_IBM_PARC_XEROX_OTHERS = new License(CPL_IBM_PARC_XEROX_OTHERS_TAG, LIC_CPL, new String[] { IBM_LONG, IBM, PARC,
XEROX, CONTRIBUTORS });
License EPL_CPL_IBM_PARC_XEROX_OTHERS = new License(EPL_CPL_IBM_PARC_XEROX_OTHERS_TAG, LIC_ECPL, new String[] { IBM_LONG,
IBM, PARC, XEROX, VMWARE, CONTRIBUTORS });
License CPL_IBM = new License(CPL_IBM_TAG, LIC_CPL, IBM, IBM_LONG);
License MPL_ONLY = new License(MPL_ONLY_TAG, LIC_MPL);
License MPL_PARC = new License(MPL_PARC_TAG, LIC_MPL, PARC);
License PARC_COPYRIGHT = new License(PARC_COPYRIGHT_TAG, null, PARC);
LICENSES = new Hashtable<>();
LICENSES.put(APL.tag, APL);
LICENSES.put(MPL.tag, MPL);
LICENSES.put(MPL_PARC.tag, MPL_PARC);
LICENSES.put(MPL_XEROX_PARC.tag, MPL_XEROX_PARC);
LICENSES.put(CPL_IBM_PARC.tag, CPL_IBM_PARC);
LICENSES.put(MPL_ONLY.tag, MPL_ONLY);
LICENSES.put(CPL_IBM.tag, CPL_IBM);
LICENSES.put(PARC_COPYRIGHT.tag, PARC_COPYRIGHT);
LICENSES.put(CPL_IBM_PARC_XEROX.tag, CPL_IBM_PARC_XEROX);
LICENSES.put(CPL_IBM_PARC_XEROX_OTHERS.tag, CPL_IBM_PARC_XEROX_OTHERS);
LICENSES.put(EPL_CPL_IBM_PARC_XEROX_OTHERS.tag, EPL_CPL_IBM_PARC_XEROX_OTHERS);
}
/** @param args String[] { < sourcepath > {, < licenseTag > } } */
public static void main(String[] args) {
switch (args.length) {
case 1:
runDirect(args[0], null, false);
break;
case 2:
runDirect(args[0], args[1], false);
break;
default:
String options = "{replace-headers|get-years|list|{licenseTag}}";
System.err.println("java {me} sourcepath " + options);
break;
}
}
/**
* Run the license check directly
*
* @param sourcepaths String[] of paths to source directories
* @param license the String tag for the license, if any
* @param failonerror boolean flag to pass to Checklics
* @throws IllegalArgumentException if sourcepaths is empty
* @return total number of failed licenses
*/
public static int runDirect(String sourcepath, String license, boolean failonerror) {
if ((null == sourcepath) || (1 > sourcepath.length())) {
throw new IllegalArgumentException("bad sourcepath: " + sourcepath);
}
Checklics me = new Checklics();
Project p = new Project();
p.setName("direct interface to Checklics");
p.setBasedir(".");
me.setProject(p);
me.setFailOnError(failonerror);
me.setSourcepath(new Path(p, sourcepath));
if (null != license) {
if ("replace-headers".equals(license)) {
me.setReplaceheaders(true);
} else if ("get-years".equals(license)) {
me.setGetYears(true);
} else if ("list".equals(license)) {
me.setList(true);
} else {
me.setLicense(license);
}
}
me.execute();
return me.failed;
}
private Path sourcepath;
private License license;
private boolean list;
private String streamTag;
private boolean failOnError;
private boolean getYears;
private boolean replaceHeaders;
private int failed;
private int passed;
private boolean printDirectories;
/** @param list if true, don't run but list known license tags */
public void setList(boolean list) {
this.list = list;
}
public void setPrintDirectories(boolean print) {
printDirectories = print;
}
/**
* When failOnError is true, if any file failed, throw BuildException listing number of files that file failed to pass license
* check
*
* @param fail if true, report errors by throwing BuildException
*/
public void setFailOnError(boolean fail) {
this.failOnError = fail;
}
/** @param tl mpl | apache | cpl */
public void setLicense(String tl) {
License input = LICENSES.get(tl);
if (null == input) {
throw new BuildException("no license known for " + tl);
}
license = input;
}
public void setSourcepath(Path path) {
if (sourcepath == null) {
sourcepath = path;
} else {
sourcepath.append(path);
}
}
public Path createSourcepath() {
return sourcepath == null ? (sourcepath = new Path(project)) : sourcepath.createPath();
}
public void setSourcepathRef(Reference id) {
createSourcepath().setRefid(id);
}
/** @param out "out" or "err" */
public void setOutputStream(String out) {
this.streamTag = out;
}
public void setReplaceheaders(boolean replaceHeaders) {
this.replaceHeaders = replaceHeaders;
}
public void setGetYears(boolean getYears) {
this.getYears = getYears;
}
/** list known licenses or check source tree */
@Override
public void execute() throws BuildException {
if (list) {
list();
} else if (replaceHeaders) {
replaceHeaders();
} else if (getYears) {
getYears();
} else {
checkLicenses();
}
}
private PrintStream getOut() {
return ("err".equals(streamTag) ? System.err : System.out);
}
interface FileVisitor {
void visit(File file);
}
/** visit all .java files in all directories... */
private void visitAll(FileVisitor visitor) {
// List filelist = new ArrayList();
String[] dirs = sourcepath.list();
for (String dir2 : dirs) {
File dir = project.resolveFile(dir2);
String[] files = getDirectoryScanner(dir).getIncludedFiles();
for (String file2 : files) {
File file = new File(dir, file2);
String path = file.getPath();
if (path.endsWith(".java")) {
visitor.visit(file);
}
}
}
}
private void replaceHeaders() {
class YearVisitor implements FileVisitor {
@Override
public void visit(File file) {
HeaderInfo info = Header.checkFile(file);
if (!Header.replaceHeader(file, info)) {
throw new BuildException("failed to replace header for " + file + " using " + info);
}
}
}
visitAll(new YearVisitor());
}
private void getYears() {
final PrintStream out = getOut();
class YearVisitor implements FileVisitor {
@Override
public void visit(File file) {
HeaderInfo info = Header.checkFile(file);
out.println(info.toString());
}
}
visitAll(new YearVisitor());
}
private void checkLicenses() throws BuildException {
if (null == license) {
setLicense(DEFAULT);
}
final License license = this.license; // being paranoid...
if (null == license) {
throw new BuildException("no license");
}
final PrintStream out = getOut();
class Visitor implements FileVisitor {
int failed = 0;
int passed = 0;
@Override
public void visit(File file) {
if (license.checkFile(file)) {
passed++;
} else {
failed++;
String path = file.getPath();
if (!license.foundLicense()) {
out.println(license.tag + " LICENSE FAIL: " + path);
}
if (!license.foundCopyright()) {
out.println(license.tag + " COPYRIGHT FAIL: " + path);
}
}
}
}
Visitor visitor = new Visitor();
visitAll(visitor);
this.failed = visitor.failed;
this.passed = visitor.passed;
if (0 < visitor.failed) {
getOut().println("Total passed: " + visitor.passed + (visitor.failed == 0 ? "" : " failed: " + visitor.failed));
if (failOnError) {
throw new BuildException(failed + " files failed license check");
}
}
}
private void list() {
Iterator enu = LICENSES.keySet().iterator();
StringBuilder sb = new StringBuilder();
sb.append("known license keys:");
boolean first = true;
while (enu.hasNext()) {
sb.append((first ? " " : ", ") + enu.next());
if (first) {
first = false;
}
}
getOut().println(sb.toString());
}
/**
* Encapsulate license and copyright specifications to check files use hokey string matching.
*/
public static class License {
/** acceptable years for copyright prefix to company - append " " */
static final String[] YEARS = // remove older after license xfer?
new String[] {
"1999",
"2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009",
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019",
"2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029"
};
public final String tag;
public final String license;
private final String[] copyright;
private boolean gotLicense;
private boolean gotCopyright;
License(String tag, String license) {
this(tag, license, (String[]) null);
}
License(String tag, String license, String copyright) {
this(tag, license, new String[] { copyright });
}
License(String tag, String license, String copyright, String altCopyright) {
this(tag, license, new String[] { copyright, altCopyright });
}
License(String tag, String license, String[] copyright) {
this.tag = tag;
if ((null == tag) || (0 == tag.length())) {
throw new IllegalArgumentException("null tag");
}
this.license = license;
this.copyright = copyright;
}
public final boolean gotValidFile() {
return foundLicense() && foundCopyright();
}
/** @return true if no license sought or if some license found */
public final boolean foundLicense() {
return ((null == license) || gotLicense);
}
/** @return true if no copyright sought or if some copyright found */
public final boolean foundCopyright() {
return ((null == copyright) || gotCopyright);
}
public boolean checkFile(final File file) {
clear();
// boolean result = false;
BufferedReader input = null;
int lineNum = 0;
try {
input = new BufferedReader(new FileReader(file));
String line;
while (!gotValidFile() && (line = input.readLine()) != null) {
lineNum++;
checkLine(line);
}
} catch (IOException e) {
System.err.println("reading line " + lineNum + " of " + file);
e.printStackTrace(System.err);
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
} // ignore
}
}
return gotValidFile();
}
@Override
public String toString() {
return tag;
}
private void checkLine(String line) {
if ((null == line) || (0 == line.length())) {
return;
}
if (!gotLicense && (null != license) && (line.contains(license))) {
gotLicense = true;
}
if (!gotCopyright && (null != copyright)) {
int loc;
for (int j = 0; !gotCopyright && (j < YEARS.length); j++) {
if (-1 != (loc = line.indexOf(YEARS[j]))) {
loc += YEARS[j].length();
String afterLoc = line.substring(loc).trim();
for (int i = 0; !gotCopyright && (i < copyright.length); i++) {
if (0 == afterLoc.indexOf(copyright[i])) {
gotCopyright = true;
}
}
}
}
}
}
private void clear() {
if (gotLicense) {
gotLicense = false;
}
if (gotCopyright) {
gotCopyright = false;
}
}
} // class License
}
class HeaderInfo {
/** File for which this is the info */
public final File file;
/** unmodifiable List of String years */
public final List years;
/** last line of license */
public final int lastLine;
/** last line of license */
public final boolean hasLicense;
public HeaderInfo(File file, int lastLine, List<String> years, boolean hasLicense) {
this.lastLine = lastLine;
this.file = file;
this.hasLicense = hasLicense;
List<String> newYears = new ArrayList<>(years);
Collections.sort(newYears);
this.years = Collections.unmodifiableList(newYears);
if ((null == file) || !file.canWrite()) {
throw new IllegalArgumentException("bad file: " + this);
}
if (!hasLicense) {
if ((0 > lastLine) || (65 < lastLine)) {
throw new IllegalArgumentException("bad last line: " + this);
}
} else {
if ((null == years) || (1 > years.size())) {
throw new IllegalArgumentException("no years: " + this);
}
if ((20 > lastLine) || (65 < lastLine)) {
throw new IllegalArgumentException("bad last line: " + this);
}
}
}
@Override
public String toString() {
return file.getPath() + ":" + lastLine + " " + years;
}
public void writeHeader(PrintWriter writer) {
if (!hasLicense) {
writer.println(TOP);
writer.println(PARC_ONLY);
writeRest(writer);
} else {
final int size = years.size();
if (1 > size) {
throw new Error("no years found in " + toString());
}
String first = (String) years.get(0);
String last = (String) years.get(size - 1);
boolean lastIs2002 = "2002".equals(last);
String xlast = last;
if (lastIs2002) { // 2002 was PARC
xlast = (String) (size > 1 ? years.get(size - 2) : null);
// 1999-2002 Xerox implies 1999-2001 Xerox
if (first.equals(xlast) && !"2001".equals(xlast)) {
xlast = "2001";
}
}
String xyears = first + "-" + xlast;
if (first.equals(last)) {
xyears = first;
}
writer.println(TOP);
if (!lastIs2002) { // Xerox only
writer.println(XEROX_PREFIX + xyears + XEROX_SUFFIX + ". ");
} else if (size == 1) { // PARC only
writer.println(PARC_ONLY);
} else { // XEROX plus PARC
writer.println(XEROX_PREFIX + xyears + XEROX_SUFFIX + ", ");
writer.println(PARC);
}
writeRest(writer);
}
}
void writeRest(PrintWriter writer) {
writer.println(" * All rights reserved.");
writer.println(" * This program and the accompanying materials are made available");
writer.println(" * under the terms of the Eclipse Public License v 2.0");
writer.println(" * which accompanies this distribution and is available at");
writer.println(" * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt");
writer.println(" * ");
writer.println(" * Contributors:");
writer.println(" * Xerox/PARC initial implementation");
writer.println(" * ******************************************************************/");
writer.println("");
}
public static final String TOP = "/* *******************************************************************";
public static final String PARC = " * 2002 Palo Alto Research Center, Incorporated (PARC).";
public static final String PARC_ONLY = " * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).";
public static final String XEROX_PREFIX = " * Copyright (c) ";
public static final String XEROX_SUFFIX = " Xerox Corporation";
}
/**
* header search/replace using hokey string matching
*/
class Header {
/** replace the header in file */
public static boolean replaceHeader(File file, HeaderInfo info) {
// ArrayList years = new ArrayList();
// int endLine = 0;
BufferedReader input = null;
PrintWriter output = null;
FileWriter outWriter = null;
int lineNum = 0;
boolean result = false;
final File inFile = new File(file.getPath() + ".tmp");
try {
File outFile = new File(file.getPath());
if (!file.renameTo(inFile) || !inFile.canRead()) {
throw new Error("unable to rename " + file + " to " + inFile);
}
outWriter = new FileWriter(outFile);
input = new BufferedReader(new FileReader(inFile));
output = new PrintWriter(outWriter, true);
info.writeHeader(output);
String line;
while (null != (line = input.readLine())) {
lineNum++;
if (lineNum > info.lastLine) {
output.println(line);
}
}
} catch (IOException e) {
System.err.println("writing line " + lineNum + " of " + file);
e.printStackTrace(System.err);
result = false;
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
result = false;
}
}
if (null != outWriter) {
try {
outWriter.close();
} catch (IOException e) {
result = false;
}
}
result = inFile.delete();
}
return result;
}
public static HeaderInfo checkFile(final File file) {
ArrayList<String> years = new ArrayList<>();
int endLine = 0;
BufferedReader input = null;
int lineNum = 0;
try {
input = new BufferedReader(new FileReader(file));
String line;
while (null != (line = input.readLine())) {
lineNum++;
String ll = line.trim();
if (ll.startsWith("package ") || ll.startsWith("import ")) {
break; // ignore default package w/o imports
}
if (checkLine(line, years)) {
endLine = lineNum;
break;
}
}
} catch (IOException e) {
System.err.println("reading line " + lineNum + " of " + file);
e.printStackTrace(System.err);
} finally {
if (null != input) {
try {
input.close();
} catch (IOException e) {
} // ignore
}
}
return new HeaderInfo(file, endLine, years, endLine > 0);
}
/**
* Add any years found (as String) to years, and return true at the first end-of-comment
*
* @return true if this line has end-of-comment
*/
private static boolean checkLine(String line, List<String> years) {
if ((null == line) || (0 == line.length())) {
return false;
}
int loc;
int start = 0;
while ((-1 != (loc = line.indexOf("199", start)) || (-1 != (loc = line.indexOf("200", start))))) {
char c = line.charAt(loc + 3);
if ((c <= '9') && (c >= '0')) {
years.add(line.substring(loc, loc + 4));
}
start = loc + 4;
}
return (line.contains("*/"));
}
} // class Header
|