Wiki source code of Creating a Groovy Class

Version 21.3 by RicardoJafe on 2012/03/29

Show last authors
1 {{warning}}
2 Work in progress, only a snippet for the moment. This page is for XWiki Syntax 1.0 only. Don't try to use this with a page written in XWiki Syntax 2.0. For XWiki Syntax 2.0 there are other ways of doing it that we need to document. Please check the [[Syntax Guide>>platform:Main.XWikiSyntax]] to understand the differences.
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 identifiers ("")
14 {{/info}}
15 )))(((
16 {{code}}
17 /* Groovy Class #* */
18
19 class groovyClass {
20
21 def xwiki;
22 def context;
23
24 void setObjects(xwiki, context) {
25 setXWiki(xwiki);
26 setContext(context);
27 }
28 void setXWiki(xwiki) {
29 this.xwiki = xwiki;
30 }
31
32 void setContext(context) {
33 this.context = context;
34 }
35
36 String helloWorld() {
37 return "Hello World";
38 }
39 }
40
41 /* *# */
42 {{/code}}
43 )))
44
45 Notice the trick of putting a Velocity comment in the Groovy comment so that the code is not parsed by Velocity.
46
47 {{info}}Notice the ";" - this identifies a new instruction line, and although not mandatory, avoids potential problems if linefeeds are lost.{{/info}}
48
49 {{info}}
50 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.
51 {{/info}}
52
53 = Instantiate and use your class from velocity =
54
55 * Create a new page, for example "Main.HelloWorldFromVelocity" containing :(((
56 {{code}}
57 #set($groovyObject = $xwiki.parseGroovyFromPage("Groovy.HelloWorldClass"))
58 $groovyObject.setObjects($xwiki, $context)
59 $groovyObject.helloWorld()
60 {{/code}}
61 )))
62
63 * See the result, feeling groovy ? ;)
64
65 = Instantiate and use your class from groovy =
66
67 * Create a new page, for example "Groovy.HelloWorldClass" containing :(((
68 {{info}}
69 This page must have been saved by a user with programming rights to be executed
70 {{/info}}
71 )))(((
72 {{code}}
73 {{groovy}}
74 groovyObject = xwiki.parseGroovyFromPage("Groovy.HelloWorldClass")
75 groovyObject.setObjects(xwiki, context)
76 print(groovyObject.helloWorld())
77 {{/groovy}}
78 {{/code}}
79 )))
80
81 * See the result, feeling groovy ? ;)
82
83 = More documentation =
84
85 Do search around for Groovy language, it's pretty rich.
86
87 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]]
88 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