Java Custom addSlashes()

October 1, 2010 at 06:56 (java, javascript, tutorial, web dev)

Java regexes can get pretty clunky due to the dual use of the backslash character, as is commonly known. Although there’s not a lot to it, below is some code to add slashes for these characters: backslash, line feed, carriage return, null, single quote. It is used in a web application to format JSON strings on the server before passing to the client, where the objects are then processed by JavaScript/Ext JS. Maybe someone will find it useful!

public class StringUtil {
    public static String addSlashes(String s) {
        s = s.replaceAll("\\\\", "\\\\\\\\");
        s = s.replaceAll("\\n", "\\\\n");
        s = s.replaceAll("\\r", "\\\\r");
        s = s.replaceAll("\\00", "\\\\0");
        s = s.replaceAll("'", "\\\\'");
        return s;
    }
}

Incidentally, there are other places where the following JavaScript replacement is necessary:

s = s.replace(/"/g, '"');

Permalink Leave a Comment

Undocumented Ext JS PagingToolbar Method

August 22, 2010 at 07:01 (extjs, javascript)

Background: I’ve mentioned Ext JS in a previous post and am still using version 3.2.1.

After another fun-filled trip through ext-all-debug.js, I’ve discovered a simple yet hidden way to load an arbitrary page number in a grid that uses a PagingToolbar (Ext.PagingToolbar). Simply call yourGridPanel.getBottomToolbar().doLoad(start), where start is the row index starting from 0. So for example if you set pageSize = 10, then to display the first page call doLoad(0), second page call doLoad(10), etc. Note that when choosing a non-multiple of pageSize, the page number gets rounded up, so for example doLoad(1) would display items 2-11 (if you have 11 or more items) and report the page number as 2.

And of course, as the title of this post implies, this method is absent from the official API documentation (as of the time of this post) despite its being a public and very useful method.

Permalink Leave a Comment

Undocumented Ext JS Grouping Sort Property

July 22, 2010 at 04:28 (extjs, javascript)

Background: Ext JS is an open source JavaScript library used for web design and geared towards interactive apps using technology such as Ajax. I am using version 3.2.1.

Today after an hour or two of frustration, I discovered an omission in the official online documentation of Ext JS. Under Ext.data.GroupingStore, the very useful property groupDir is mentioned nowhere, as of the time of this post. Without this property, it is exceedingly difficult (in my experience) to specify a simple sorting preference on the grouping field for this type of grid.

Read the rest of this entry »

Permalink 2 Comments