Wednesday, April 22, 2009

XPages: Workarounds for lack of Regular Expression functionality

I wrote these two String methods as workarounds for the previously mentioned missing Regular Expression functionality.

// Finds all occurences of a substring in a string using a regular expression
// Similar to real JavaScript RegExp match with 'g' modifier
String.prototype.matchAll = function( regExp ){
// The input String
var string = this;

// Find all matches/add to matches-array
var matches = [];
while( string.match( regExp ) ){
matches.push( string.match( regExp )[0] );
string = string.replace( regExp, '' );
return matches;

// Replace all occurences of a substring using a function
// Similar to real JavaScript, where you can pass a function as the second argument
String.prototype.replaceAllFn = function( regExp, fn ){
// The input String
var string = this;

// Array used to find matches withing the string
var matchArray = null;
// JavaScript equivalent keeps track of the position of the substrings
var position = null;

while( matchArray = string.match( regExp ) ){
position = regExp );

// Replace substring(s) in the string using the supplied function
// Input parameters to the supplied function: all matches withing the
// string, the position of the match, the full string
string = string.replace( matchArray[0],
fn.apply( this, matchArray.concat( position, string ) ) );
return string;


// Find all substrings withing braces
'{lastName}, {firstName}'.matchAll( /\{(\w+)\}/g );
// Result, array with two items: ["{lastName}", "{firstName}"]

// Map object properties to a string template
var object = { firstName: 'Tommy', lastName: 'Valand' };
var template = '{lastName}, {firstName}';
template.replaceAllFn( /\{(\w+)\}/, function( item, key ){
return object[key] || '';
// Result: "Valand, Tommy"

// Convert CSS to camelCase
"-moz-border-radius".replaceAllFn(/-\w/g, function( string ){
return string.replace('-','').toUpperCase();
// Result: MozBorderRadius

// Calculate celcius from fahrenheit
"212F".replaceAllFn(/(\d+(?:\.\d*)?)F\b/g, function(str, p1, offset, s){
return ((p1-32) * 5/9) + "C";
//Result: 100C

As others have mentioned, closures seems to work in XPages' pseudo-JavaScript.

function testWithClosures(){
var object = { firstName: 'Tommy', lastName: 'Valand' };
var template = '{lastName}, {firstName}';
return template.replaceAllFn( /\{(\w+)\}/, function( item, key ){
return object[key] || '';

// Result: "Valand, Tommy"

If closures weren't supported, replaceAll would crash, since the object is unavailable (undefined) in the global scope. object is only available in the scope that the "replace-function" was created.

Also, if you didn't already notice it, you can extend native objects as you can with the native objects in the web browsers. Thread carefully though.. :)

If you find any bugs in regards to the JavaScript methods being emulated, let me know.

There is (at least) one serious bug with my replaceAllFn method.
'test'.replaceAllFn( /t/, function(){ return 't'; } results in an eternal loop.
I'll try to find a better way.

Wednesday, April 8, 2009

Hotmail POP3-/SMTP access (free)

I'm not sure if it's opened up free POP3-/SMTP-service for all countries, but it seems to work in Norway. Webmail was down/I needed to reply to a reader in need, so I had to find another way to get/send my mail.

MSN Hotmail and Hotmail POP3/SMTP Access.

Friday, April 3, 2009

Forcing charset on content on the web

I hit a snag today. I needed to use the ?OpenField-command in an Ajax application to fetch utf-8 encoded content. The content seemed to be sent as ISO-8859-1. I believe the browser read it as UTF-8, which resulted in all the norwegian characters not showing correctly.

The Domino team already thought about this way back in N/D 5.02 (according to the help-file). Back then, they added a charset parameter that tells the server which charset it should send the content in. The charset parameter seems to work more or less in any context.


Thursday, April 2, 2009

Opening EPS-files in GIMP

I had to convert an EPS-file to GIF today. This isn't possible out of the box with GIMP, which is my weapon of choice when working with images.

This guide made it possible.

Wednesday, April 1, 2009

Updated the TextToPNG-generator

Update 14.04.09: I added support for PNG transparency, and improved the vertical positioning of text to avoid clipping.

When I first made it, i called it a header-generator.

I actually needed something similar today at work, for a newsletter-application I'm working on. I added some functionality at work, and planned to release the updated version as is. I saw how horrendous the interface looked/wished for a little more functionality, so I added a little more functionality/freshened up the interface.

Old interface:

New interface:

>> Download DemoApp
Requirements to run the app: Domino 8.x (code written in Java 5)

Example images:

Question: Can XPages only be used for HTML?

The reason I'm asking is that the html/js/css code generated by your average XPage is too bloated for my taste.

I love the server side scripting/speed/pseudo-relational lookups part of the technology though.

My question: Is it possible to use XPages to generate other content than HTML, like (valid) XML and JSON, and how?

I've tried my friend Google, but he found nothing for me.