Wiki source code of Creating a Groovy Class

Version 6.1 by Silvia Macovei on 2010/02/25

Show last authors
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 * Create a new page, for example "Groovy.HelloWorldClass" containing :
64 (((
65 {{info}} This page must have been saved by a user with programming rights to be executed {{/info}}
66 )))
67 (((
68 {{groovy}}
69 groovyObject = xwiki.parseGroovyFromPage("Dev.HelloWorldGroovyClass")
70 groovyObject.setObjects(xwiki, context)
71 print(groovyObject.helloWorld())
72 {{/groovy}}
73 )))
74
75 * See the result, feeling groovy ? ;)
76
77 = More documentation =
78 Do search around for Groovy language, it's pretty rich.
79
80 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]]
81 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).

Get Connected