01: package com.mycompany.checks;
02:
03: import java.io.File;
04: import com.puppycrawl.tools.checkstyle.api.*;
05:
06: /**
07: * An example for a user provided FileSetCheck,
08: * checks that the number of files does not excced a certain limit.
09: *
10: * This Class is provided for educational purposes only, we do not
11: * consider it useful to check your production code.
12: *
13: * @author lkuehne
14: */
15: public class LimitImplementationFiles extends AbstractFileSetCheck {
16: /**
17: * the maximium number of implementation files,
18: * default is 100.
19: */
20: private int max = 100;
21:
22: /**
23: * Give user a chance to configure max in the
24: * config file.
25: *
26: * @param aMax the user specified maximum.
27: */
28: public void setMax(int aMax) {
29: max = aMax;
30: }
31:
32: /**
33: * @see FileSetCheck
34: */
35: public void process(File[] files) {
36: if (files != null && files.length > max) {
37:
38: // figure out the file that contains the error
39: final String path = files[max].getPath();
40:
41: // message collector is used to collect error messages,
42: // needs to be reset before starting to collect error messages
43: // for a file.
44: getMessageCollector().reset();
45:
46: // message dispatcher is used to fire AuditEvents
47: MessageDispatcher dispatcher = getMessageDispatcher();
48:
49: // signal start of file to AuditListeners
50: dispatcher.fireFileStarted(path);
51:
52: // log the message
53: log(0, "max.files.exceeded", new Integer(max));
54:
55: // you can call log() multiple times to flag multiple
56: // errors in the same file
57:
58: // fire the errors for this file to the AuditListeners
59: fireErrors(path);
60:
61: // signal end of file to AuditListeners
62: dispatcher.fireFileFinished(path);
63: }
64: }
65: }
|