Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Download a web page

// Groovy code to download a web-page


def download(address, outfile)
{
    def file = new FileOutputStream(outfile)
    def out = new BufferedOutputStream(file)
    out << new URL(address).openStream()
    out.close()
}

download("http://www.google.com", "google.html");

Groovy - Plain Text Word Wrap method

// Groovy Method to perform word-wrap to a specified length.
// Returns a List of strings representing the wrapped text

// Quick and Dirty method for plain text word-wrap to a specified width
static class TextUtils {
	
	static String[] wrapntab(input, linewidth = 70, indent = 0) throws IllegalArgumentException {
		if(input == null)
			throw new IllegalArgumentException("Input String must be non-null")
		if(linewidth <= 1)
			throw new IllegalArgumentException("Line Width must be greater than 1")
		if(indent <= 0)
			throw new IllegalArgumentException("Indent must be greater than 0")
		
		def olines = []
		def oline = " " * indent
		
		input.split(" ").each() { wrd ->
			if( (oline.size() + wrd.size()) <= linewidth ) {
				oline <<= wrd <<= " "
			}else{
				olines += oline
				oline = " " * indent
			}
		}
		olines += oline
		return olines
	}
}

// TEST
// the input String
input = "Note From SUPPLIER: Booking confirmed by fax.  4 standard rooms - 3 twin shared, 1 single room, please advise if guests require meals.. "

// call static wrapntab method to break the input string into 70 char wide lines with a 4 char initial indent
olines = TextUtils.wrapntab(input,70,4)

// print the output
olines.each() {
	println it
}

Format code with line numbers in Markdown notation

C:\>type Format.groovy | groovy Format
     1: def counter = 1
     2:
     3: System.in.eachLine {
     4:         line ->
     5:
     6:         def result = "    "
     7:         (2 - ("" + counter).length()).times { result += " " }
     8:         result += "${counter}: ${line}"
     9:         println result
    10:         counter++
    11: }

C:\>


def counter = 1

System.in.eachLine {
	line ->
	
	def result = "    "
	(2 - ("" + counter).length()).times { result += " " }
	result += "${counter}: ${line}"
	println result
	counter++
}

Send a bad request to a web service for testing garbage resilience

// description of your code here

def link = "http://localhost:8080/webservices/MyWebService"

def url = new URL(link)

def conn = url.openConnection()
conn.doOutput = true

conn.outputStream << "BROL"

conn.inputStream.eachLine { println it }

capturing grails GSP output

// this creates the template manually and captures the output of the GSP in a StringWriter called "writer"

def binding = [ dude : Dude.get( params.id ) ];  
def writer = new StringWriter();
def template = grailsAttributes.getPagesTemplateEngine().createTemplate("/WEB-INF/grails-app/views/dude/show.gsp", servletContext, request, response);
template.make(binding).writeTo(writer);
println(writer);
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS