!




!
!


        http://groups.google.com/group/jggug-summer-2009
!


    !


        jggug-summer-2009@googlegroups.com
        subject                    /
    !


!
!


    !


!


    !


!


    !
!




!           JAVA_HOME          GROOVY_HOME

!   $GROOVY_HOME/bin

!


    % groovy -v
    Groovy Version: 1.6.4 JVM: 1.5.0_19
!


    !


!


    !


        –
    !


        –
    !


        –
!




    !   Input/OutputStream   Reader/Writer


    % groovy filecopy <srcfile> <destfile>
!


!


    !


    !
                     File#getText(), readBytes(), append(), leftShift()...
    !


    !
                   String#execute()



        groovy.util.AntBuilder
java.io.File



src = new File(args[0])
dest = new File(args[1])
dest << src.text
                dest.leftShift(src.getText())
                leftShift append
                          GDK
src = new File(args[0])
dest = new File(args[1])
dest << src.readBytes()




             OutOfMemoryError
proc = "cp ${args[0]} ${args[1]}".execute()
proc.waitFor()

         waitFor()
         cp



                     OK   UNIX cp
ant = new AntBuilder()
ant.copy(file:args[0], tofile:args[1])

       Ant copy


                        OK
!




!




    % groovy feedreader <URL>
!




!
<rdf:RDF>
  <channel>
  ...
  </channel>
  <item>
    <title>...</title>
    ...
  </item>
  <item>
    <title>...</title>
    ...
  </item>
</rdf:RDF>
<rss>
  <channel>
    ...
    <item>
      <title>...</title>
      ...
    </item>
    <item>
      <title>...</title>
      ...
    </item>
  </channel>
</rss>
<feed>
  ...
  <entry>
    <title>...</title>
    ...
  </entry>
  <entry>
    <title>...</title>
    ...
  </entry>
</feed>
root = new XmlSlurper().parse(args[0])
switch(root.name()) {
  case 'RDF' : nodes = root.item;         break
  case 'rss' : nodes = root.channel.item; break
  case 'feed': nodes = root.entry;        break
}
nodes.each { println it.title }
!




    !




    % groovy -l 8080 httpserver
!   -l <port>
    !




    !

                    line

    !                      return
        'success'
if (!line) {
  println 'HTTP/1.0 200 OK'
  println 'Content-Type: text/htmln'
  println new File('message.html').text
  return 'success'
}

                If
!




    % groovy -l 8080 httpserver
!


    !
import groovy.text.SimpleTemplateEngine as STE

if (!line) {
  println 'HTTP/1.0 200 OK'
  println 'Content-Type: text/htmln'
  text = new File('message.html').text
  println new STE().createTemplate(text).make()
  return 'success'
}
!




    !


    !      http://ja.doukaku.org/205/


    % groovy cmdopt <arguments...>
cli = new CliBuilder(usage:'cmdopt -o [-q] [-d{0|1|2}]
      [     ...]')
cli.with {
  o longOpt:'output', 'set output', required:true
  q longOpt:'quote', 'set quote'
  d longOpt:'debug', 'set debug level', args:1,
argName:'level'
}

if (!(opt = cli.parse(args))) die 'bad command line'
params = opt.arguments()
if (params.size() < 1) die 'parameter must be specified'
level = opt.d ? opt.d as int : 0
if (!(level in 0..2)) die 'debug level must be in 0-2'
println '[            ]'
println "o(output): ${opt.o ? 'ON' : 'OFF'}"
println "q(quote): ${opt.q ? 'ON' : 'OFF'}"
println "d(debug): $level"
println 'n[            ]'
println "     : ${params.size()}"
params.eachWithIndex { param, i -> println "${i + 1}:
$param" }

def die(msg) {
  cli.writer.println "error: $msg"
  cli.usage()
  System.exit 1
}

お題でGroovyプログラミング: Part A

  • 2.
  • 3.
    ! http://groups.google.com/group/jggug-summer-2009 ! ! jggug-summer-2009@googlegroups.com subject / ! !
  • 4.
    ! ! ! ! ! !
  • 5.
    ! ! JAVA_HOME GROOVY_HOME ! $GROOVY_HOME/bin ! % groovy -v Groovy Version: 1.6.4 JVM: 1.5.0_19
  • 6.
    ! ! ! ! – ! – ! –
  • 8.
    ! ! Input/OutputStream Reader/Writer % groovy filecopy <srcfile> <destfile>
  • 9.
    ! ! ! ! File#getText(), readBytes(), append(), leftShift()... ! ! String#execute() groovy.util.AntBuilder
  • 10.
    java.io.File src = newFile(args[0]) dest = new File(args[1]) dest << src.text dest.leftShift(src.getText()) leftShift append GDK
  • 11.
    src = newFile(args[0]) dest = new File(args[1]) dest << src.readBytes() OutOfMemoryError
  • 12.
    proc = "cp${args[0]} ${args[1]}".execute() proc.waitFor() waitFor() cp OK UNIX cp
  • 13.
    ant = newAntBuilder() ant.copy(file:args[0], tofile:args[1]) Ant copy OK
  • 14.
    ! ! % groovy feedreader <URL>
  • 15.
  • 16.
    <rdf:RDF> <channel> ... </channel> <item> <title>...</title> ... </item> <item> <title>...</title> ... </item> </rdf:RDF>
  • 17.
    <rss> <channel> ... <item> <title>...</title> ... </item> <item> <title>...</title> ... </item> </channel> </rss>
  • 18.
    <feed> ... <entry> <title>...</title> ... </entry> <entry> <title>...</title> ... </entry> </feed>
  • 19.
    root = newXmlSlurper().parse(args[0]) switch(root.name()) { case 'RDF' : nodes = root.item; break case 'rss' : nodes = root.channel.item; break case 'feed': nodes = root.entry; break } nodes.each { println it.title }
  • 20.
    ! ! % groovy -l 8080 httpserver
  • 21.
    ! -l <port> ! ! line ! return 'success'
  • 22.
    if (!line) { println 'HTTP/1.0 200 OK' println 'Content-Type: text/htmln' println new File('message.html').text return 'success' } If
  • 23.
    ! % groovy -l 8080 httpserver
  • 24.
    ! !
  • 25.
    import groovy.text.SimpleTemplateEngine asSTE if (!line) { println 'HTTP/1.0 200 OK' println 'Content-Type: text/htmln' text = new File('message.html').text println new STE().createTemplate(text).make() return 'success' }
  • 26.
    ! ! ! http://ja.doukaku.org/205/ % groovy cmdopt <arguments...>
  • 27.
    cli = newCliBuilder(usage:'cmdopt -o [-q] [-d{0|1|2}] [ ...]') cli.with { o longOpt:'output', 'set output', required:true q longOpt:'quote', 'set quote' d longOpt:'debug', 'set debug level', args:1, argName:'level' } if (!(opt = cli.parse(args))) die 'bad command line' params = opt.arguments() if (params.size() < 1) die 'parameter must be specified' level = opt.d ? opt.d as int : 0 if (!(level in 0..2)) die 'debug level must be in 0-2'
  • 28.
    println '[ ]' println "o(output): ${opt.o ? 'ON' : 'OFF'}" println "q(quote): ${opt.q ? 'ON' : 'OFF'}" println "d(debug): $level" println 'n[ ]' println " : ${params.size()}" params.eachWithIndex { param, i -> println "${i + 1}: $param" } def die(msg) { cli.writer.println "error: $msg" cli.usage() System.exit 1 }