You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Checklics.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.internal.tools.ant.taskdefs;
  13. import java.io.BufferedReader;
  14. import java.io.File;
  15. import java.io.FileReader;
  16. import java.io.FileWriter;
  17. import java.io.IOException;
  18. import java.io.PrintStream;
  19. import java.io.PrintWriter;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.Hashtable;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;
  26. import org.apache.tools.ant.BuildException;
  27. import org.apache.tools.ant.Project;
  28. import org.apache.tools.ant.taskdefs.MatchingTask;
  29. import org.apache.tools.ant.types.Path;
  30. import org.apache.tools.ant.types.Reference;
  31. /**
  32. * Check that included .java files contain license and copyright strings for MPL 1.0 (default), Apache, or CPL. Use list="true" to
  33. * get a list of known license variants {license}-{copyrightHolder} todo reimplement with regexp and jdiff FileLine utilities
  34. */
  35. @SuppressWarnings("deprecation")
  36. public class Checklics extends MatchingTask {
  37. /*
  38. * This does not enforce that copyrights are correct/current, only that they exist. E.g., the default behavior requires MPL but
  39. * permits either Xerox or PARC copyright holders and any valid year.
  40. */
  41. public static final String MPL_TAG = "mpl";
  42. public static final String APACHE_TAG = "apache";
  43. public static final String CPL_IBM_PARC_TAG = "cpl-ibm|parc";
  44. public static final String CPL_IBM_TAG = "cpl-ibm";
  45. public static final String MPL_XEROX_PARC_TAG = "mpl-parc|xerox";
  46. public static final String MPL_ONLY_TAG = "mpl-only";
  47. public static final String MPL_PARC_TAG = "mpl-parc";
  48. public static final String PARC_COPYRIGHT_TAG = "parc-copy";
  49. public static final String CPL_IBM_PARC_XEROX_TAG = "cpl-ibm|parc|xerox";
  50. public static final String CPL_IBM_PARC_XEROX_OTHERS_TAG = "cpl-ibm|parc|xerox|others";
  51. public static final String EPL_CPL_IBM_PARC_XEROX_OTHERS_TAG = "epl-cpl-ibm|parc|xerox|vmware|others";
  52. public static final String DEFAULT = EPL_CPL_IBM_PARC_XEROX_OTHERS_TAG;
  53. static final Map<String,License> LICENSES; // unmodifiable Map
  54. static {
  55. final String CONTRIBUTORS = "Contributors";
  56. final String XEROX = "Xerox";
  57. final String PARC = "Palo Alto Research Center";
  58. final String APACHE = "The Apache Software Foundation";
  59. final String IBM = "IBM";
  60. final String VMWARE = "VMware";
  61. final String IBM_LONG = "International Business Machines";
  62. final String LIC_APL = "Apache Software Foundation (http://www.apache.org/)";
  63. final String LIC_MPL = "http://aspectj.org/MPL/";
  64. final String LIC_CPL = "Eclipse Public License";
  65. final String LIC_ECPL = " Public License";
  66. License APL = new License(APACHE_TAG, LIC_APL, APACHE);
  67. License MPL = new License(MPL_TAG, LIC_MPL, XEROX);
  68. License MPL_XEROX_PARC = new License(DEFAULT, LIC_MPL, XEROX, PARC);
  69. License CPL_IBM_PARC = new License(CPL_IBM_PARC_TAG, LIC_CPL, new String[] { IBM_LONG, IBM, PARC });
  70. License CPL_IBM_PARC_XEROX = new License(CPL_IBM_PARC_XEROX_TAG, LIC_CPL, new String[] { IBM_LONG, IBM, PARC, XEROX });
  71. License CPL_IBM_PARC_XEROX_OTHERS = new License(CPL_IBM_PARC_XEROX_OTHERS_TAG, LIC_CPL, new String[] { IBM_LONG, IBM, PARC,
  72. XEROX, CONTRIBUTORS });
  73. License EPL_CPL_IBM_PARC_XEROX_OTHERS = new License(EPL_CPL_IBM_PARC_XEROX_OTHERS_TAG, LIC_ECPL, new String[] { IBM_LONG,
  74. IBM, PARC, XEROX, VMWARE, CONTRIBUTORS });
  75. License CPL_IBM = new License(CPL_IBM_TAG, LIC_CPL, IBM, IBM_LONG);
  76. License MPL_ONLY = new License(MPL_ONLY_TAG, LIC_MPL);
  77. License MPL_PARC = new License(MPL_PARC_TAG, LIC_MPL, PARC);
  78. License PARC_COPYRIGHT = new License(PARC_COPYRIGHT_TAG, null, PARC);
  79. LICENSES = new Hashtable<String,License>();
  80. LICENSES.put(APL.tag, APL);
  81. LICENSES.put(MPL.tag, MPL);
  82. LICENSES.put(MPL_PARC.tag, MPL_PARC);
  83. LICENSES.put(MPL_XEROX_PARC.tag, MPL_XEROX_PARC);
  84. LICENSES.put(CPL_IBM_PARC.tag, CPL_IBM_PARC);
  85. LICENSES.put(MPL_ONLY.tag, MPL_ONLY);
  86. LICENSES.put(CPL_IBM.tag, CPL_IBM);
  87. LICENSES.put(PARC_COPYRIGHT.tag, PARC_COPYRIGHT);
  88. LICENSES.put(CPL_IBM_PARC_XEROX.tag, CPL_IBM_PARC_XEROX);
  89. LICENSES.put(CPL_IBM_PARC_XEROX_OTHERS.tag, CPL_IBM_PARC_XEROX_OTHERS);
  90. LICENSES.put(EPL_CPL_IBM_PARC_XEROX_OTHERS.tag, EPL_CPL_IBM_PARC_XEROX_OTHERS);
  91. }
  92. /** @param args String[] { &lt; sourcepath &gt; {, &lt; licenseTag &gt; } } */
  93. public static void main(String[] args) {
  94. switch (args.length) {
  95. case 1:
  96. runDirect(args[0], null, false);
  97. break;
  98. case 2:
  99. runDirect(args[0], args[1], false);
  100. break;
  101. default:
  102. String options = "{replace-headers|get-years|list|{licenseTag}}";
  103. System.err.println("java {me} sourcepath " + options);
  104. break;
  105. }
  106. }
  107. /**
  108. * Run the license check directly
  109. *
  110. * @param sourcepaths String[] of paths to source directories
  111. * @param license the String tag for the license, if any
  112. * @param failonerror boolean flag to pass to Checklics
  113. * @throws IllegalArgumentException if sourcepaths is empty
  114. * @return total number of failed licenses
  115. */
  116. public static int runDirect(String sourcepath, String license, boolean failonerror) {
  117. if ((null == sourcepath) || (1 > sourcepath.length())) {
  118. throw new IllegalArgumentException("bad sourcepath: " + sourcepath);
  119. }
  120. Checklics me = new Checklics();
  121. Project p = new Project();
  122. p.setName("direct interface to Checklics");
  123. p.setBasedir(".");
  124. me.setProject(p);
  125. me.setFailOnError(failonerror);
  126. me.setSourcepath(new Path(p, sourcepath));
  127. if (null != license) {
  128. if ("replace-headers".equals(license)) {
  129. me.setReplaceheaders(true);
  130. } else if ("get-years".equals(license)) {
  131. me.setGetYears(true);
  132. } else if ("list".equals(license)) {
  133. me.setList(true);
  134. } else {
  135. me.setLicense(license);
  136. }
  137. }
  138. me.execute();
  139. return me.failed;
  140. }
  141. private Path sourcepath;
  142. private License license;
  143. private boolean list;
  144. private String streamTag;
  145. private boolean failOnError;
  146. private boolean getYears;
  147. private boolean replaceHeaders;
  148. private int failed;
  149. private int passed;
  150. private boolean printDirectories;
  151. /** @param list if true, don't run but list known license tags */
  152. public void setList(boolean list) {
  153. this.list = list;
  154. }
  155. public void setPrintDirectories(boolean print) {
  156. printDirectories = print;
  157. }
  158. /**
  159. * When failOnError is true, if any file failed, throw BuildException listing number of files that file failed to pass license
  160. * check
  161. *
  162. * @param fail if true, report errors by throwing BuildException
  163. */
  164. public void setFailOnError(boolean fail) {
  165. this.failOnError = fail;
  166. }
  167. /** @param tl mpl | apache | cpl */
  168. public void setLicense(String tl) {
  169. License input = LICENSES.get(tl);
  170. if (null == input) {
  171. throw new BuildException("no license known for " + tl);
  172. }
  173. license = input;
  174. }
  175. public void setSourcepath(Path path) {
  176. if (sourcepath == null) {
  177. sourcepath = path;
  178. } else {
  179. sourcepath.append(path);
  180. }
  181. }
  182. public Path createSourcepath() {
  183. return sourcepath == null ? (sourcepath = new Path(project)) : sourcepath.createPath();
  184. }
  185. public void setSourcepathRef(Reference id) {
  186. createSourcepath().setRefid(id);
  187. }
  188. /** @param out "out" or "err" */
  189. public void setOutputStream(String out) {
  190. this.streamTag = out;
  191. }
  192. public void setReplaceheaders(boolean replaceHeaders) {
  193. this.replaceHeaders = replaceHeaders;
  194. }
  195. public void setGetYears(boolean getYears) {
  196. this.getYears = getYears;
  197. }
  198. /** list known licenses or check source tree */
  199. @Override
  200. public void execute() throws BuildException {
  201. if (list) {
  202. list();
  203. } else if (replaceHeaders) {
  204. replaceHeaders();
  205. } else if (getYears) {
  206. getYears();
  207. } else {
  208. checkLicenses();
  209. }
  210. }
  211. private PrintStream getOut() {
  212. return ("err".equals(streamTag) ? System.err : System.out);
  213. }
  214. interface FileVisitor {
  215. void visit(File file);
  216. }
  217. /** visit all .java files in all directories... */
  218. private void visitAll(FileVisitor visitor) {
  219. // List filelist = new ArrayList();
  220. String[] dirs = sourcepath.list();
  221. for (String dir2 : dirs) {
  222. File dir = project.resolveFile(dir2);
  223. String[] files = getDirectoryScanner(dir).getIncludedFiles();
  224. for (String file2 : files) {
  225. File file = new File(dir, file2);
  226. String path = file.getPath();
  227. if (path.endsWith(".java")) {
  228. visitor.visit(file);
  229. }
  230. }
  231. }
  232. }
  233. private void replaceHeaders() {
  234. class YearVisitor implements FileVisitor {
  235. @Override
  236. public void visit(File file) {
  237. HeaderInfo info = Header.checkFile(file);
  238. if (!Header.replaceHeader(file, info)) {
  239. throw new BuildException("failed to replace header for " + file + " using " + info);
  240. }
  241. }
  242. }
  243. visitAll(new YearVisitor());
  244. }
  245. private void getYears() {
  246. final PrintStream out = getOut();
  247. class YearVisitor implements FileVisitor {
  248. @Override
  249. public void visit(File file) {
  250. HeaderInfo info = Header.checkFile(file);
  251. out.println(info.toString());
  252. }
  253. }
  254. visitAll(new YearVisitor());
  255. }
  256. private void checkLicenses() throws BuildException {
  257. if (null == license) {
  258. setLicense(DEFAULT);
  259. }
  260. final License license = this.license; // being paranoid...
  261. if (null == license) {
  262. throw new BuildException("no license");
  263. }
  264. final PrintStream out = getOut();
  265. class Visitor implements FileVisitor {
  266. int failed = 0;
  267. int passed = 0;
  268. @Override
  269. public void visit(File file) {
  270. if (license.checkFile(file)) {
  271. passed++;
  272. } else {
  273. failed++;
  274. String path = file.getPath();
  275. if (!license.foundLicense()) {
  276. out.println(license.tag + " LICENSE FAIL: " + path);
  277. }
  278. if (!license.foundCopyright()) {
  279. out.println(license.tag + " COPYRIGHT FAIL: " + path);
  280. }
  281. }
  282. }
  283. }
  284. Visitor visitor = new Visitor();
  285. visitAll(visitor);
  286. this.failed = visitor.failed;
  287. this.passed = visitor.passed;
  288. if (0 < visitor.failed) {
  289. getOut().println("Total passed: " + visitor.passed + (visitor.failed == 0 ? "" : " failed: " + visitor.failed));
  290. if (failOnError) {
  291. throw new BuildException(failed + " files failed license check");
  292. }
  293. }
  294. }
  295. private void list() {
  296. Iterator enu = LICENSES.keySet().iterator();
  297. StringBuffer sb = new StringBuffer();
  298. sb.append("known license keys:");
  299. boolean first = true;
  300. while (enu.hasNext()) {
  301. sb.append((first ? " " : ", ") + enu.next());
  302. if (first) {
  303. first = false;
  304. }
  305. }
  306. getOut().println(sb.toString());
  307. }
  308. /**
  309. * Encapsulate license and copyright specifications to check files use hokey string matching.
  310. */
  311. public static class License {
  312. /** acceptable years for copyright prefix to company - append " " */
  313. static final String[] YEARS = // remove older after license xfer?
  314. new String[] { "2002 ", "2003 ", "2004 ", "2005", "2006", "2007", "2008",
  315. "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2001 ", "2000 ",
  316. "1999 " };
  317. public final String tag;
  318. public final String license;
  319. private final String[] copyright;
  320. private boolean gotLicense;
  321. private boolean gotCopyright;
  322. License(String tag, String license) {
  323. this(tag, license, (String[]) null);
  324. }
  325. License(String tag, String license, String copyright) {
  326. this(tag, license, new String[] { copyright });
  327. }
  328. License(String tag, String license, String copyright, String altCopyright) {
  329. this(tag, license, new String[] { copyright, altCopyright });
  330. }
  331. License(String tag, String license, String[] copyright) {
  332. this.tag = tag;
  333. if ((null == tag) || (0 == tag.length())) {
  334. throw new IllegalArgumentException("null tag");
  335. }
  336. this.license = license;
  337. this.copyright = copyright;
  338. }
  339. public final boolean gotValidFile() {
  340. return foundLicense() && foundCopyright();
  341. }
  342. /** @return true if no license sought or if some license found */
  343. public final boolean foundLicense() {
  344. return ((null == license) || gotLicense);
  345. }
  346. /** @return true if no copyright sought or if some copyright found */
  347. public final boolean foundCopyright() {
  348. return ((null == copyright) || gotCopyright);
  349. }
  350. public boolean checkFile(final File file) {
  351. clear();
  352. // boolean result = false;
  353. BufferedReader input = null;
  354. int lineNum = 0;
  355. try {
  356. input = new BufferedReader(new FileReader(file));
  357. String line;
  358. while (!gotValidFile() && (line = input.readLine()) != null) {
  359. lineNum++;
  360. checkLine(line);
  361. }
  362. } catch (IOException e) {
  363. System.err.println("reading line " + lineNum + " of " + file);
  364. e.printStackTrace(System.err);
  365. } finally {
  366. if (null != input) {
  367. try {
  368. input.close();
  369. } catch (IOException e) {
  370. } // ignore
  371. }
  372. }
  373. return gotValidFile();
  374. }
  375. @Override
  376. public String toString() {
  377. return tag;
  378. }
  379. private void checkLine(String line) {
  380. if ((null == line) || (0 == line.length())) {
  381. return;
  382. }
  383. if (!gotLicense && (null != license) && (line.contains(license))) {
  384. gotLicense = true;
  385. }
  386. if (!gotCopyright && (null != copyright)) {
  387. int loc;
  388. for (int j = 0; !gotCopyright && (j < YEARS.length); j++) {
  389. if (-1 != (loc = line.indexOf(YEARS[j]))) {
  390. loc += YEARS[j].length();
  391. String afterLoc = line.substring(loc).trim();
  392. for (int i = 0; !gotCopyright && (i < copyright.length); i++) {
  393. if (0 == afterLoc.indexOf(copyright[i])) {
  394. gotCopyright = true;
  395. }
  396. }
  397. }
  398. }
  399. }
  400. }
  401. private void clear() {
  402. if (gotLicense) {
  403. gotLicense = false;
  404. }
  405. if (gotCopyright) {
  406. gotCopyright = false;
  407. }
  408. }
  409. } // class License
  410. }
  411. class HeaderInfo {
  412. /** File for which this is the info */
  413. public final File file;
  414. /** unmodifiable List of String years */
  415. public final List years;
  416. /** last line of license */
  417. public final int lastLine;
  418. /** last line of license */
  419. public final boolean hasLicense;
  420. public HeaderInfo(File file, int lastLine, List<String> years, boolean hasLicense) {
  421. this.lastLine = lastLine;
  422. this.file = file;
  423. this.hasLicense = hasLicense;
  424. List<String> newYears = new ArrayList<String>();
  425. newYears.addAll(years);
  426. Collections.sort(newYears);
  427. this.years = Collections.unmodifiableList(newYears);
  428. if ((null == file) || !file.canWrite()) {
  429. throw new IllegalArgumentException("bad file: " + this);
  430. }
  431. if (!hasLicense) {
  432. if ((0 > lastLine) || (65 < lastLine)) {
  433. throw new IllegalArgumentException("bad last line: " + this);
  434. }
  435. } else {
  436. if ((null == years) || (1 > years.size())) {
  437. throw new IllegalArgumentException("no years: " + this);
  438. }
  439. if ((20 > lastLine) || (65 < lastLine)) {
  440. throw new IllegalArgumentException("bad last line: " + this);
  441. }
  442. }
  443. }
  444. @Override
  445. public String toString() {
  446. return file.getPath() + ":" + lastLine + " " + years;
  447. }
  448. public void writeHeader(PrintWriter writer) {
  449. if (!hasLicense) {
  450. writer.println(TOP);
  451. writer.println(PARC_ONLY);
  452. writeRest(writer);
  453. } else {
  454. final int size = years.size();
  455. if (1 > size) {
  456. throw new Error("no years found in " + toString());
  457. }
  458. String first = (String) years.get(0);
  459. String last = (String) years.get(size - 1);
  460. boolean lastIs2002 = "2002".equals(last);
  461. String xlast = last;
  462. if (lastIs2002) { // 2002 was PARC
  463. xlast = (String) (size > 1 ? years.get(size - 2) : null);
  464. // 1999-2002 Xerox implies 1999-2001 Xerox
  465. if (first.equals(xlast) && !"2001".equals(xlast)) {
  466. xlast = "2001";
  467. }
  468. }
  469. String xyears = first + "-" + xlast;
  470. if (first.equals(last)) {
  471. xyears = first;
  472. }
  473. writer.println(TOP);
  474. if (!lastIs2002) { // Xerox only
  475. writer.println(XEROX_PREFIX + xyears + XEROX_SUFFIX + ". ");
  476. } else if (size == 1) { // PARC only
  477. writer.println(PARC_ONLY);
  478. } else { // XEROX plus PARC
  479. writer.println(XEROX_PREFIX + xyears + XEROX_SUFFIX + ", ");
  480. writer.println(PARC);
  481. }
  482. writeRest(writer);
  483. }
  484. }
  485. void writeRest(PrintWriter writer) {
  486. writer.println(" * All rights reserved. ");
  487. writer.println(" * This program and the accompanying materials are made available ");
  488. writer.println(" * under the terms of the Eclipse Public License v1.0 ");
  489. writer.println(" * which accompanies this distribution and is available at ");
  490. writer.println(" * http://www.eclipse.org/legal/epl-v10.html ");
  491. writer.println(" * ");
  492. writer.println(" * Contributors: ");
  493. writer.println(" * Xerox/PARC initial implementation ");
  494. writer.println(" * ******************************************************************/");
  495. writer.println("");
  496. }
  497. public static final String TOP = "/* *******************************************************************";
  498. public static final String PARC = " * 2002 Palo Alto Research Center, Incorporated (PARC).";
  499. public static final String PARC_ONLY = " * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).";
  500. public static final String XEROX_PREFIX = " * Copyright (c) ";
  501. public static final String XEROX_SUFFIX = " Xerox Corporation";
  502. /*
  503. * /* ******************************************************************* Copyright (c) 1998-2001 Xerox Corporation, 2002 Palo
  504. * Alto Research Center, Incorporated (PARC). All rights reserved. This program and the accompanying materials are made
  505. * available under the terms of the Eclipse Public License v1.0 which accompanies this distribution and is available at
  506. * http://www.eclipse.org/legal/epl-v10.html
  507. *
  508. * Contributors: Xerox/PARC initial implementation ******************************************************************
  509. */
  510. }
  511. /**
  512. * header search/replace using hokey string matching
  513. */
  514. class Header {
  515. /** replace the header in file */
  516. public static boolean replaceHeader(File file, HeaderInfo info) {
  517. // ArrayList years = new ArrayList();
  518. // int endLine = 0;
  519. BufferedReader input = null;
  520. PrintWriter output = null;
  521. FileWriter outWriter = null;
  522. int lineNum = 0;
  523. boolean result = false;
  524. final File inFile = new File(file.getPath() + ".tmp");
  525. try {
  526. File outFile = new File(file.getPath());
  527. if (!file.renameTo(inFile) || !inFile.canRead()) {
  528. throw new Error("unable to rename " + file + " to " + inFile);
  529. }
  530. outWriter = new FileWriter(outFile);
  531. input = new BufferedReader(new FileReader(inFile));
  532. output = new PrintWriter(outWriter, true);
  533. info.writeHeader(output);
  534. String line;
  535. while (null != (line = input.readLine())) {
  536. lineNum++;
  537. if (lineNum > info.lastLine) {
  538. output.println(line);
  539. }
  540. }
  541. } catch (IOException e) {
  542. System.err.println("writing line " + lineNum + " of " + file);
  543. e.printStackTrace(System.err);
  544. result = false;
  545. } finally {
  546. if (null != input) {
  547. try {
  548. input.close();
  549. } catch (IOException e) {
  550. result = false;
  551. }
  552. }
  553. if (null != outWriter) {
  554. try {
  555. outWriter.close();
  556. } catch (IOException e) {
  557. result = false;
  558. }
  559. }
  560. result = inFile.delete();
  561. }
  562. return result;
  563. }
  564. public static HeaderInfo checkFile(final File file) {
  565. ArrayList<String> years = new ArrayList<String>();
  566. int endLine = 0;
  567. BufferedReader input = null;
  568. int lineNum = 0;
  569. try {
  570. input = new BufferedReader(new FileReader(file));
  571. String line;
  572. while (null != (line = input.readLine())) {
  573. lineNum++;
  574. String ll = line.trim();
  575. if (ll.startsWith("package ") || ll.startsWith("import ")) {
  576. break; // ignore default package w/o imports
  577. }
  578. if (checkLine(line, years)) {
  579. endLine = lineNum;
  580. break;
  581. }
  582. }
  583. } catch (IOException e) {
  584. System.err.println("reading line " + lineNum + " of " + file);
  585. e.printStackTrace(System.err);
  586. } finally {
  587. if (null != input) {
  588. try {
  589. input.close();
  590. } catch (IOException e) {
  591. } // ignore
  592. }
  593. }
  594. return new HeaderInfo(file, endLine, years, endLine > 0);
  595. }
  596. /**
  597. * Add any years found (as String) to years, and return true at the first end-of-comment
  598. *
  599. * @return true if this line has end-of-comment
  600. */
  601. private static boolean checkLine(String line, ArrayList<String> years) {
  602. if ((null == line) || (0 == line.length())) {
  603. return false;
  604. }
  605. int loc;
  606. int start = 0;
  607. while ((-1 != (loc = line.indexOf("199", start)) || (-1 != (loc = line.indexOf("200", start))))) {
  608. char c = line.charAt(loc + 3);
  609. if ((c <= '9') && (c >= '0')) {
  610. years.add(line.substring(loc, loc + 4));
  611. }
  612. start = loc + 4;
  613. }
  614. return (line.contains("*/"));
  615. }
  616. } // class Header