JavaDude's Groovy Snippets

July 26, 2010

Add libraries to groovyConsole ?

Filed under: groovy — Tags: , , , — admin @ 8:25 am

For little experiments and trying snippets of Groovy-Code the console is good enough. But soon you will realize, you need more libraries. You can either manually add them via “Script | Add jar file to classpath” (every time you open the console you would need to do this) or, for a permanent solution, copy it into you Groovy Home / lib folder, but that is not a  proper approach, rather copy the desired files into .groovy/lib in your user home directory (Linux).

groovyConsole

Add libraries

January 12, 2010

Accessing Trac with Groovy and the XML-RPC library

Filed under: Groovy Snippets, Groovy Tutorial — Tags: , , — admin @ 5:47 pm

With Groovy and the XML-RPC library (groovy.codehaus.org/XMLRPC) the access to Trac gets extremely easy. The older version 0.4 and 0.5 of the library could not handle basic authorization but thanks to Tim fixing the missing feature and some other xml format error, you can now access any Trac Wikipage with as little as 3 lines of Groovy code.

  1. def serverProxy = new XMLRPCServerProxy("https://user:password@sometracserver.com/login/xmlrpc")
  2. // or
  3. // def serverProxy = new XMLRPCServerProxy("http://sometracserver.com/login/xmlrpc")
  4. serverProxy.setBasicAuth("user","password")
  5. println serverProxy.wiki.getPage("TracBackup")

Remark:
At the time of this entry version 0.6 is not released yet. Check for availability at repository.codehaus.org/org/codehaus/groovy/groovy-xmlrpc/ for the new version or update your svn copy and build by yourself (see blog entry)

Update 2010-02-01: The new version was released as 0.5.1 (link)

January 6, 2010

Groovy XML-RPC library 0.5

Filed under: Groovy News — Tags: , — admin @ 4:52 pm

Update: Thanks to Guillaume’s persistence the version 0.5 was released officially today. Have a look at http://repository.codehaus.org/org/codehaus/groovy/groovy-xmlrpc/ to find the latest release of the library. Thanks.

I will continue now with my main project, to create a Netbeans plugin for Trac.

December 27, 2009

Snippet: Accessing SNIPPLR with Groovy and Apache XML-RPC

Filed under: Groovy Snippets, Groovy Tutorial — Tags: , , , — admin @ 9:49 am

In the previous post we used the Groovy XML-RPC library, in this snippet we will use the Apache XML-RPC library. Why ? There is no basic security implemented in the Groovy XML-RPC to access for example a

The prerequisites are still the same, just we change the library:

  • Download the Apache library from here. You should download a file from the binaries folder (apache-xmlrpc-3.1.2-bin.zip), inside you will find a lib subfolder with a couple of jars we need to add to the classpath.
    (commons-logging-1.1.jar, xmlrpc-client-3.1.2.jar, xmlrpc-server-3.1.2.jar, ws-commons-util-1.0.2.jar, xmlrpc-common-3.1.2.jar)

Snippet:


 

Remark: You can add more parameters for the server with

  • config.setEncoding(“UTF-8″)
    config.setBasicUserName(“[username]“);
    config.setBasicPassword(“[password]“);

December 25, 2009

Snippet: Create simple XML with StreamingMarkupBuilder

Filed under: Groovy Snippets — Tags: , , — admin @ 8:42 am

The MarkupBuilder is lacking of xml declaration features, so I ported the previous sample to StreamingMarkupBuilder. Unfortunately it throws the complete XML content into 1 line without CR, to make it again human-readable, aka pretty-print it, we use the XmlUtil.serialize method.

.


 

.

Note: mkp is part of the XML helperclass (link)

.

December 24, 2009

Snippet: Create XML file with MarkupBuilder

Filed under: Groovy Snippets — Tags: , , — admin @ 3:39 pm


 

Please note the difference between MarkupBuilder and StramingMarkupBuilder. More information about XML processing at groovy.codehaus.org
MarkupBuilder is not very flexible, but simple to use for some basic task. If you require comments, XML declarations and more, your rather use the StreamingMarkupBuilder.

Powered by WordPress