This section provides three sets of sample code to demonstrate some scenarios with the query tool. The codes may not be executable or return a correct result. They are just examples to show you how to use the Query tool and how it works.

(i) Example 1

Print any classes whose containing in package com.nomagic.reportwizard.

Without QueryTool:

#foreach ($c in $Class)
  #if ($c.qualifiedName.startsWith("com.nomagic.reportwizard"))
    $c.name
  #end
#end

With QueryTool:

#import("query", "com.nomagic.reportwizard.tools.QueryTool")
#foreach ($c in $query.get("class[qualifiedName^=com.nomagic.reportwizard]"))
  $c.name
#end

(ii) Example 2

Print any classes whose containing in model Design and name ends with Bean.

Without QueryTool:

#set ($match = false)
#macro (recursiveCheck $e)
  #if ($e.elementType == "model" && e.name == "Design")
    #set ($match = true)
  #else
    #recursiveCheck($e.owner)
  #end
#end
 
#foreach ($c in $Class)
  #set ($match = false)
  #recursiveCheck($c)
  #if ($check)
    #if ($c.name.endsWith("Bean"))
      $c.name
    #end
  #end
#end

With QueryTool:

#import("query", "com.nomagic.reportwizard.tools.QueryTool")
 
#foreach ($c in $query.get("model[name=Design] class[name$=Bean]"))
  $c.name
#end

(iii) Example 3

Matches any property element which owner element is class whose name attributes value is exactly equal to foo.

Without QueryTool:

#foreach ($p in $Property)
  #set ($owner = $p.owner)
  #if ($owner.elementType == "class" && $owner.name == "foo")
      $p.name
  #end
#end

With QueryTool:

#import("query", "com.nomagic.reportwizard.tools.QueryTool")
 
#foreach ($p in $query.get("class[name=foo] > property"))
  $p.name
#end