jump to article
...intermittent thoughts

some more @Functions for XPages (@URLEncode(), @URLDecode())

As mentioned earlier, a couple of @Functions are missing in XPages serverside JavaScript. When I was a bit confused by that before I am now in a state of simply doing it another way without even realizing an @Function is missing - probably a reason why some of them are not available. Another reason is how easy it sometimes is to achieve the same by incorporating native Java inside serverside JavaScript.

Since I just showed how to incorporate native Java inside JavaScript with XPages in my XPages session here at DNUG, I thought I should post the code for both sharing two other extensions to the already available @WebDbname() function and for showing how easy one can leverage native Java code inside his serverside JavaScript.

So this is the first example - @URLEncode() in JavaScript:

/** ****************************************************************************
* @URLEncode()
* provides closely the same functionality its @Formula pendant, that is it
* encodes an object to a URL encoded format
*
* @param encodeObject the Object to encode. The Objects toString() method is
*                 used to retrieve a String to encode!
* @param encSch optional encoding scheme to use
* @see java.net.URLEncoder
*         (http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLEncoder.html)
* @see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/package-summary.html
* @returns URL encoded version of encodeObject or null in case of any error
* @author Michael Gollmick
* @version 1.0
* @date 20090509
* @depends java.net.URLEncoder
**************************************************************************** **/
function @URLEncode(encodeObject, encSch:String) {
        try {
                var encScheme = ((encSch) && (encSch !== null))?encSch:"UTF-8";
                return java.net.URLEncoder.encode(encodeObject.toString(),
                        encScheme);
        } catch (e) {
                print("ERROR in @URLEncode:" + e);
        }
        return null;
}


To complete the encoding experience, we need the function to retranslate encodings - @URLDecode() in JavaScript:

/** ****************************************************************************
* @URLDecode()
* provides closely the same functionality its @Formula pendant, that is it
* Decodes a URL Encoded string to normal format
*
* @param strToDecode the String to decode
* @param encodeObject optional encoding scheme to use
* @see java.net.URLDecoder
*         (http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLDecoder.html)
* @see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/package-summary.html
* @returns decoded version of strToDecode or null in case of any error
* @author Michael Gollmick
* @version 1.0
* @date 20090509
* @depends java.net.URLDecoder
**************************************************************************** **/
function @URLDecode(strToDecode:String, encSch:String) {
        try {
                var encScheme = ((encSch) && (encSch !== null))?encSch:"UTF-8";
                return java.net.URLDecoder.decode(strToDecode, encScheme);
        } catch (e) {
                print("ERROR in @URLDecode:" + e);
        }
        return null;
}


As you easily see - the magic is done in both cases in only one line, invoking a native static final Java method. So that would make it easy to use only that lines. Of course the whole functions do have some more functionality, so it is probably not the worst idea to use them ;-)

Big Apple in Naumburg

Just got an invitation to a exhibit opening on Friday. Unfortunately I cannot go. But I know it is going to be very good - and the exhibit will last some days. The city of Naumburg is showing some pictures of New York, my friend Andreas Klingebiel shot back in 2000, when he was there for an internship. That pictures were shown several times before, for instance in the City of Cologne and in Leipzigs main train station. Quite impressive!
Picture is © 2000 by Andreas Klingebiel and must not be redistributed!

If you visit Naumburg until September this year, you should probably have a look at these pictures in the city library:

Stadtbibliothek Naumburg, Jägerstr. 4, D-06618 Naumburg

The exhibit is free of charge.