XPath has a bunch of handy functions that you can use for writing PMD rules. For example, the AbstractNaming rule finds abstract classes that aren't named something like "AbstractFoo" using the starts-with function:
But for real power, you need regular expressions - and thanks to Jaxen's extension functions and Jakarta ORO, PMD supports that now! Here's an example; this would find classes named 'Foo' or 'Bar':
Note the use of the new
regexp function - that's where the magic happens. For a more complicated example, here's a rule that checks for hardcoded IP addresses:
Props to both the Jaxen and the ORO guys for their nice APIs; adding this regular expression support took only about 40 lines of code. Very nice!
Updated 1/5/06: function is called 'matches' now per the XPath 2.0 spec. Thanks to Daniel Sheppard for the pointer!