Hi Dan,

> Is there an easy way to parse those variables similar to
> Window.Location.getParameter ?
> Window.Location.getHash returns the whole string.

we use a dedicated HistoryToken class to transparently parse and
manage history tokens. The constructor takes a history token string,
which in our case is supposed to contain a path followed by any number
of URL-style parameters (e.g. index.html#subpath_1/.../subpath_n?
param_1=value_1&...&param_m=value_m). Excluding getters and setters
(which always return the object so you can concatenate like this:
token = new HistoryToken("myPlace").addToken("mySubPlace").addParam
("param1", "value1") :)) the relevant portion of the class looks like
follows:

public class HistoryToken {

        protected String[] subTokens;
        protected Map<String, String> parameterMap;
        protected String token;

        public HistoryToken(String token) {

                this.token = token;
                parameterMap = new HashMap<String, String>();

                if(token == null || token.isEmpty()) {
                        subTokens = new String[]{};
                        return;
                }
                String[] split = token.split("\\?");
                subTokens = split[0].split("/");
                if(split.length > 1) {
                        split = split[1].split("&");
                        for(int i=0; i<split.length; i++) {
                                String[] params = split[i].split("=");
                                parameterMap.put(URL.decodeComponent(params[0]),
URL.decodeComponent(params[1]));
                        }
                }
        }

        public HistoryToken() {

                this.token = null;
                parameterMap = new HashMap<String, String>();
                        subTokens = new String[]{};

        }


        public void buildToken() {
                token = "";
                for (int i = 0; i < subTokens.length; i++) {
                        token += subTokens[i];
                        if(i<subTokens.length-1) token += "/";
                }

                if(parameterMap.size() > 0) token += "?";

                for (Iterator i = parameterMap.entrySet().iterator(); 
i.hasNext();)
{
                        Map.Entry<String, String> param = (Map.Entry<String, 
String>) i.next
();
                        token += 
URL.encodeComponent(param.getKey())+"="+URL.encodeComponent
(param.getValue());
                        if(i.hasNext()) token += "&";
                }

        }

/*  ... ... ... */

}

This approach has thus far worked pretty well for our product :)

Cheers, Jonas

--

You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.


Reply via email to