<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: groovy code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 29 Aug 2008 13:56:16 GMT</pubDate>
    <description>DZone Snippets: groovy code</description>
    <item>
      <title>List the 10 most recent files: Groovy version.</title>
      <link>http://snippets.dzone.com/posts/show/5924</link>
      <description>&lt;code&gt;&lt;br /&gt;def recents = new File(".").listFiles().findAll { it.file }.sort { it.lastModified() }.reverse()[0..9]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Aug 2008 05:16:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5924</guid>
      <author>marcospereira (Marcos Silva Pereira)</author>
    </item>
    <item>
      <title>Showing a use to DELEGATE_FIRST closure resolve strategy: Selenium navigation</title>
      <link>http://snippets.dzone.com/posts/show/5923</link>
      <description>// Show how to use groovy closures to write more clear selenium code&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// More details here: http://snippets.dzone.com/posts/show/5922&lt;br /&gt;// first, add a method to Selenium class&lt;br /&gt;Selenium.metaClass.execute = { closure -&gt;&lt;br /&gt;    closure.delegate = delegate&lt;br /&gt;    closure.resolveStrategy = Closure.DELEGATE_FIRST&lt;br /&gt;    closure()&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// making selenium stuff more clear&lt;br /&gt;&lt;br /&gt;def selenium = getSelenium()&lt;br /&gt;selenium.execute {&lt;br /&gt;    type "field", "value"&lt;br /&gt;    click "submitButton"&lt;br /&gt;    waitForPageToLoad "5000"&lt;br /&gt;    // do some more stuff&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Aug 2008 05:06:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5923</guid>
      <author>marcospereira (Marcos Silva Pereira)</author>
    </item>
    <item>
      <title>Making the methods and properties lookups inside a closure be resolved to the delegate.</title>
      <link>http://snippets.dzone.com/posts/show/5922</link>
      <description>// The following code shows how methods and properties will be resolved to the delegate instead of the closure itself.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// creating a closure&lt;br /&gt;def closure = {&lt;br /&gt;    add "groovy"&lt;br /&gt;    add "java"&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// declaring a list&lt;br /&gt;def list = []&lt;br /&gt;&lt;br /&gt;// making the call methods inside closure be resolved to the list&lt;br /&gt;closure.delegate = list&lt;br /&gt;closure.resolveStrategy = Closure.DELEGATE_FIRST&lt;br /&gt;&lt;br /&gt;// executing the closure&lt;br /&gt;closure();&lt;br /&gt;&lt;br /&gt;// now, the list was changed&lt;br /&gt;list.each { println it }&lt;br /&gt;&lt;br /&gt;// output &lt;br /&gt;// groovy&lt;br /&gt;// java&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Aug 2008 05:00:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5922</guid>
      <author>marcospereira (Marcos Silva Pereira)</author>
    </item>
    <item>
      <title>Download a web page</title>
      <link>http://snippets.dzone.com/posts/show/5541</link>
      <description>// Groovy code to download a web-page&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;def download(address, outfile)&lt;br /&gt;{&lt;br /&gt;    def file = new FileOutputStream(outfile)&lt;br /&gt;    def out = new BufferedOutputStream(file)&lt;br /&gt;    out &lt;&lt; new URL(address).openStream()&lt;br /&gt;    out.close()&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;download("http://www.google.com", "google.html");&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 27 May 2008 11:07:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5541</guid>
      <author>sandeepsinghal (Sandeep Singhal)</author>
    </item>
    <item>
      <title>Groovy - Plain Text Word Wrap method</title>
      <link>http://snippets.dzone.com/posts/show/4839</link>
      <description>// Groovy Method to perform word-wrap to a specified length.&lt;br /&gt;// Returns a List of strings representing the wrapped text&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Quick and Dirty method for plain text word-wrap to a specified width&lt;br /&gt;static class TextUtils {&lt;br /&gt;	&lt;br /&gt;	static String[] wrapntab(input, linewidth = 70, indent = 0) throws IllegalArgumentException {&lt;br /&gt;		if(input == null)&lt;br /&gt;			throw new IllegalArgumentException("Input String must be non-null")&lt;br /&gt;		if(linewidth &lt;= 1)&lt;br /&gt;			throw new IllegalArgumentException("Line Width must be greater than 1")&lt;br /&gt;		if(indent &lt;= 0)&lt;br /&gt;			throw new IllegalArgumentException("Indent must be greater than 0")&lt;br /&gt;		&lt;br /&gt;		def olines = []&lt;br /&gt;		def oline = " " * indent&lt;br /&gt;		&lt;br /&gt;		input.split(" ").each() { wrd -&gt;&lt;br /&gt;			if( (oline.size() + wrd.size()) &lt;= linewidth ) {&lt;br /&gt;				oline &lt;&lt;= wrd &lt;&lt;= " "&lt;br /&gt;			}else{&lt;br /&gt;				olines += oline&lt;br /&gt;				oline = " " * indent&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		olines += oline&lt;br /&gt;		return olines&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// TEST&lt;br /&gt;// the input String&lt;br /&gt;input = "Note From SUPPLIER: Booking confirmed by fax.  4 standard rooms - 3 twin shared, 1 single room, please advise if guests require meals.. "&lt;br /&gt;&lt;br /&gt;// call static wrapntab method to break the input string into 70 char wide lines with a 4 char initial indent&lt;br /&gt;olines = TextUtils.wrapntab(input,70,4)&lt;br /&gt;&lt;br /&gt;// print the output&lt;br /&gt;olines.each() {&lt;br /&gt;	println it&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 04 Dec 2007 00:43:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4839</guid>
      <author>dwp (dave pugh)</author>
    </item>
    <item>
      <title>Format code with line numbers in Markdown notation</title>
      <link>http://snippets.dzone.com/posts/show/4642</link>
      <description>&lt;code&gt;&lt;br /&gt;C:\&gt;type Format.groovy | groovy Format&lt;br /&gt;     1: def counter = 1&lt;br /&gt;     2:&lt;br /&gt;     3: System.in.eachLine {&lt;br /&gt;     4:         line -&gt;&lt;br /&gt;     5:&lt;br /&gt;     6:         def result = "    "&lt;br /&gt;     7:         (2 - ("" + counter).length()).times { result += " " }&lt;br /&gt;     8:         result += "${counter}: ${line}"&lt;br /&gt;     9:         println result&lt;br /&gt;    10:         counter++&lt;br /&gt;    11: }&lt;br /&gt;&lt;br /&gt;C:\&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def counter = 1&lt;br /&gt;&lt;br /&gt;System.in.eachLine {&lt;br /&gt;	line -&gt;&lt;br /&gt;	&lt;br /&gt;	def result = "    "&lt;br /&gt;	(2 - ("" + counter).length()).times { result += " " }&lt;br /&gt;	result += "${counter}: ${line}"&lt;br /&gt;	println result&lt;br /&gt;	counter++&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 12 Oct 2007 12:09:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4642</guid>
      <author>devijvers (Steven Devijver)</author>
    </item>
    <item>
      <title>Send a bad request to a web service for testing garbage resilience</title>
      <link>http://snippets.dzone.com/posts/show/4641</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def link = "http://localhost:8080/webservices/MyWebService"&lt;br /&gt;&lt;br /&gt;def url = new URL(link)&lt;br /&gt;&lt;br /&gt;def conn = url.openConnection()&lt;br /&gt;conn.doOutput = true&lt;br /&gt;&lt;br /&gt;conn.outputStream &lt;&lt; "BROL"&lt;br /&gt;&lt;br /&gt;conn.inputStream.eachLine { println it }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 12 Oct 2007 12:07:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4641</guid>
      <author>devijvers (Steven Devijver)</author>
    </item>
    <item>
      <title>capturing grails GSP output</title>
      <link>http://snippets.dzone.com/posts/show/3852</link>
      <description>// this creates the template manually and captures the output of the GSP in a StringWriter called "writer"&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def binding = [ dude : Dude.get( params.id ) ];  &lt;br /&gt;def writer = new StringWriter();&lt;br /&gt;def template = grailsAttributes.getPagesTemplateEngine().createTemplate("/WEB-INF/grails-app/views/dude/show.gsp", servletContext, request, response);&lt;br /&gt;template.make(binding).writeTo(writer);&lt;br /&gt;println(writer);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 22 Apr 2007 19:58:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3852</guid>
      <author>graham (graham o'regan)</author>
    </item>
  </channel>
</rss>
