вторник, 10 декабря 2013 г.

понедельник, 28 октября 2013 г.

groovy script for running jetty server

The following script starts jetty server and opens the folder, specified on command line, for http access (read-only):
Usage:
  1. Save this script to file “jetty.groovy”
  2. Invoke on command-line:
    groovy jetty.groovy /path/to/some/folder"
  3. Enter address in web-browser:
    http://localhost:8080/jetty
Expected result: you see the content of the folder “/path/to/some/folder” in the web-browser.

пятница, 20 сентября 2013 г.

groovy switch: nasty bug

I found nasty error in Groovy compiler. Consider the following code:

1
2
3
4
5
6
7
8
byte b = 1
switch(b) {
  case 0..9:
    println 'it is between 0 and 9'
  break
  default:
    println 'it is something else'
}

It executes 'default' part, not the part with 0..9, which is not what a programmer would typically expect.
The reason behind it should be related to type conversion between "byte" and "int" types. With the following workaround:

1
swtch((int)b)

the program executes "proper" case.

среда, 4 сентября 2013 г.

groovy: switch statement and closure comprehension – nice for DSL

It is rather easy to extend groovy switch statement with our own DSL:


The trick here is that single-argument versions of IsGreaterThan, IsLessThan return closures. Switch-statement “understands” closures: it passes it’s argument (x in our case) as a parameter to the closure and expects boolean result being returned from the closure.Same thing can be done via function currying, but it looks not so nice, as with function overload.

среда, 7 августа 2013 г.

Groovy DSL == thermonuclear way of writing XML

The code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import groovy.xml.MarkupBuilder

String createEAD(Closure closure) {
  def writer = new StringWriter()
  def xml = new MarkupBuilder(writer)
  xml.mkp.xmlDeclaration(version: '1.0', encoding: 'UTF-8')
  xml.'ead:ead'('xmlns:ead': 'urn:isbn:1-931666-22-9') {
    closure.delegate = new Object() {
      def text(Map attrs, content) {
        def a = attrs.keySet().find { it in ['bold', 'italic', 'underline'] }
        if(a && attrs[a]) {
          xml.'ead:emph' render: a, {
            text attrs.findAll({ it.key != a }), content
          }
        } else
          text content
      }
      def text(content) {
        if(content instanceof String)
          xml.mkp.yield content
        else if(content instanceof Closure)
          content()
      }
    }
    closure()
  }
  return writer.toString()
}

println createEAD {
  text bold: true, italic: true, {
    text 'Hello, '
    text underline: true, 'world!'
  }
}


produces XML:

1
2
3
4
5
6
7
8
<?xml version='1.0' encoding='UTF-8'?>
<ead:ead xmlns:ead='urn:isbn:1-931666-22-9'>
  <ead:emph render='bold'>
    <ead:emph render='italic'>Hello, 
      <ead:emph render='underline'>world!</ead:emph>
    </ead:emph>
  </ead:emph>
</ead:ead>

Firefox 23: Javascript is always ON

From Firefox 23 Release Notes:
"Enable JavaScript" preference checkbox has been removed and user-set values will be reset to the default
YESSS!!! javascript is now always ON, it is not even possible to turn it off! Other browsers, unite!
Bureaucrats will probably have to reconsider what they mean under Barrierefreiheit.