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 ;-)

  1. 1) Randal Oulton said: (15.10.2010 23:30:05 GMT)
    Where did you store these functions?

    Where did you store these functions?

    Cheers!

  2. 2) Michael Gollmick Said: (20.10.2010 22:29:32 GMT)

    re: Where did you store these functions?

    These functions can be stored inside any of the interfaces that are prvided to you by XPages. But to be honest that would be cumbersome to use. So best advice is to use these functions from within a shared Script Library that is used as a resource on your XPage or Custom Control. If you add the Library as resource, the Functions are available to all parts of the CC or XP that loads the library.

  3. 3) Jens Winkelmann said: (13.05.2011 15:27:41 GMT)
    Thanks - it works

    That saved me time

  4. 4) Ray-Ban Sunglasses said: (28.12.2015 22:03:48 GMT)
    Ray-Ban Sunglasses

    { Link }

  5. 5) neha mehta actress hot said: (18.06.2016 13:30:23 GMT)
    neha mehta actress hot

    very nice.

    { Link }

  6. 6) cialis said: (26.09.2016 15:13:26 GMT)
    buy cialis on line

    Angus Cipla { Link } , price sildenafil one way or another for without prescription overnight uk for.

  7. 7) vagra_sale said: (01.12.2016 9:41:42 GMT)
    vagra sale

    { Link } , For Women Herbal in Odessa .

  8. 8) herbal_viagra said: (10.01.2017 21:34:12 GMT)
    herbal viagra

    Training: there there there there is is is is ordering { Link } , online safe doctor begging for months.

  9. 9) advance said: (05.05.2017 1:22:16 GMT)
    cash advance loans in virginia

    SPECIAL OFFER : { Link } , For Women Herbal on SALE , Buy Viagra For Women Herbal shop online .


Add Comment
 
Subject:
   
Name:
E-mail:
Web Site:
 
Comment:  (No HTML - Links will be converted if prefixed http://)
 
Remember Me?