Menu

XPath rule questions

2007-05-27
2012-10-07
  • Randal Cobb

    Randal Cobb - 2007-05-27

    Hello all,

    I am just learning XPath after writing a few PMD rules in Java and I had a few "confusions"...

    Is XPath only used to test the positive presence of a specific situation? As in, if the situation is true; or, if the XPath rule FINDS the value that is being queried for?

    Can XPath rules combine results to form specialized situations? I.e., can XPath rules be written just to test the existence of certain conditions only if the class being tested is say, an EJB3 session bean (or contains an '@Stateless' annotation)?

    Basically, I have been tasked with writing about 20-30 new rules that ensure my team is using certain company imposed logic and/or certain EJB3 specific coding standards are met and I am not sure which method of writing rules would be quickest and most efficient (Java or XPath) to get this done.

    Thanks in advance!

     
    • Randal Cobb

      Randal Cobb - 2007-06-15

      Perfect!

      Thanks so much for the help. Looks like I really need to dig into XPath.

      Awesome book, btw!

       
      • Tom Copeland

        Tom Copeland - 2007-06-15

        Super, and thanks for picking up the book, too!

        Yours,

        tom

         
    • Tom Copeland

      Tom Copeland - 2007-05-28

      Hi Randal -

      > Is XPath only used to test the
      > positive presence of a specific situation?

      Hm, sort of - it returns node sets that match the given criteria. So it can be used in a "negative" sense in that if you pass in a query and no nodes match, you'll get nothing in the node set and thus no problem reports.

      > can XPath rules be written just to test the existence of
      > certain conditions only if the class being tested is say,
      > an EJB3 session bean (or contains an '@Stateless' annotation)?

      Definitely. For example, look at the conditions in EmptyStaticInitializer:

      //Initializer[@Static='true']/Block[count(*)=0]

      So the Initializer must have a static modifier and also no Block descendants.

      > I am not sure which method of writing rules would be
      > quickest and most efficient (Java or XPath) to get this done.

      I usually try to write the rule in XPath first and only fall back on Java if it's not fast enough - or if the XPath starts getting really hairy. If you're interested in a decent survey of the XPath rules in PMD, check out appendix A of "PMD Applied" (http://pmdapplied.com/); there's a lot of good info in there about various ins and outs of writing XPath rules.

      Yours,

      Tom

       
    • Randal Cobb

      Randal Cobb - 2007-05-31

      Thanks, Tom, that helps alot.

      Now, can you do something like this?

      //NormalAnnotation[Name/@Image = 'Stateless']
      and //SingleMemberAnnotation[Name/@Image = 'TransactionAttribute']
      and //MethodDeclarator[starts-with(@Image, 'get')]

      I can run each of these queries individually and each finds an appropriate node (give the code below) but returning "No matching nodes <some funky number>" in the designer if I try to run them as I have them above.

      @Stateless(name = "MyService")
      @Local(MyServiceLocal.class)
      @Remote(MyServiceRemote.class)
      public class MyServiceBean implements MyServiceLocal, MyServiceRemote {

      @TransactionAttribute(TransactionAttributeType.SUPPORTS)
      public List&lt;Salary&gt; getSomething(String arg1, String arg2){
      List&lt;MyObject&gt; results = null;
      results = getDao().getMyObjectList(arg1, arg2);
          return results;
      }
      

      }

      I am pretty confident that I can make them "negative" so the result will be true once I figure out how to test for all of the nodes in the same query.

      Thanks again!

       
      • Tom Copeland

        Tom Copeland - 2007-06-15

        Hi Randal -

        Hm, so you want to only return classes which meet all 3 criteria? I think this should do the trick:

        //TypeDeclaration
        [//NormalAnnotation[Name/@Image = 'Stateless'] ]
        [//SingleMemberAnnotation[Name/@Image = 'TransactionAttribute']]
        [//MethodDeclarator[starts-with(@Image, 'get')]]

        See how the three conditions are ANDed together by including them as children of an enclosing node? XPath is pretty sweet...

        Yours,

        Tom
        http://generatingparserswithjavacc.com/

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.