File: demo.vm

package info (click to toggle)
velocity-tools 2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,952 kB
  • sloc: java: 24,414; xml: 7,944; jsp: 459; makefile: 24
file content (179 lines) | stat: -rw-r--r-- 7,878 bytes parent folder | download | duplicates (6)
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
## Licensed to the Apache Software Foundation (ASF) under one
## or more contributor license agreements.  See the NOTICE file
## distributed with this work for additional information
## regarding copyright ownership.  The ASF licenses this file
## to you under the Apache License, Version 2.0 (the
## "License"); you may not use this file except in compliance
## with the License.  You may obtain a copy of the License at
##
##   http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing,
## software distributed under the License is distributed on an
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
## KIND, either express or implied.  See the License for the
## specific language governing permissions and limitations
## under the License.

#*
 * This template exists to automatically generate demos for a tool.
 * It expects the following values to be set before it is parsed:
 *   $toolname -> should be the context key of the tool
 *   $toolclass -> should be the actual Class of the tool
 *   $toollink -> a URL for the javadoc of the tool Class on velocity.apache.org
 *               This is usually set by copying the $doclink reference that is set
 *               whenever the #doclink() macro is used.
 * Optionally, you can also set:
 *   $toolDemo -> to specify the default text for the full demo at the bottom
 *
 * If you're wondering why this is a template, instead of a macro;
 * it is because it takes less memory in the cache this way.
 *#
#set( $demo = $text.demo )
#set( $tool = $class.inspect($toolclass) )

<table border="1" cellspacing="0" cellpadding="3">
<tr>
  <th>$demo.function</th>
  <form method="get" action="$link.self">
  <th nowrap="1">
    $demo.result<br>
    <input style="font-size: 8pt" type="submit" value="$demo.clear">
  </th>
  ## be sure not to lose the current layout when clearing other params
  #if( $params.layout )
    <input type="hidden" name="layout" value="$params.layout">
  #end
  </form>
  <th>$demo.description</th>
</tr>
## generate a demo for each method (except the configure(Map) method)
#foreach( $method in $loop.watch($tool.methods).exclude('method configure(java.util.Map)') )
#if( (!$skip or !$skip.contains($method.uniqueName)) and (!$skipAll or !$skipAll.contains($method.name)) )
<tr>
  ## create a fake call of the method to use as the name/signature
  #set( $call = "${esc.d}${toolname}.#if( !$method.takesParameters() && $method.propertyName )$method.propertyName#else$method.signature#end" )
  <td class="method" valign="top"><a href="$toollink#$method.javadocRef">$call</a></td>
  #if( !$method.takesParameters() )
    ## simply call the method
    #set( $result = $call )
    #set( $result = $render.eval($call) )
    <td class="demo" valign="top" id="${method.javadocRef}">#if( $result == $call )$text.demo.nullResult#else$esc.xml($result)#end</td>
  #else
    ## create a form to allow the user to input values and call the method
    <form method="get" name="$method.uniqueName" action="$link.self.anchor($link.decode($method.javadocRef))"> ## HttpUnit doesn't like anchor-only links
    <td class="demo" valign="top">
    <nobr>
      ## create a text field for each parameter
      ## whose size is inversely proportional to the number of fields
      #set( $fieldsize = $math.sub($math.idiv(24, $method.parameterCount), $method.parameterCount) )
      #foreach( $param in $method.parameters )
        #set( $fieldname = "$method.uniqueName$velocityCount" )
        ## look for a default value
        #set( $fielddefault = $text.get("${toolname}.${method.uniqueName}.param$velocityCount") )
        #if( !$fielddefault.exists )
          ## look for a default for this type for this method
          #set( $fielddefault = $text.get("${toolname}.${method.name}.$param.simpleName") )
          #if( !$fielddefault.exists )
            ## look for a default for this type for this tool
            #set( $fielddefault = $text.get("${toolname}.$param.simpleName") )
            #if( !$fielddefault.exists )
              ## generate a reasonable default for the param type
              #set( $fielddefault = "#demoDefault( $param )" )
            #end
          #end
        #end
        ## look for a value in the params, use the default if none is found
        #set( $fieldvalue = $display.alt($params.get($fieldname), $fielddefault) )
        #if( $velocityCount > 1 ),#end
        <input type="text" name="$fieldname" value="$fieldvalue" size="$fieldsize"/>
      #end
      <input type="submit" value="$text.demo.try">
    </nobr>
    ## if we have at least one param value
    #if( $params.get("${method.uniqueName}1") )
      ## do the actual call using the provided param values
      #set( $call = "${esc.d}${toolname}.${method.name}(#foreach( $param in $method.parameters )#if( $velocityCount > 1 ), #end$!params.get($render.eval('$method.uniqueName$velocityCount'))#end)" )
      #set( $result = $call )
      #set( $result = $render.eval($call) )
      <br>
      $call
      =
      <div id="$method.javadocRef">#if( $result == $call )$demo.nullResult#else$esc.xml($result)#end</div>
    #end
    ## maintain all params for other method demos
    #foreach( $param in $params.all.keySet() )
      #if( !$param.startsWith($method.uniqueName) && $param != 'fullDemo' )
        #foreach( $value in $params.getStrings($param) )
          <input type="hidden" name="$param" value="$esc.html($value)">
        #end
      #end
    #end
    </td>
    </form>
  #end
  ## look for a description of the specific method
  #set( $desc = $text.get("${toolname}.${method.uniqueName}") )
  #if( !$desc.exists )
    ## look for a description of all methods with that name
    #set( $desc = $text.get("${toolname}.${method.name}") )
    #if( !$desc.exists )
      #if( $method.propertyName )
        ## look for a tool-specific generic property description
        #set( $desc = $text.get("${toolname}.propertyDescription").insert($method.propertyName) )
      #else
        ## look for a generic method description for the tool
        #set( $desc = $text.get("${toolname}.methodDescription") )
      #end
      #if( !$desc.exists )
        #if( $method.uniqueName eq 'toString' )
          ## use the generic toString description 
          #set( $desc = $demo.toString )
        #else
          ## use the generic description missing message
          #set( $desc = $demo.descriptionMissing.insert("${toolname}.${method.uniqueName}") )
        #end
      #end
    #end
  #end
  <td class="desc" valign="top">$desc</td>
</tr>
#end
#end
<tr>
  ## create a space for an extended, custom, one line demo
  <form method="get" action="$link.self.anchor('custom')">
  <td class="demo" valign="top" colspan="2" id="custom">
    ## first check the params for a custom demo
    #set( $custom = $params.getValue('custom', false) )
    ## then look for a default in the resources
    #set( $default = $text.get("${toolname}.custom") )
    #if( !$default.exists )
      ## ok, just have a dumb little custom demo
      #set( $default = "$esc.d$toolname" )
    #end
    <input type="text" size="40" name="custom" value="#if( $custom )$custom#else$default#end">
    <input type="submit" value="$text.demo.try">
    #if( $params.custom )
      #set( $result = $call )
      #set( $result = $render.eval($params.custom) )
      <br>
      $params.custom
      =
      <div>#if( $result == $call )$demo.nullResult#else$esc.xml($result)#end</div>
    #end
    ## maintain params for all the other demos
    #foreach( $param in $params.all.keySet() )
      #if( $param != 'custom' && $param != 'fullDemo' )
        #foreach( $value in $params.getStrings($param) )
          <input type="hidden" name="$param" value="$esc.html($value)">
        #end
      #end
    #end
  </td>
  </form>
  <td>$text.demo.tryAnything</td>
</tr>
</table>

#parse( 'fullDemo.vm' )