!




!
!


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


    !


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


    !


!


    !


!


    !
!




!           JAVA_HOME          GROOVY_HOME

!   $GROOVY_HOME/bin

!


    % groovy -v
    Groovy Version: 1.6.4 JVM:...
!


    !


!


    !


        –
    !


        –
    !


        –
!




    !   Input/OutputStream   Reader/Writer


    % groovy filecopy <srcfile> <destfile>
!


!


    !


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


    !
           ...
java.io.File



src = new File(args[0])
dest = new File(args[1])
dest << src.text
                dest.leftShift(src.getTe...
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   UN...
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>...
<rss>
  <channel>
    ...
    <item>
      <title>...</title>
      ...
    </item>
    <item>
      <title>...</title>
  ...
<feed>
  ...
  <entry>
    <title>...</title>
    ...
  </entry>
  <entry>
    <title>...</title>
    ...
  </entry>
</fee...
root = new XmlSlurper().parse(args[0])
switch(root.name()) {
  case 'RDF' : nodes = root.item;         break
  case 'rss' ...
!




    !




    % 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
  re...
!




    % groovy -l 8080 httpserver
!


    !
import groovy.text.SimpleTemplateEngine as STE

if (!line) {
  println 'HTTP/1.0 200 OK'
  println 'Content-Type: text/htm...
!




    !


    !      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', r...
println '[            ]'
println "o(output): ${opt.o ? 'ON' : 'OFF'}"
println "q(quote): ${opt.q ? 'ON' : 'OFF'}"
println ...
お題でGroovyプログラミング: Part A
お題でGroovyプログラミング: Part A
Upcoming SlideShare
Loading in …5
×

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

1,342 views
1,281 views

Published on

JGGUGサマースペシャルg*ワークショップ2009での発表資料。Part Bは上原さん、Part Cは須江さん。

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,342
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

  1. 1. ! !
  2. 2. ! http://groups.google.com/group/jggug-summer-2009 ! ! jggug-summer-2009@googlegroups.com subject / ! !
  3. 3. ! ! ! ! ! !
  4. 4. ! ! JAVA_HOME GROOVY_HOME ! $GROOVY_HOME/bin ! % groovy -v Groovy Version: 1.6.4 JVM: 1.5.0_19
  5. 5. ! ! ! ! – ! – ! –
  6. 6. ! ! Input/OutputStream Reader/Writer % groovy filecopy <srcfile> <destfile>
  7. 7. ! ! ! ! File#getText(), readBytes(), append(), leftShift()... ! ! String#execute() groovy.util.AntBuilder
  8. 8. java.io.File src = new File(args[0]) dest = new File(args[1]) dest << src.text dest.leftShift(src.getText()) leftShift append GDK
  9. 9. src = new File(args[0]) dest = new File(args[1]) dest << src.readBytes() OutOfMemoryError
  10. 10. proc = "cp ${args[0]} ${args[1]}".execute() proc.waitFor() waitFor() cp OK UNIX cp
  11. 11. ant = new AntBuilder() ant.copy(file:args[0], tofile:args[1]) Ant copy OK
  12. 12. ! ! % groovy feedreader <URL>
  13. 13. ! !
  14. 14. <rdf:RDF> <channel> ... </channel> <item> <title>...</title> ... </item> <item> <title>...</title> ... </item> </rdf:RDF>
  15. 15. <rss> <channel> ... <item> <title>...</title> ... </item> <item> <title>...</title> ... </item> </channel> </rss>
  16. 16. <feed> ... <entry> <title>...</title> ... </entry> <entry> <title>...</title> ... </entry> </feed>
  17. 17. 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 }
  18. 18. ! ! % groovy -l 8080 httpserver
  19. 19. ! -l <port> ! ! line ! return 'success'
  20. 20. if (!line) { println 'HTTP/1.0 200 OK' println 'Content-Type: text/htmln' println new File('message.html').text return 'success' } If
  21. 21. ! % groovy -l 8080 httpserver
  22. 22. ! !
  23. 23. 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' }
  24. 24. ! ! ! http://ja.doukaku.org/205/ % groovy cmdopt <arguments...>
  25. 25. 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'
  26. 26. 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 }

×