001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.tools.code;
007:
008: import java.io.File;
009: import java.io.IOException;
010: import java.io.RandomAccessFile;
011:
012: /**
013: * This tool checks that for each .java file there is a package.html file,
014: * that for each .java file there is at least one (class level) javadoc comment,
015: * and that lines with comments are not too long.
016: */
017: public class CheckJavadoc {
018:
019: private int errorCount;
020: private static final int MAX_COMMENT_LINE_SIZE = 80;
021:
022: public static void main(String[] args) throws Exception {
023: new CheckJavadoc().run();
024: }
025:
026: void run() throws Exception {
027: String baseDir = "src";
028: check(new File(baseDir));
029: if (errorCount > 0) {
030: throw new Exception(errorCount + " errors found");
031: }
032: }
033:
034: private int check(File file) throws Exception {
035: String name = file.getName();
036: if (file.isDirectory()) {
037: if (name.equals("CVS") || name.equals(".svn")) {
038: return 0;
039: }
040: File[] list = file.listFiles();
041: boolean foundPackageHtml = false, foundJava = false;
042: for (int i = 0; i < list.length; i++) {
043: int type = check(list[i]);
044: if (type == 1) {
045: foundJava = true;
046: } else if (type == 2) {
047: foundPackageHtml = true;
048: }
049: }
050: if (foundJava && !foundPackageHtml) {
051: System.out
052: .println("No package.html file, but a Java file found at: "
053: + file.getAbsolutePath());
054: errorCount++;
055: }
056: } else {
057: if (name.endsWith(".java")) {
058: checkJavadoc(file);
059: return 1;
060: } else if (name.equals("package.html")) {
061: return 2;
062: }
063: }
064: return 0;
065: }
066:
067: private void checkJavadoc(File file) throws IOException {
068: RandomAccessFile in = new RandomAccessFile(file, "r");
069: byte[] data = new byte[(int) file.length()];
070: in.readFully(data);
071: in.close();
072: String text = new String(data);
073: int comment = text.indexOf("/**");
074: if (comment < 0) {
075: System.out.println("No Javadoc comment: "
076: + file.getAbsolutePath());
077: errorCount++;
078: }
079: int open = text.indexOf('{');
080: if (open < 0 || open < comment) {
081: System.out
082: .println("No '{' or '{' before the first Javadoc comment: "
083: + file.getAbsolutePath());
084: errorCount++;
085: }
086: int pos = 0;
087: int lineNumber = 1;
088: boolean inComment = false;
089: while (true) {
090: int next = text.indexOf("\n", pos);
091: if (next < 0) {
092: break;
093: }
094: String line = text.substring(pos, next).trim();
095: if (line.startsWith("/*")) {
096: inComment = true;
097: }
098: if (inComment) {
099: if (line.length() > MAX_COMMENT_LINE_SIZE) {
100: System.out.println("Long line : "
101: + file.getAbsolutePath() + " ("
102: + file.getName() + ":" + lineNumber + ")");
103: errorCount++;
104: }
105: if (line.endsWith("*/")) {
106: inComment = false;
107: }
108: }
109: if (!inComment && line.startsWith("//")
110: && line.length() > MAX_COMMENT_LINE_SIZE) {
111: System.out.println("Long line: "
112: + file.getAbsolutePath() + " ("
113: + file.getName() + ":" + lineNumber + ")");
114: errorCount++;
115: }
116: lineNumber++;
117: pos = next + 1;
118: }
119: }
120:
121: }
|