Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View kdabir's full-sized avatar
👨‍💻
go git it

Kunal Dabir kdabir

👨‍💻
go git it
View GitHub Profile
@kdabir
kdabir / closure_vs_loop.groovy
Created July 22, 2011 03:22
compare the time taken while iterating over collection with closure and for loop
def file = "input.txt"
// just to heat up the jvm :)
new File(file).eachLine{
it.toUpperCase()
}
// with closure
start = System.nanoTime()
new File(file).eachLine{
it.toUpperCase()
@kdabir
kdabir / repeating_words.groovy
Created February 21, 2012 17:02
to find if any word exists in *every* line in input
/**
* Problem: to find if any word exists in *every* line in input
*/
def input = """This is big line
This is another big big line
There is something common here
"""
/* APPROACH ONE : order of O(n)
@kdabir
kdabir / check_proxy.groovy
Created February 22, 2012 12:36
Setting proxy in groovy
println System.properties.grep ({it.key.contains("proxy")})
// print all system properties
// System.properties.each {k,v-> println "$k=$v"}
@kdabir
kdabir / rss_read.groovy
Created February 22, 2012 12:58
Parsing rss in groovy
// define the url
def url = "http://news.google.com/news?ned=us&topic=h&output=rss"
def rss = new XmlSlurper().parse(url)
println rss.channel.title
rss.channel.item.each {
println "- ${it.title}"
}
@kdabir
kdabir / get_content.groovy
Created February 22, 2012 13:04
get content from url and write to a file in groovy
// saving from url to a file (append)
new File("output.xml") << new URL ("http://some.url/some/path.xml").getText()
@kdabir
kdabir / html_markup.groovy
Last active September 15, 2020 05:15
Using MarkupBuilder to generate html markup in groovy
// MarkupBuilder is a lot cleaner way of generating valid xml/html markup
// than writing tags as string and forgetting to close one ;)
def writer = new StringWriter() // html is written here by markup builder
def markup = new groovy.xml.MarkupBuilder(writer) // the builder
markup.html{
table {
tr {
td(class:"row", "hello world!")
@kdabir
kdabir / mvn_compile_win.groovy
Created February 22, 2012 13:32
executing system process with groovy
// following process executes 'mvn compile' from a groovy script
// it can be replaced with any system program
Process compile = "cmd /c mvn compile".execute() // 'cmd /c' works only on win platform
compile.waitForProcessOutput(out, err)
if (out) println "out:\n$out"
if (err) println "err:\n$err"
exitValue = compile.exitValue()
@kdabir
kdabir / read_excel.groovy
Created February 22, 2012 13:51
Reading MS Excel file in groovy
def file_path = "C:/path/to/excel/file.xlsx"
def read_only = true
// Unfortunately this will work only on windows because of this driver :(
def connection_url= """jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
DBQ=$file_path;READONLY=$read_only"""
if (!new File(file_path).exists()) return "invalid file"
def sql = groovy.sql.Sql.newInstance(connection_url, '', '')
@kdabir
kdabir / pom.xml
Created February 22, 2012 14:23
Configuring Cobertura, PMD & Findbugs in Maven
...
<reporting>
<plugins>
<!-- checks the test coverage -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
</plugin>
@kdabir
kdabir / pom.xml
Created February 22, 2012 14:42
Configuring jetty plugin to auto deploy and store session between restarts in Maven
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.24</version> <!-- TODO upgrade to version 7 -->
<configuration>
<stopPort>9669</stopPort>
<stopKey>${appname}</stopKey>
<!-- Redeploy every x seconds if changes are detected,
0 for no automatic redeployment -->
<scanIntervalSeconds>3</scanIntervalSeconds>