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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia Site Renderer 1.4 at 18 Feb 2015 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CodeNarc - CodeNarc - Creating a RuleSet</title>
<style type="text/css" media="all">
@import url("./css/maven-base.css");
@import url("./css/maven-theme.css");
@import url("./css/site.css");
</style>
<link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
<meta name="Date-Revision-yyyymmdd" content="20150218" />
<meta http-equiv="Content-Language" content="en" />
</head>
<body class="composite">
<div id="banner">
<a href="./" id="bannerLeft">
<img src="images/codenarc-logo.png" alt="CodeNarc" />
</a>
<a href="http://github.com/CodeNarc" id="bannerRight">
<img src="images/forkme_right_red_aa0000.png" alt="Fork me on GitHub" />
</a>
<div class="clear">
<hr/>
</div>
</div>
<div id="breadcrumbs">
<div class="xleft">
<span id="publishDate">Last Published: 18 Feb 2015</span>
| <span id="projectVersion">Version: 0.23</span>
</div>
<div class="xright">
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="leftColumn">
<div id="navcolumn">
<h5>General</h5>
<ul>
<li class="none">
<a href="index.html" title="Home">Home</a>
</li>
<li class="none">
<a href="https://sourceforge.net/project/showfiles.php?group_id=250145" class="externalLink" title="Downloads">Downloads</a>
</li>
<li class="none">
<a href="apidocs/index.html" title="Javadocs">Javadocs</a>
</li>
<li class="none">
<a href="http://sourceforge.net/mail/?group_id=250145" class="externalLink" title="Mailing Lists">Mailing Lists</a>
</li>
<li class="none">
<a href="http://sourceforge.net/tracker/?group_id=250145" class="externalLink" title="Bug Tracker">Bug Tracker</a>
</li>
<li class="none">
<a href="http://sourceforge.net/projects/codenarc" class="externalLink" title="SourceForge Project">SourceForge Project</a>
</li>
<li class="none">
<a href="http://github.com/CodeNarc" class="externalLink" title="GitHub Project">GitHub Project</a>
</li>
</ul>
<h5>Running</h5>
<ul>
<li class="none">
<a href="codenarc-ant-task.html" title="Ant Task Usage">Ant Task Usage</a>
</li>
<li class="none">
<a href="codenarc-command-line.html" title="Command-Line">Command-Line</a>
</li>
<li class="none">
<a href="codenarc-run-as-a-test.html" title="Run as a Test">Run as a Test</a>
</li>
<li class="none">
<a href="codenarc-other-tools-frameworks.html" title="Other Tools/Frameworks">Other Tools/Frameworks</a>
</li>
</ul>
<h5>Using</h5>
<ul>
<li class="none">
<strong>Creating a RuleSet</strong>
</li>
<li class="none">
<a href="codenarc-creating-rule.html" title="Creating a Rule">Creating a Rule</a>
</li>
<li class="none">
<a href="codenarc-configuring-rules.html" title="Configuring Rules">Configuring Rules</a>
</li>
<li class="none">
<a href="StarterRuleSet-AllRulesByCategory.groovy.txt" title="Starter RuleSet (All)">Starter RuleSet (All)</a>
</li>
</ul>
<h5>Report Types</h5>
<ul>
<li class="none">
<a href="codenarc-HtmlReportWriter.html" title="HTML Report">HTML Report</a>
</li>
<li class="none">
<a href="codenarc-XmlReportWriter.html" title="XML Report">XML Report</a>
</li>
<li class="none">
<a href="codenarc-TextReportWriter.html" title="Text and IDE Reports">Text and IDE Reports</a>
</li>
</ul>
<h5>Sample Reports</h5>
<ul>
<li class="none">
<a href="SampleCodeNarcHtmlReport.html" title="Sample HTML Report">Sample HTML Report</a>
</li>
<li class="none">
<a href="SampleCodeNarcXmlReport.xml" title="Sample XML Report">Sample XML Report</a>
</li>
</ul>
<h5>Rules</h5>
<ul>
<li class="none">
<a href="codenarc-rule-index.html" title="Rule Index">Rule Index</a>
</li>
<li class="none">
<a href="codenarc-rules-basic.html" title="Basic Rules">Basic Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-braces.html" title="Braces Rules">Braces Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-concurrency.html" title="Concurrency Rules">Concurrency Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-convention.html" title="Convention Rules">Convention Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-design.html" title="Design Rules">Design Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-dry.html" title="DRY Rules">DRY Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-enhanced.html" title="Enhanced Rules">Enhanced Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-exceptions.html" title="Exceptions Rules">Exceptions Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-formatting.html" title="Formatting Rules">Formatting Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-generic.html" title="Generic Rules">Generic Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-grails.html" title="Grails Rules">Grails Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-groovyism.html" title="Groovyism Rules">Groovyism Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-imports.html" title="Imports Rules">Imports Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-jdbc.html" title="JDBC Rules">JDBC Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-junit.html" title="JUnit Rules">JUnit Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-logging.html" title="Logging Rules">Logging Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-naming.html" title="Naming Rules">Naming Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-size.html" title="Size/Complexity Rules">Size/Complexity Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-security.html" title="Security Rules">Security Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-serialization.html" title="Serialization Rules">Serialization Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-unnecessary.html" title="Unnecessary Rules">Unnecessary Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-unused.html" title="Unused Rules">Unused Rules</a>
</li>
</ul>
<h5>Developing</h5>
<ul>
<li class="none">
<a href="codenarc-developer-guide.html" title="Developer Guide">Developer Guide</a>
</li>
</ul>
<h5>Project Documentation</h5>
<ul>
<li class="collapsed">
<a href="project-info.html" title="Project Information">Project Information</a>
</li>
<li class="collapsed">
<a href="project-reports.html" title="Project Reports">Project Reports</a>
</li>
</ul>
<a href="http://sourceforge.net" title="Hosted on SourceForge.net" class="poweredBy">
<img class="poweredBy" alt="Hosted on SourceForge.net" src="http://sflogo.sourceforge.net/sflogo.php?group_id=208647&type=2" />
</a>
<a href="http://maven.apache.org" title="Build with Maven 2" class="poweredBy">
<img class="poweredBy" alt="Build with Maven 2" src="images/logos/maven-feather.png" />
</a>
</div>
</div>
<div id="bodyColumn">
<div id="contentBox">
<div class="section">
<h2>CodeNarc - Creating a RuleSet<a name="CodeNarc_-_Creating_a_RuleSet"></a></h2><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><b>Contents</b></p>
<ul>
<li><a href="#New_and_Improved_Way_To_Configure_a_RuleSet">New and Improved Way To Configure a RuleSet</a></li>
<li><a href="#Creating_a_Groovy_RuleSet_File">Creating a Groovy RuleSet File</a></li>
<li><a href="#Creating_an_XML_RuleSet_File">Creating an XML RuleSet File</a></li></ul>
<div class="section">
<h3>New and Improved Way To Configure a RuleSet<a name="New_and_Improved_Way_To_Configure_a_RuleSet"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><b>NEW!</b> The preferred way to configure the rules that <b>CodeNarc</b> will use is to create a custom <i>RuleSet</i> specifying the rule names (i.e., without depending on the <i>RuleSet</i> files provided with <b>CodeNarc</b>. This allows finer control over your custom RuleSet and insulates you from the provided <i>RuleSets</i> that can (and often do) change from release to release.</p>
<div class="section">
<h4>Just Copy and Tweak One of the Starter RuleSet Files<a name="Just_Copy_and_Tweak_One_of_the_Starter_RuleSet_Files"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>See the <a href="./StarterRuleSet-AllRulesByCategory.groovy.txt">Starter RuleSet - All Rules By Category</a>. It contains all of the rules provided with the current version of <b>CodeNarc</b>, organized by category. Just delete or comment out the rules you don't want to use.</p>
<p>Alternatively, there is a <a href="./StarterRuleSet-AllRules.groovy.txt">Starter RuleSet - All Rules</a>. It contains all of the rules provided with the current version of <b>CodeNarc</b>, in alphabetical order. Just delete or comment out the rules you don't want to use.</p></div>
<div class="section">
<h4>The Other RuleSet Options Still Work<a name="The_Other_RuleSet_Options_Still_Work"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You can still create your own custom <a href="#A_Sample_Groovy_RuleSet_Using_the_Old_Syntax">Groovy</a> <i>RuleSet</i> using the older syntax. You can even mix the new rule-name-only syntax with the older Groovy DSL syntax. It's all good!</p>
<p>You can also continue to use the predefined <i>RuleSets</i> distributed with <b>CodeNarc</b> or you can create your own <a href="#Creating_an_XML_RuleSet_File">XML</a> <i>RuleSet</i>. See the site navigation menu for a list of the <i>RuleSets</i> provided out of the box by <b>CodeNarc</b>.</p></div></div>
<div class="section">
<h3><a name="Creating_a_Groovy_RuleSet_File">Creating a Groovy RuleSet File</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><b>CodeNarc</b> provides a Groovy DSL (domain-specific language) for defining <i>RuleSets</i>.</p>
<p>The preferred syntax for defining a <i>RuleSet</i> is to specify the list of rules using only the rule names. As mentioned above, this allows finer control over your custom RuleSet and insulates you from the provided <i>RuleSets</i> that can (and often do) change from release to release.</p>
<div class="section">
<h4>A Sample Groovy RuleSet<a name="A_Sample_Groovy_RuleSet"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Here is an example of a Groovy <i>RuleSet</i> file using the preferred syntax:</p>
<div class="source">
<pre>ruleset {
description 'A custom Groovy RuleSet'
CyclomaticComplexity {
maxMethodComplexity = 1
}
ClassName
MethodName
ConfusingTernary(priority:3)
StatelessClass {
name = 'StatelessDao'
applyToClassNames = '*Dao'
}
}
</pre></div>
<p>Things to note:</p>
<ul>
<li>The Groovy <i>RuleSet</i> file itself must be accessible on the classpath.</li>
<li>Each rule is specified by its rule <i>name</i>.</li>
<li>The <b>StatelessClass</b> rule redefines the <i>name</i> for that rule instance. You can have multiple instances of the same rule class as long as they have unique rule <i>names</i>.</li>
<li>You can optionally configure a rule instance by specifying rule properties in a <i>Map</i> after the rule <i>name</i> (see <b>ConfusingTernary</b> in the example). Or you can specify rule properties in a <i>Closure</i> after the rule <i>name</i> (see <b>CyclomaticComplexity</b> and <b>StatelessClass</b> in the example).</li>
<li>The easiest way to create a new custom <i>RuleSet</i> is to copy the <a href="./StarterRuleSet-AllRulesByCategory.groovy.txt">Starter RuleSet</a> (as a .groovy file). It contains all of the rules provided with the current version of <b>CodeNarc</b>, organized by category. Just delete or comment out the rules you don't want to use.</li></ul>
<p>And here is an example that mixes both the preferred (new) syntax along with the older syntax, to illustrate backward-compatibility:</p>
<div class="source">
<pre>ruleset {
MethodName
ConfusingTernary(priority:3)
StatelessClass {
name = 'StatelessDao'
applyToClassNames = '*Dao'
}
// Old style
rule(org.codenarc.rule.basic.ThrowExceptionFromFinallyBlockRule) {
priority = 3
}
// Old style
ruleset('rulesets/dry.xml')
}
</pre></div></div>
<div class="section">
<h4>A Sample Groovy RuleSet Using the Old Syntax<a name="A_Sample_Groovy_RuleSet_Using_the_Old_Syntax"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Here is an example of a Groovy <i>RuleSet</i> file using the older syntax:</p>
<div class="source">
<pre>import org.codenarc.rule.basic.ThrowExceptionFromFinallyBlockRule
ruleset {
description 'A sample Groovy RuleSet'
ruleset('rulesets/basic.xml') {
'CatchThrowable' {
priority = 1
enabled = false
}
'EqualsAndHashCode' priority:3
exclude 'Empty*'
}
rule(ThrowExceptionFromFinallyBlockRule) {
priority = 3
}
rule("rules/MyCustomRuleScript.groovy")
ruleset('MyGroovyRuleSet.groovy')
}
</pre></div>
<p>Things to note:</p>
<ul>
<li>The Groovy <i>RuleSet</i> file itself must be accessible on the classpath.</li>
<li>The "outer" <b>ruleset</b> defines the contents of the <i>RuleSet</i> (within a <i>closure</i>). It can include an optional <b>description</b> and any combination of <b>ruleset</b> (other <i>RuleSets</i>) and <b>rule</b> statements (individual <i>Rules</i>).</li></ul>
<p>About the "inner" <b>ruleset</b> statements:</p>
<ul>
<li>Each <b>ruleset</b> statement loads a <i>RuleSet</i> file. The path specifies either a Groovy file or an XML file. By default, the paths specified are relative to the classpath. But these paths may be optionally prefixed by any of the valid java.net.URL prefixes, such as "file:" (to load from a relative or absolute path on the filesystem), or "http:".</li>
<li>The 'rulesets/basic.xml' <i>RuleSet</i> file is interpreted as an XML file based on the '.xml' extension. Likewise, the 'MyGroovyRuleSet.groovy' file is interpreted as a Groovy <i>RuleSet</i> file.</li>
<li>The <i>RuleSet</i> can be customized by following the <i>RuleSet</i> path with an (optional) <i>closure</i>, containing any combination of the following:
<ol style="list-style-type: decimal">
<li><b>Rule Configurations</b> -- A <b>ruleset</b> statement can optionally provide configuration of the properties for individual rules within the <i>RuleSet</i>. Specify the name of the rule (as a <i>String</i>), followed by its configuration. (Remember to specify the rule <i>name</i>, not the class name. In most cases, the rule <i>name</i> is the class name without the "Rule" suffix). The name of the <i>Rule</i> can be followed by:
<ol style="list-style-type: lower-alpha">
<li>A <i>Map</i> of property names and values. See 'EqualsAndHashCode' within the example.</li>
<li>A <i>closure</i> containing property assignments statements. See 'CatchThrowable' within the example.</li></ol>
<p>Properties set this way can be of type <i>String</i>, <i>int</i>, <i>long</i> or <i>boolean</i>.</p></li>
<li><b>Rule Filtering (include or exclude statements)</b> -- A <b>ruleset</b> statement can optionally specify <b>include</b> and/or <b>exclude</b> pattern(s) of rule names to include or exclude from the <i>RuleSet</i>. See <a href="#Filtering_Rules_Within_a_RuleSet">Filtering Rules Within a RuleSet</a>.</li></ol></li></ul>
<p>About the <b>rule</b> statements:</p>
<ul>
<li>Each <b>rule</b> statements loads a single <i>Rule</i>.</li>
<li>The <b>rule</b> statement must specify either:
<ol style="list-style-type: lower-alpha">
<li>The class name for a <i>Rule</i>. See <tt>ThrowExceptionFromFinallyBlockRule</tt> within the example. The <i>Rule</i> class must be available on the classpath.</li>
<li>The path to a <i>Rule Script</i> file. See "MyGroovyRuleSet.groovy" within the example. By default, the paths specified are relative to the classpath. But these paths may be optionally prefixed by any of the valid java.net.URL prefixes, such as "file:" (to load from a relative or absolute path on the filesystem) or "http:".</li></ol></li>
<li>A <b>rule</b> can optionally provide configuration of the <i>Rule</i> properties by specifying a <i>closure</i> containing property assignment statements. See <tt>ThrowExceptionFromFinallyBlockRule</tt> within the example. As within <b>ruleset</b> statements, properties set this way can be of type <i>String</i>, <i>int</i>, <i>long</i> or <i>boolean</i>.</li></ul></div></div>
<div class="section">
<h3><a name="Creating_an_XML_RuleSet_File">Creating an XML RuleSet File</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The XML schema for a <b>CodeNarc</b> <i>RuleSet</i> file is embodied in the "ruleset-schema.xsd" file which is included within the <b>CodeNarc</b> jar. It contains three sections, all of which are optional, though the sections must be in the order listed:</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>XML Tag</b></td>
<td align="left"><b>Purpose</b></td>
<td align="left"><b>How many are allowed</b></td></tr>
<tr class="b">
<td align="left"><tt><description></tt></td>
<td align="left">Describe the purpose of the <i>RuleSet</i></td>
<td align="left">Zero or one</td></tr>
<tr class="a">
<td align="left"><tt><ruleset-ref></tt></td>
<td align="left">Include a nested <i>RuleSet</i>, optionally configuring and/or filtering the rules within it. The path to the <i>RuleSet</i> can specify either an XML file or a Groovy <i>RuleSet</i> file.</td>
<td align="left">Zero or more</td></tr>
<tr class="b">
<td align="left"><tt><rule></tt></td>
<td align="left">Include a single rule; specify its fully-qualified classname</td>
<td align="left">Zero or more</td></tr>
<tr class="a">
<td align="left"><tt><rule-script></tt></td>
<td align="left">Include a single rule implemented by a groovy script; specify the path of the script. The path is relative to the classpath by default, but can optionally specify a URL prefix.</td>
<td align="left">Zero or more</td></tr></table>
<div class="section">
<h4>A Sample XML RuleSet<a name="A_Sample_XML_RuleSet"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Here is an example XML <i>RuleSet</i> file:</p>
<div class="source">
<pre><ruleset xmlns="http://codenarc.org/ruleset/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://codenarc.org/ruleset/1.0 http://codenarc.org/ruleset-schema.xsd"
xsi:noNamespaceSchemaLocation="http://codenarc.org/ruleset-schema.xsd">
<description>Sample rule set</description>
<ruleset-ref path='rulesets/imports.xml'>
<rule-config name='DuplicateImport'>
<property name='priority' value='1'/>
</rule-config>
</ruleset-ref>
<ruleset-ref path='rulesets/basic.xml'>
<exclude name='StringInstantiation'/>
</ruleset-ref>
<rule class='org.codenarc.rule.generic.IllegalRegexRule'>
<property name="name" value="AuthorTagNotAllowed"/>
<property name='regex' value='\@author'/>
</rule>
<rule-script path='rules/MyStaticFieldRule.groovy'/>
</ruleset>
</pre></div>
<p>Things to note:</p>
<ul>
<li>The <i>RuleSet</i> file itself must be accessible on the classpath.</li>
<li>The top-level <ruleset> element must include the namespace declaration shown.</li></ul>
<p>About the <ruleset-ref> elements:</p>
<ul>
<li>By default, The <i>path</i> of the <ruleset-ref> is relative to the classpath. But these paths may be optionally prefixed by any of the valid java.net.URL prefixes, such as "file:" (to load from a relative or absolute path on the filesystem) or "http:".</li>
<li>You can optionally set arbitrary properties for the rules within the <i>RuleSet</i> by including one or more <property> elements within a <rule-config> element. In the example above, the <b>DuplicateImportRule</b> <i>priority</i> property is set to 1. Properties set this way can be of type <i>String</i>, <i>int</i>, <i>long</i> or <i>boolean</i>.</li>
<li>Remember that the <i>name</i> for a <rule-config> specifies the rule <i>name</i>, not the class name. (In most cases, the rule <i>name</i> is the class name without the "Rule" suffix.)</li></ul>
<p>About the <rule> elements:</p>
<ul>
<li>You must specify the fully-qualified class name for each <i>Rule</i>.</li>
<li>The <i>Rule</i> class must be available on the classpath.</li>
<li>You can optionally set arbitrary properties for individual rules by including a <property> element within a <rule>. For the <b>IllegalRegexRule</b> rule above, the <i>name</i> property is set to "AuthorTagNotAllowed" and the <i>regex</i> property is set to "\@author". Properties set this way can be of type <i>String</i>, <i>int</i>, <i>long</i> or <i>boolean</i>.</li>
<li>Because the <i>name</i> property is customized for the <b>IllegalRegexRule</b>, the localized rule description will be retrieved from the custom messages resource bundle file ("codenarc-messages.properties"), if that bundle file exists, otherwise a default generic message will be used. The resource bundle message key will be based on the customized name. In this case, the message key will be "AuthorTagNotAllowed.description".</li></ul>
<p>About the <rule-script> elements:</p>
<ul>
<li>You must specify the <i>path</i> for the rule script Groovy file. By default, this <i>path</i> is relative to the classpath. But these paths may be optionally prefixed by any of the valid java.net.URL prefixes, such as "file:" (to load from a relative or absolute path on the filesystem) or "http:".</li>
<li>The class defined within the rule script Groovy file must implement the <tt>org.codenarc.rule.Rule</tt> interface.</li>
<li>You can optionally set arbitrary properties for the rule by including a <property> element within a <rule-script> the same way you can for a <rule>. That is not shown here.</li></ul></div></div>
<div class="section">
<h3><a name="Filtering_Rules_Within_a_RuleSet">Filtering Rules Within a RuleSet</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You can use the <include> and <exclude> elements within a <ruleset-ref> to filter a <i>RuleSet</i> and include and/or the exclude individual rules. In the following <i>RuleSet</i> excerpt, the entire "rulesets/basic.xml" <i>RuleSet</i> is included, except for the <b>BooleanInstantiationRule</b> and <b>StringInstantiationRule</b>.</p>
<div class="source">
<pre> ruleset('rulesets/basic.xml') {
exclude 'BooleanInstantiation'
exclude 'StringInstantiation'
}
</pre></div>
<p>And here is the same example in XML <i>RuleSet</i> format:</p>
<div class="source">
<pre> <ruleset-ref path='rulesets/basic.xml'>
<exclude name='BooleanInstantiation'/>
<exclude name='StringInstantiation'/>
</ruleset-ref>
</pre></div>
<p>Alternatively, you may wish to explicitly specify the rules that you want included from a <i>RuleSet</i> rather than those that are excluded. In the following <i>RuleSet</i> excerpt, ONLY the <b>ReturnFromFinallyBlockRule</b> and <b>StringInstantiationRule</b> rules are included from the "rulesets/basic.xml" <i>RuleSet</i>.</p>
<div class="source">
<pre> ruleset('rulesets/basic.xml') {
include 'ReturnFromFinallyBlockRule'
include 'StringInstantiation'
}
</pre></div>
<p>And here is the same example in XML <i>RuleSet</i> format:</p>
<div class="source">
<pre> <ruleset-ref path='rulesets/basic.xml'>
<include name='ReturnFromFinallyBlockRule'/>
<include name='StringInstantiation'/>
</ruleset-ref>
</pre></div>
<p><b>Note</b>: In all cases, the rule <i>name</i> is specified, not the class name. (In most cases, the rule <i>name</i> is the class name without the "Rule" suffix.)</p>
<p><b>Note</b>: If you specify at least one <include>, then ONLY rules matching an <include> will be included.</p>
<p><b>Note</b>: If you specify an <include> and an <exclude> for a rule, then the <exclude> takes precedence.</p>
<div class="section">
<h4>Using Wildcards Within <include> and <exclude> Names<a name="Using_Wildcards_Within_include_and_exclude_Names"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You may optionally include wildcard characters ('*' or '?') in the rule <i>name</i> for both <include> and an <exclude> elements. The wildcard character '*' within the <i>name</i> matches a sequence of zero or more characters in the rule name, while the wildcard character '?' within the <i>name</i> matches exactly one character in the rule name.</p>
<p>In the following <i>RuleSet</i> excerpt, all rules matching '*Instantiation' are included from the "rulesets/basic.xml" <i>RuleSet</i>. In this case, that will include the <b>StringInstantiationRule</b> and <b>BooleanInstantiationRule</b> rules.</p>
<div class="source">
<pre> <ruleset-ref path='rulesets/basic.xml'>
<include name='*Instantiation'/>
</ruleset-ref>
</pre></div></div></div></div>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">
Copyright © 2015.
All Rights Reserved.
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>
|