agileaholic :)

Java, groovy, grails, hacking and more…

Archive for July, 2008

Regex in Groovy

Posted by Prabhu Beeman on July 19, 2008

Target Audience: Regex newbies

I would like to talk more about regex this time. This might be helpful for someone who thought regex to be very complex.

Let us use regex to validate an email (like firstname.lastname@email.com or firstname@email.com)

In regex, what matters is the pattern. Let us examine our pattern one by one.

how should we validate firstname( it should start with a alphabet and can have any number)

def FIRSTNAME = /[a-z]+\d*/

eg: james

we define the pattern always between the backslashes ‘/’

[a-z]   – means a to z

[a-z]* – means a to z can occur one or more times

\d       – means any digit/number

\d*     – means any digit/number zero or more times

def DOT = /[.]?/

[.]  – means a dot should be present

[.]? – means a dot should be present zero or one time

please be aware that  . without the square brackets means ‘can have any character’

def SECONDNAME = /\w*/

eg: gosling

\w -   means any word (includes number as well)

\w* – means any word can occur zero or more times

def AT = /[@]/

[@] – means must have @ symbol

def DOMAIN = /[a-z]+\w*/

// eg: sun

[a-z]+ – means a to z can occur one or more times

\w* – means any word including numbers can occur zero or more time

def TLD = /[a-z]{2,4}/

//eg: com, us, info

[a-z]{2,4} – means a to z can occur 2 to 4 times (ie can be “us” or “com” or “info”)

Now putting it all together

def emailRegex = /[a-z]+\d*[.]?\w*[@][a-z]+\w*[a-z]{2,4}/

or even better

def emailRegex = /$FIRSTNAME$DOT$SECONDNAME$AT$DOMAIN$DOT$TLD/

If you want to validate an email in groovy, following is the code

   1: email="james.gosling@sun.com"
   2:
   3: FIRSTNAME = /[a-z]+\d*/
   4: DOT = /[.]?/
   5: SECONDNAME = /\w*/
   6: AT = /[@]/
   7: DOMAIN= /[a-z]+\w*/
   8: TLD=/[a-z]{2,4}/
   9:
  10: print (email ==~ /$FIRSTNAME$DOT$SECONDNAME$AT$DOMAIN$DOT$TLD/ )

==~ – means look for the matching pattern

Try validating with other email values and try optimizing your program by bringing in more constraints.

Disclaimer: This program by no means is complete and cannot be used for a full fledged email validation, please google ‘regex’ for better regex patterns.

Posted in groovy, regex | Leave a Comment »

Groovy – operator overriding

Posted by Prabhu Beeman on July 18, 2008

What do you think will the world without Google will be?

Let us try to represent this using a groovy code and in the process know more about the groovy language.

   1: class World {

   2:     String result = "World";

   3:

   4:     World(){}

   5:

   6:     World minus(Company company) {

   7:         if(company.getName() == "Google") {

   8:             result = "No Search!!!No AdSense!!!"

   9:         }

  10:         else {

  11:             result = "Don't Know"

  12:         }

  13:         return this;

  14:     }

  15:

  16:     World minus(String company) {

  17:         minus(new Company(company))

  18:     }

  19:     // This method is called when we try to print the object

  20:     String toString() {

  21:         return result;

  22:     }

  23: }

  24:

  25: class Company {

  26:     String name

  27:     Company(String name) {

  28:         this.name = name

  29:     }

  30: }

  31:

Let me explain the code here

We do not have any more semicolons (optional)

Line 6: We have overridden the ‘-’ (minus) operator here, I will explain this later

Line 7: Now where was getName() defined, well getName() and setName(…) method is created for you the moment you create the property (Line 26)

Line 16: That is operator overloading

Alright so here is the final piece of code, let us go ahead and look at it…

   1: def ourWorld = new World()

   2: def google = new Company("Google")

   3: ourWorld - google

Line 1: This is dynamic typing, the compiler takes care of the object type it should be converting to.

We create a world instance and a Company instance.

So when we remove google from ourWorld(Line 4)

ourWorld – google

What do we get??

Result: No Search!!!No Adsense!!!

Even better with the following code

   1: def ourWorld = new World()

   2: ourWorld – “Google”

Isn’t this amazing. You just subtract Google from ourWorld and you see the result, couldn’t depict better than this.

Posted in agile, groovy | Tagged: , | Leave a Comment »

NONOH – NO Security

Posted by Prabhu Beeman on July 8, 2008

I stay away from India and had to use some cheap voip service to call back home.

I used nonoh, pretty cheap so you have to compromise on the quality.

Well, i realized very recently that i need to compromise even on the security aspect of it.

When I login to the application they are passing the username and password as part of the url (a GET request). Well, the username and password is encrypted. But it didn’t take me more than 10 secs to know that they use ROT13. Probably might help you protect from lame users but not from the ever watching nerds.

Posted in agile | Leave a Comment »

Modal Box Plugin for Grails

Posted by Prabhu Beeman on July 6, 2008

[Update: This issue is now fixed in the latest version of the plugin. Thanks for the fix Alex.]
Plugin url: http://www.grails.org/ModalBox+Plugin

I tried to implement the Modal Box Plugin in my application and had difficulties in getting it done.The instruction given in the above site looked straight forward. But then after following the instruction all I get is a blank page..

looked into the generated html (right click and view source)

following was the header snippet (<modalbox:modalIncludes/> generates the following code)

<script type=’text/javascript’ src=’/myapp/plugins/modalbox-0.1/js/modalbox/prototype.js’ />
<script type=’text/javascript’ src=’/myapp/plugins/modalbox-0.1/js/modalbox/scriptaculous.js?load=effects’ />
<script type=’text/javascript’ src=’/myapp/plugins/modalbox-0.1/js/modalbox/modalbox.js’ />
<link rel=’stylesheet’ href=’/myapp/plugins/modalbox-0.1/css/modalbox.css’ />

Effectively removing the <modalbox:modalIncludes/> from the jsp gsp renders the page correctly
So time to scrutinize the above 4 lines..
The problem was actually with the empty close tag.

So ideally
<script type=’text/javascript’ src=’/myapp/plugins/modalbox-0.1/js/modalbox/prototype.js’ />

should be

<script type=’text/javascript’ src=’/myapp/plugins/modalbox-0.1/js/modalbox/prototype.js’ ></script>
and so is the rest.
bingo.. everything works great now.

So how do i make this change in the plugin?

Go to your plugins directory -> modalbox-0.1->grails-app->taglib->ModalboxTaglib

then make your changes inside def modalIncludes

Its done…I’m a happy soul now :-)

Posted in grails, plugin | 1 Comment »