aboutsummaryrefslogtreecommitdiff
path: root/src/xdocs/writingfilters.xml
blob: 96d08710045c2b93d75957e86a298c1593fd901a (plain)
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
<?xml version="1.0" encoding="UTF-8"?>

<document xmlns="http://maven.apache.org/XDOC/2.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">

  <head>
    <title>Writing Filters</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"/>
    <script type="text/javascript" src="js/anchors.js"/>
    <script type="text/javascript" src="js/google-analytics.js"/>
    <link rel="icon" href="images/favicon.png" type="image/x-icon" />
    <link rel="shortcut icon" href="images/favicon.ico" type="image/ico" />
  </head>

  <body>
    <section name="Content">
      <macro name="toc">
        <param name="fromDepth" value="1"/>
        <param name="toDepth" value="1"/>
      </macro>
    </section>

    <section name="Overview">
      <p>
        A <code>Checker</code> has a set of <code>Filter</code>s that decide which audit events the <code>Checker</code> reports through its listeners.  Interface
        <code>Filter</code> and class <code>FilterSet</code> are intended to support general <code>AuditEvent</code> filtering using a set of <code>Filter</code>s.
      </p>

      <p>
        A <code>Filter</code> has <code>boolean</code> method <code>accept(AuditEvent)</code> that returns <code>true</code> if the <code>Filter</code>
        accepts the <code>AuditEvent</code> parameter and returns
        <code>false</code> if the <code>Filter</code> rejects it.
      </p>

      <p>
        A <code>FilterSet</code> is a particular <code>Filter</code> that contains a set of <code>Filter</code>s. A <code>FilterSet</code>
        accepts an <code>AuditEvent</code> if and only if all
        <code>Filter</code>s in the set accept the <code>AuditEvent</code>.
      </p>

      <p>
        Here is a UML diagram for interface <code>Filter</code>
        and class <code>FilterSet</code>.
      </p>

      <img src="images/Filter.gif" alt="Filter UML diagram" />
    </section>

    <section name="Writing Filters">
      <p>
        The <code>Filter</code> that we demonstrate here rejects
        audit events for files whose name matches a <a
        href="property_types.html#regexp">regular expression</a>.  In order to
        enable the specification of the file name pattern as a property in a
        configuration file, the <code>Filter</code> is an <a
        href="apidocs/com/puppycrawl/tools/checkstyle/api/AutomaticBean.html">AutomaticBean</a>
        with mutator method <code>setFiles(String)</code> that
        receives the file name pattern.  An <code>AutomaticBean</code> uses JavaBean introspection to set
        JavaBean properties such as <code>files</code>.
      </p>

      <source>
package com.mycompany.filters;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

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 Pattern mFileRegexp;

    public FilesFilter()
        throws PatternSyntaxException
    {
        setFiles("^$");
    }

    public boolean accept(AuditEvent aEvent)
    {
        final String fileName = aEvent.getFileName();
        return ((fileName == null) || !mFileRegexp.matcher(fileName).find());
    }

    public void setFiles(String aFilesPattern)
        throws PatternSyntaxException
    {
        mFileRegexp = Utils.getPattern(aFilesPattern);
    }
}
      </source>
    </section>

    <section name="Declare check's external resource locations">
      <p>
        See <a href="writingchecks.html#Declare_checks_external_resource_locations">Declare check's external resource locations</a>.
      </p>
    </section>

    <section name="Using Filters">
      <p>
        To incorporate a <code>Filter</code> in the filter set
        for a <code>Checker</code>, include a module element for
        the <code>Filter</code> in the <a
        href="config.html#Filters">configuration file</a>. For example, to
        configure a <code>Checker</code> so that it uses custom
        filter <code>FilesFilter</code> to prevent reporting of
        audit events for files whose name contains &quot;Generated&quot;,
        include the following module in the configuration file:
      </p>

      <source>
&lt;module name=&quot;com.mycompany.filters.FilesFilter&quot;&gt;
    &lt;property name=&quot;files&quot; value=&quot;Generated&quot;/&gt;
&lt;/module&gt;
      </source>
    </section>

    <section name="Huh? I can&#39;t figure it out!">
      <p>
        That&#39;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 &quot;I want to write a filter but I don&#39;t
        know how, can you help me?&quot;. 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.
      </p>
    </section>

    <section name="Contributing">
      <p>
        We need <em>your</em> help to keep improving Checkstyle. Whenever you
        write a filter that you think is generally useful, please consider <a
        href="contributing.html">contributing</a> it to the Checkstyle
        community and submit it for inclusion in the next release of
        Checkstyle.
      </p>
    </section>
  </body>
</document>