Wiki source code of Creating a Groovy Class
Version 19.1 by Vincent Massol on 2012/03/29
Show last authors
author | version | line-number | content |
---|---|---|---|
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 | |||
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). |