Wiki source code of Creating a Groovy Class
Version 7.2 by Vincent Massol on 2010/02/26
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{warning}}Work in progress, only a snippet for the moment{{/warning}} | ||
2 | |||
3 | This tutorial illustrates the ##XWiki.parseGroovyFromPage## API method. This method allow you to instantiate a groovy class from both velocity and groovy code. | ||
4 | |||
5 | = Create a groovy class = | ||
6 | |||
7 | * Create a new page, for example ##Groovy.HelloWorldClass## containing : | ||
8 | ((( | ||
9 | {{info}}This page must have been saved by a user with programming [[rights>>platform:Features.RightsManagement]] to be executed{{/info}} | ||
10 | ))) | ||
11 | ((( | ||
12 | {{info}}When creating a page to access via "parseGroovyFromString", make sure you do not have opening and closing groovy identifiers (""){{/info}} | ||
13 | ))) | ||
14 | ((( | ||
15 | {{code}} | ||
16 | /* Groovy Class #* */ | ||
17 | |||
18 | class groovyClass { | ||
19 | |||
20 | def xwiki; | ||
21 | def context; | ||
22 | |||
23 | void setObjects(xwiki, context) { | ||
24 | setXWiki(xwiki); | ||
25 | setContext(context); | ||
26 | } | ||
27 | void setXWiki(xwiki) { | ||
28 | this.xwiki = xwiki; | ||
29 | } | ||
30 | |||
31 | void setContext(context) { | ||
32 | this.context = context; | ||
33 | } | ||
34 | |||
35 | String helloWorld() { | ||
36 | return "Hello World"; | ||
37 | } | ||
38 | } | ||
39 | |||
40 | /* *# */ | ||
41 | {{/code}} | ||
42 | ))) | ||
43 | |||
44 | Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity. | ||
45 | |||
46 | {{info}}Notice the ";" - this identifies a new instruction line, and although not mandatory, avoids potential problems if linefeeds are lost.{{/info}} | ||
47 | |||
48 | {{info}}As you can see, we can get and store the xwiki and context objects in the class to be able to use them; Their use is not illustrated in this tutorial.{{/info}} | ||
49 | |||
50 | = Instantiate and use your class from velocity = | ||
51 | * Create a new page, for example "Main.HelloWorldFromVelocity" containing : | ||
52 | ((( | ||
53 | {{code}} | ||
54 | #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")) | ||
55 | $groovyObject.setObjects($xwiki, $context) | ||
56 | $groovyObject.helloWorld() | ||
57 | {{/code}} | ||
58 | ))) | ||
59 | |||
60 | * See the result, feeling groovy ? ;) | ||
61 | |||
62 | = Instantiate and use your class from groovy = | ||
63 | |||
64 | * Create a new page, for example "Groovy.HelloWorldClass" containing : | ||
65 | ((( | ||
66 | {{info}} This page must have been saved by a user with programming rights to be executed {{/info}} | ||
67 | ))) | ||
68 | ((( | ||
69 | {{groovy}} | ||
70 | groovyObject = xwiki.parseGroovyFromPage("Dev.HelloWorldGroovyClass") | ||
71 | groovyObject.setObjects(xwiki, context) | ||
72 | print(groovyObject.helloWorld()) | ||
73 | {{/groovy}} | ||
74 | ))) | ||
75 | |||
76 | * See the result, feeling groovy ? ;) | ||
77 | |||
78 | = More documentation = | ||
79 | |||
80 | Do search around for Groovy language, it's pretty rich. | ||
81 | |||
82 | A cute feature is, for example, [[how it can access XML>>http://www.ibm.com/developerworks/java/library/j-pg05199/index.html?S_TACT=105AGX02&S_CMP=EDU]] | ||
83 | Many tools are equipped to edit Groovy, among others [[IntelliJ IDEA>>http://www.jetbrains.com/idea/]] and [[Eclipse>>http://www.eclipse.org/]]. For both Velocity and Groovy, IntelliJ IDEA can be enriched with the type of predefined variables thanks to intentions or dynamic properties (e.g. xwiki, doc, context). |