Wiki source code of Creating a Groovy Class

Version 18.3 by RicardoJafe on 2012/03/29

Show last authors
1 {{warning}}
2 Work in progress, only a snippet for the moment
3 {{/warning}}
4
5 This tutorial illustrates the ##XWiki.parseGroovyFromPage## API method. This method allow you to instantiate a groovy class from both velocity and groovy code.
6
7 = Create a groovy class =
8
9 * Create a new page, for example ##Groovy.HelloWorldClass## containing :(((
10 {{info}}This page must have been saved by a user with programming [[rights>>platform:Features.RightsManagement]] to be executed{{/info}}
11 )))(((
12 {{info}}
13 When creating a page to access via "parseGroovyFromString", make sure you do not have opening and closing groovy tags (don't put {{code}}{{groovy}} and {{/groovy}} {{/code}} on the page)
14
15 {{/info}}
16 )))(((
17 {{code}}
18 /* Groovy Class #* */
19
20 class groovyClass {
21
22 def xwiki;
23 def context;
24
25 void setObjects(xwiki, context) {
26 setXWiki(xwiki);
27 setContext(context);
28 }
29 void setXWiki(xwiki) {
30 this.xwiki = xwiki;
31 }
32
33 void setContext(context) {
34 this.context = context;
35 }
36
37 String helloWorld() {
38 return "Hello World";
39 }
40 }
41
42 /* *# */
43 {{/code}}
44 )))
45
46 Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity.
47
48 {{info}}Notice the ";" - this identifies a new instruction line, and although not mandatory, avoids potential problems if linefeeds are lost.{{/info}}
49
50 {{info}}
51 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.
52 {{/info}}
53
54 = Instantiate and use your class from velocity =
55
56 * Create a new page, for example "Main.HelloWorldFromVelocity" containing :(((
57 {{code}}
58 #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass"))
59 $groovyObject.setObjects($xwiki, $context)
60 $groovyObject.helloWorld()
61 {{/code}}
62 )))
63
64 * See the result, feeling groovy ? ;)
65
66 = Instantiate and use your class from groovy =
67
68 * Create a new page, for example "Groovy.HelloWorldClass" containing :(((
69 {{info}}
70 This page must have been saved by a user with programming rights to be executed
71 {{/info}}
72 )))(((
73 {{code}}
74 {{groovy}}
75 groovyObject = xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")
76 groovyObject.setObjects(xwiki, context)
77 print(groovyObject.helloWorld())
78 {{/groovy}}
79 {{/code}}
80 )))
81
82 * See the result, feeling groovy ? ;)
83
84 = More documentation =
85
86 Do search around for Groovy language, it's pretty rich.
87
88 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]]
89 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