If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Wednesday, April 28, 2010

Object Oriented Javascript

Nowadays you'll find many ways to program Object Oriented in javascript. But if you'd like to do it without depending on some third-party javascript library below you'll find an example.




Now you can create a new Lion object which inherits all properties from the function (class) it extends.

Friday, April 23, 2010

xslt1.0 challenge

Someone on the xslt mailing list wanted to build a nice html table based on searchresults but the problem was not all searchresults had the same data.

I decided to give it a go and actually made the challenge a bit more difficult ;-)

Input:


Expected:


As you can see the tableheaders are sorted and the datarows should of course match up with the corresponding headers.

Thursday, April 8, 2010

Miljoenenjacht

Today I heard about a game called 'miljoenenjacht' on the radio. You basically have to answer some questions online which will gradually reveal the ZIP code where the Nationale Postcode loterij has hidden a suitcase full of money.

Anyway... I thought of checking it out and i answered the first question correctly. This revealed 2 numbers of the zip code pattern "[0-9][0-9][0-9][0-9] [A-Z][A-Z]".

Since i didn't have any idea what postcodes are available in the Netherlands I downloaded some CSV files from the internet which contained following information
- Zip code
- City Name
- Street Name

To be able to query those files by a specific zip code pattern I wrote following diry hack :-)







The output of running the main method:

The pattern resulted in 1319 hits.
7908 AA | Hoogeveen | Mortonhof
7908 AB | Hoogeveen | De Weide
7908 AC | Hoogeveen | Zuidwoldigerweg
...

Tuesday, April 6, 2010

When enums are not an option

I had to write some code for JDK1.4 which does not support enums. Below you will find some way to achieve enumeration like functionality.

The first thing I did was creating an empty marker interface.


Next I wrote a DataTypeFactory which exposes final DataTypes.

Monday, March 29, 2010

Scala traits

Let us first step back and look into Java interfaces. A Java interface typically is defined as a thin interface, meaning it only describes the minimal required methods to get the work done. The main reason is that the more methods an interface defines, the heavier the burden becomes for the implementer of that interface.

The real issue though is that in Java an interface itself can't provide any concrete implementation.

Let me explain this by a concrete example.



You should agree that isLast() could easily be derived using hasNext().

In java we cannot put actual implementation code in the interface all concrete classes implementing this interface need to do so or extend from a abstract class which implements isLast().

Scala traits are comparable to java interfaces. They describe methods which an object should implement but they can provide concrete implementations themselves.


So in scala the best practice is to write only a few ABSTRACT methods and enrich the interface with a lot of concrete methods implemented based upon these abstract methods.

Like a Java interface a Scala trait can also have NO constructor parameters. In Java a class can implement multiple interfaces using the 'implements' keyword. An interface itself can extend 1 other interfaces using the 'extends' keyword.

In Scala you can extend a Trait using the 'extends' keyword. This would mean you also implicitly inherit from the trait's superclass. But you could also mix-in the trait using the 'with' keyword.

A nice example of implementing a very thin abstract interface (toXML and fromXML) extended with concrete rich interface methods is shown below.






The output on my console:


Another big advantage is that traits are stackable through linearization used by the scala compiler.

... TODO: workout some examples

Friday, March 26, 2010

the beauty of control abstractions in SCALA - part 3

This time i decided to test out SCALA's xml API... again it's pretty impressive how easy it is to save or load xml.

For testing purpose i made modifications to 'FileComponent.scala' and 'TreeComponent.scala'. I added 2 methods:
- toXML
- saveAsXML

TreeComponent.scala



FileComponent.scala


FileComponentTest.scala



Output: d:/tmp/filetree.xml

SyntaxHighlighter

Yesterday my blog was unusable during the whole day due to the fact that i'm using hosted files for formatting my code snippets. I really like syntaxhighlighter a lot since it allows me to copy and paste directly in this editor without having to escape ampersands, greather then or less then,....

Anyway... it seems like Alex Gorbatchev made a switch from hosting the source files on a private server to the amazon cloud. So the cloud should in theory be able to upscale and downscale when the request load increases dramatically. Just to be sure however i downloaded a private copy and uploaded them to my google docs account which has 1gigabyte of room left... Next i had to share all individual files and generate the links. But what i actually wanted to share is that if you have a blog and you want to host custom javascript/css files instead of completely polluting your blog master template...this is one way to do it.

Cheers,
Robby Pelssers

Thursday, March 25, 2010

the beauty of control abstractions in SCALA - part 2

Ok... after i got my first sample working i was keen on further abstracting the code.

Thanks to excellent input from Luc Duponcheel and Tony Morris the following code shows how you can further abstract the previous article with the help of implicits.


TraversableComponent.scala:


FileComponent.scala


FileComponentTest.scala


Now the only thing missing is a predicate which will be used to determine if the file should be printed or not....

some use case...
if file-extension is .xml
if filename contains "test"


To be continued....

the beauty of control abstractions in SCALA

I still think it's hard to get a good grip on everything SCALA has to offer but the more I experiment with SCALA, the more excited i get.

Just for fun I wrote a little standalone object named FileUtils. It has 3 methods which respectively print recursively all files/directories, only files or only directories.

If you start coding conventionally you might end up with something like this:



But if you watch closely the methods are practically the same except for the check which is done before i print the file to the console... And because of this i have to call the same method recursively.

It took me a while to understand how control abstractions work but here is the refactored version.




Think of how this will influence your life as a developer... no more unnecessary code duplications because of some minor differences.

Cheers,
Robby

Thursday, March 18, 2010

Creating dynamic watermarks with SVG

My customer wanted to show a watermark which should be dynamically created based on the data (xml) which was processed with xslt. The end result is a nicely styled html page. (see image below).


In order to achieve this result I automatically pulled out my SVG knowledge which would allow me to create images dynamically. Since not all browsers are able to render SVG i used Cocoon's batik block to serialize the svg image to png.

I'll shortly show that this can be easily accomplished with Apache Cocoon 2.2.

First make sure you add following dependencies to your block:




Next in line you will need to create some SVG template for your watermark:


A next step is writing some flowscript function which sets the jx transformer context:


We also need to make some sitemap adjustments:



And finally i need to add the background image dynamically based on content within my stylesheets:



And to finalize the positioning etc i made some css adjustments: