Loading a Groovy Script from another Script

3 messages Options
Embed this post
Permalink
Andreas Schaefer

Loading a Groovy Script from another Script

Reply Threaded More More options
Print post
Permalink
When I try to load a Groovy script using xwiki.parseGroovyFromPage()  
then I fail when this Script is enclosed inside '{{groovy}}'. This  
means I have to use two different documents when I want to use the  
same document to be loaded by XWiki and by xwiki.parseGroovyFromPage()  
because one needs the be enclosed by '{{groovy}} and the other can't.

Isn't there a way to remove '{{groovy}}' when parsed so that the  
scripts can be used for both?

In addition the 'GroovyScriptEngineImpl' which is used for the Binding  
does throw a NoSuchPropertyException when a property is not available.  
This makes it cumbersome to test for properties and add them if not  
already put inside. For example I need some objects over and over like  
'BlogParams' and so I would like to store them inside the bindings. In  
order to avoid the exception I either have to catch the Exception  
(time consuming) or I need to get the properties map and check if the  
property is already added to the map.
Is there a better way to do that?

Cheers - Andy
_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs
Sergiu Dumitriu-2

Re: Loading a Groovy Script from another Script

Reply Threaded More More options
Print post
Permalink
On 11/01/2009 09:19 PM, Andreas Schaefer wrote:
> When I try to load a Groovy script using xwiki.parseGroovyFromPage()
> then I fail when this Script is enclosed inside '{{groovy}}'. This
> means I have to use two different documents when I want to use the
> same document to be loaded by XWiki and by xwiki.parseGroovyFromPage()
> because one needs the be enclosed by '{{groovy}} and the other can't.
>
> Isn't there a way to remove '{{groovy}}' when parsed so that the
> scripts can be used for both?

You could make it look like a groovy comment:

// {{groovy}}
content here
// {{/groovy}}

The problem is that the first // will be printed when viewing the page.

Or, we can change parseGroovyFromPage to trim the groovy markers.

> In addition the 'GroovyScriptEngineImpl' which is used for the Binding
> does throw a NoSuchPropertyException when a property is not available.
> This makes it cumbersome to test for properties and add them if not
> already put inside. For example I need some objects over and over like
> 'BlogParams' and so I would like to store them inside the bindings. In
> order to avoid the exception I either have to catch the Exception
> (time consuming) or I need to get the properties map and check if the
> property is already added to the map.
> Is there a better way to do that?
>
> Cheers - Andy

--
Sergiu Dumitriu
http://purl.org/net/sergiu/
_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs
Ludovic Dubost-2

Re: Loading a Groovy Script from another Script

Reply Threaded More More options
Print post
Permalink
In reply to this post by Andreas Schaefer

Hi Andreas,

I'm not sure why you would want both the parseGroovyFromPage and the
script in {{groovy}} tags.

The parseGroovyFromPage is intended to instanciate an object from the
class in the page.
Now you can load groovy in groovy so you can have a script in {{groovy}}
tags load a script coming from a page.

If the use case makes sense, it would be needed to strip out the
{{groovy}} tags in the core becore compiling it.
You can try using parseGroovyFromString to do that yourself.

I cannot really help on the GroovyScriptEngineImpl question

Ludovic

Andreas Schaefer a écrit :

> When I try to load a Groovy script using xwiki.parseGroovyFromPage()  
> then I fail when this Script is enclosed inside '{{groovy}}'. This  
> means I have to use two different documents when I want to use the  
> same document to be loaded by XWiki and by xwiki.parseGroovyFromPage()  
> because one needs the be enclosed by '{{groovy}} and the other can't.
>
> Isn't there a way to remove '{{groovy}}' when parsed so that the  
> scripts can be used for both?
>
> In addition the 'GroovyScriptEngineImpl' which is used for the Binding  
> does throw a NoSuchPropertyException when a property is not available.  
> This makes it cumbersome to test for properties and add them if not  
> already put inside. For example I need some objects over and over like  
> 'BlogParams' and so I would like to store them inside the bindings. In  
> order to avoid the exception I either have to catch the Exception  
> (time consuming) or I need to get the properties map and check if the  
> property is already added to the map.
> Is there a better way to do that?
>
> Cheers - Andy
> _______________________________________________
> devs mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/devs
>
>  


--
Ludovic Dubost
Blog: http://blog.ludovic.org/
XWiki: http://www.xwiki.com
Skype: ldubost GTalk: ldubost

_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs