From a9bfdceec2a2d86a622c03fbd4052f007b5e70cc Mon Sep 17 00:00:00 2001 From: Rick Giles Date: Tue, 15 Jul 2003 11:10:15 +0000 Subject: [PATCH] completed Filter docs. --- .../com/mycompany/filters/FilesFilter.java | 44 +++++ docs/Filter.gif | Bin 0 -> 4254 bytes docs/index.html | 1 + docs/releasenotes.html | 3 + docs/writingfilters.html | 187 ++++++++++++++++++ 5 files changed, 235 insertions(+) create mode 100644 contrib/examples/filters/com/mycompany/filters/FilesFilter.java create mode 100644 docs/Filter.gif create mode 100644 docs/writingfilters.html diff --git a/contrib/examples/filters/com/mycompany/filters/FilesFilter.java b/contrib/examples/filters/com/mycompany/filters/FilesFilter.java new file mode 100644 index 000000000..584dd3779 --- /dev/null +++ b/contrib/examples/filters/com/mycompany/filters/FilesFilter.java @@ -0,0 +1,44 @@ +package com.mycompany.filters; + +import org.apache.regexp.RE; +import org.apache.regexp.RESyntaxException; + +import com.puppycrawl.tools.checkstyle.api.AuditEvent; +import com.puppycrawl.tools.checkstyle.api.AutomaticBean; +import com.puppycrawl.tools.checkstyle.api.Filter; +import com.puppycrawl.tools.checkstyle.api.Utils; + +public class FilesFilter + extends AutomaticBean + implements Filter +{ + private RE mFileRegexp; + + public FilesFilter() + throws RESyntaxException + { + setFiles("^$"); + } + + public int decide(Object aObject) + { + if (!(aObject instanceof AuditEvent)) { + return Filter.NEUTRAL; + } + + final AuditEvent event = (AuditEvent) aObject; + final String fileName = event.getFileName(); + if ((fileName != null) && mFileRegexp.match(fileName)) { + return Filter.DENY; + } + else { + return Filter.NEUTRAL; + } + } + + public void setFiles(String aFilesPattern) + throws RESyntaxException + { + mFileRegexp = Utils.getRE(aFilesPattern); + } +} diff --git a/docs/Filter.gif b/docs/Filter.gif new file mode 100644 index 0000000000000000000000000000000000000000..b123fcc3ca54c69d3fda7be4a045b0eba0eb9877 GIT binary patch literal 4254 zcmeH`=QkUS0>*i@YILcpQPgY3s1DTbQV~(JDv45i*9u8QDn^7LW{{+)Es>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releasenotes.html b/docs/releasenotes.html index 684788209..f40efa642 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -121,6 +121,9 @@
  • Added usage checks OneMethodPrivateFieldCheck, UnusedLocalVariableCheck, UnusedParameterCheck, UnusedPrivateFieldCheck, UnusedPrivateMethodCheck.
  • +
  • Added filters for audit events (partially fulfills request 559103). + A SuppressionFilter denies events according to a suppressions file (request 756416).
  • +

    diff --git a/docs/writingfilters.html b/docs/writingfilters.html new file mode 100644 index 000000000..0c583e77d --- /dev/null +++ b/docs/writingfilters.html @@ -0,0 +1,187 @@ + + + + + Writing your own filters + + + + + + + + + + +

    Writing your own Filters

    Checkstyle Logo
    + + + + + + + + +

    Overview

    +

    + A Checker has a chain of Filters + that decide which audit events the Checker reports through its listeners. + Interface Filter and class FilterChain + are intended to support general Object filtering in a filter chain. +

    +

    + A Filter has one method, decide(Object), + that determines the result of filtering an Object. + Method decide(Object) should return one of three constants, + Filter.ACCEPT, Filter.DENY, + or Filter.NEUTRAL which may be interpreted as the Filter + accepts, denies, or is neutral towards the Object parameter. +

    +

    + A FilterChain is a particular Filter + that contains a chain of Filters. + FilterChain method decide(Object) + applies the Filters in chain order. + If a Filter in the chain + accepts the Object, then the FilterChain + accepts the Object without consulting the remaining Filters. + If a Filter + denies the Object, then the FilterChain + denies the Object without consulting the remaining Filters. + If all Filters are neutral towards the Object, + then so too is the FilterChain. When you require filtering based on a sequence + of simpler decisions, you should consider incorporating Filters + in a FilterChain. + CSVFilter, a filter for comma-separated values, + is an example of such filtering. +

    +

    + Here is a UML diagram for interface Filter + and class FilterChain. +

    + Filter UML diagram + +

    Writing Filters

    + +

    + The Filter that we demonstrate here denies audit events for files whose name matches a + regular expression. + In order to enable the specification of the file name pattern as a property in a configuration file, + the Filter is an + AutomaticBean + with mutator method setFiles(String) that receives the file name pattern. + An AutomaticBean uses JavaBean introspection to set JavaBean properties such as + files. +

    + +
    +package com.mycompany.filters;
    +
    +import org.apache.regexp.RE;
    +import org.apache.regexp.RESyntaxException;
    +
    +import com.puppycrawl.tools.checkstyle.api.AuditEvent;
    +import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
    +import com.puppycrawl.tools.checkstyle.api.Filter;
    +import com.puppycrawl.tools.checkstyle.api.Utils;
    +
    +public class FilesFilter
    +    extends AutomaticBean
    +    implements Filter
    +{
    +    private RE mFileRegexp;
    +
    +    public FilesFilter()
    +        throws RESyntaxException
    +    {
    +        setFiles("^$");
    +    }
    +    
    +    public int decide(Object aObject)
    +    {
    +        if (!(aObject instanceof AuditEvent)) {
    +            return Filter.NEUTRAL;
    +        }
    +        
    +        final AuditEvent event = (AuditEvent) aObject;
    +        final String fileName = event.getFileName();
    +        if ((fileName != null) && mFileRegexp.match(fileName)) {
    +            return Filter.DENY;        
    +        }
    +        else {
    +            return Filter.NEUTRAL;
    +        }
    +    }
    +
    +    public void setFiles(String aFilesPattern)
    +        throws RESyntaxException
    +    {
    +        mFileRegexp = Utils.getRE(aFilesPattern);
    +    }
    +}    
    +

    Using Filters

    + +

    + To incorporate a Filter in the filter chain for a Checker, + include a module element for the Filter in the + configuration file. For example, to configure a Checker + so that it uses custom filter FilesFilter to deny reporting of + audit events for files whose name contains "Generated", include the following module in the + configuration file: +

    + +
         
    +    <module name="com.mycompany.filters.FilesFilter">
    +        <property name="files" value="Generated"/>
    +    </module>
    +      
    + +

    Huh? I can't figure it out!

    +

    + That's probably our fault, and it means that we have to provide better + documentation. Please do not hesitate to ask questions on the user mailing list, + this will help us to improve this document. Please ask your questions as + precisely as possible. We will not be able to answer questions like "I want + to write a listener but I don't know how, can you help me?". Tell us + what you are trying to do (the purpose of the filter), what you have + understood so far, and what exactly you are getting stuck on. +

    + +

    Contributing

    +

    + We need your help to keep improving Checkstyle. + + Whenever you write a filter that you think is generally useful, please + consider contributing it to the Checkstyle + community and submit it for inclusion in the next release of Checkstyle. + +

    + +
    +


    + + + +