examples/xmlpatterns/xquery/globalVariables/reportGlobals.xq
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 (: 
       
     2     This XQuery loads a GCC-XML file and reports the locations of all 
       
     3     global variables in the original C++ source. To run the query,
       
     4     use the command line:
       
     5 
       
     6     xmlpatterns reportGlobals.xq -param fileToOpen=globals.gccxml -output globals.html
       
     7 
       
     8     "fileToOpen=globals.gccxml" binds the file name "globals.gccxml"
       
     9     to the variable "fileToOpen" declared and used below.
       
    10 :)
       
    11 
       
    12 declare variable $fileToOpen as xs:anyURI external;
       
    13 declare variable $inDoc as document-node() := doc($fileToOpen);
       
    14 
       
    15 (: 
       
    16    This function determines whether the typeId is a complex type,
       
    17    e.g. QString. We only check whether it's a class. To be strictly
       
    18    correct, we should check whether the class has a non-synthesized 
       
    19    constructor. We accept both mutable and const types.
       
    20 :)
       
    21 declare function local:isComplexType($typeID as xs:string) as xs:boolean
       
    22 {
       
    23     exists($inDoc/GCC_XML/Class[@id = $typeID])
       
    24     or
       
    25     exists($inDoc/GCC_XML/Class[@id = $inDoc/GCC_XML/CvQualifiedType[@id = $typeID]/@type])
       
    26 };
       
    27 
       
    28 (:
       
    29    This function determines whether the typeId is a primitive type.
       
    30 :)
       
    31 declare function local:isPrimitive($typeId as xs:string) as xs:boolean
       
    32 {
       
    33     exists($inDoc/GCC_XML/FundamentalType[@id = $typeId])
       
    34 };
       
    35 
       
    36 (: 
       
    37    This function constructs a line for the report. The line contains
       
    38    a variable name, the source file, and the line number.
       
    39 :)
       
    40 declare function local:location($block as element()) as xs:string
       
    41 {
       
    42     concat($inDoc/GCC_XML/File[@id = $block/@file]/@name, " at line ", $block/@line)
       
    43 };
       
    44 
       
    45 (: 
       
    46    This function generates the report. Note that it is called once
       
    47    in the <body> element of the <html> output.
       
    48 
       
    49    It ignores const variables of simple types but reports all others.
       
    50 :)
       
    51 declare function local:report() as element()+
       
    52 {
       
    53     let $complexVariables as element(Variable)* := $inDoc/GCC_XML/Variable[local:isComplexType(@type)]
       
    54     return if (exists($complexVariables))
       
    55            then (<p xmlns="http://www.w3.org/1999/xhtml/">Global variables with complex types:</p>,
       
    56                  <ol xmlns="http://www.w3.org/1999/xhtml/">
       
    57                     {
       
    58                         (: For each Variable in $complexVariables... :)
       
    59                         $complexVariables/<li><span class="variableName">{string(@name)}</span> in {local:location(.)}</li>
       
    60                     }
       
    61                  </ol>)
       
    62            else <p xmlns="http://www.w3.org/1999/xhtml/">No complex global variables found.</p>
       
    63 
       
    64     ,
       
    65 
       
    66     let $primitiveVariables as element(Variable)+ := $inDoc/GCC_XML/Variable[local:isPrimitive(@type)]
       
    67     return if (exists($primitiveVariables))
       
    68            then (<p xmlns="http://www.w3.org/1999/xhtml/">Mutable global variables with primitives types:</p>,
       
    69                  <ol xmlns="http://www.w3.org/1999/xhtml/">
       
    70                     {
       
    71                         (: For each Variable in $complexVariables... :)
       
    72                         $primitiveVariables/<li><span class="variableName">{string(@name)}</span> in {local:location(.)}</li>
       
    73                     }
       
    74                  </ol>)
       
    75            else <p xmlns="http://www.w3.org/1999/xhtml/">No mutable primitive global variables found.</p>
       
    76 };
       
    77 
       
    78 (:
       
    79     This is where the <html> report is output. First 
       
    80     there is some style stuff, then the <body> element,
       
    81     which contains the call to the \c{local:report()}
       
    82     declared above.
       
    83 :)
       
    84 <html xmlns="http://www.w3.org/1999/xhtml/" xml:lang="en" lang="en">
       
    85     <head>
       
    86         <title>Global variables report for {$fileToOpen}</title>
       
    87     </head>
       
    88     <style type="text/css">
       
    89         .details
       
    90         {{
       
    91             text-align: left;
       
    92             font-size: 80%;
       
    93             color: blue
       
    94         }}
       
    95         .variableName
       
    96         {{
       
    97             font-family: courier;
       
    98 	    color: blue
       
    99         }}
       
   100     </style>
       
   101 
       
   102     <body>
       
   103         <p class="details">Start report: {current-dateTime()}</p>
       
   104         {
       
   105             local:report()
       
   106         }
       
   107         <p class="details">End report: {current-dateTime()}</p>
       
   108     </body>
       
   109 
       
   110 </html>