Replace deprecated DOM.setElementAttribute().
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / place / HasDocumentPlace.java
1 /*
2  * Copyright (C) 2012 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.client.place;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.glom.web.client.StringUtils;
26
27 import com.google.gwt.place.shared.Place;
28
29 /**
30  * @author Murray Cumming <murrayc@murrayc.com>
31  *
32  */
33 public abstract class HasDocumentPlace extends Place {
34
35         protected final String documentID;
36
37         /**
38          * 
39          */
40         public HasDocumentPlace(final String documentID) {
41                 super();
42                 this.documentID = documentID;
43         }
44         
45         public String getDocumentID() {
46                 return documentID;
47         }
48         
49         public static class Tokenizer {
50                 protected final String documentKey = "document";
51                 private final String equals = "=";
52                 private final String separator = "&";
53                 
54                 /**
55                  * Get a HashMap of parameter names and values from the history token. This can parse tokens built by
56                  * buildParamsToken().
57                  * 
58                  * @param historyToken
59                  *            The historyToken provided to getPlace().
60                  * @return A HasMap of names to values.
61                  */
62                 protected HashMap<String, String> getTokenParams(final String historyToken) {
63                         final String[] arStr = historyToken.substring(0, historyToken.length()).split(separator);
64                         final HashMap<String, String> params = new HashMap<String, String>();
65                         for (final String element : arStr) {
66                                 final String[] substr = element.split(equals);
67                                 if (substr.length != 2) {
68                                         continue;
69                                 }
70
71                                 String key = "";
72                                 String value = "";
73                                 if (!StringUtils.isEmpty(substr[0])) {
74                                         key = substr[0];
75                                 }
76
77                                 if (!StringUtils.isEmpty(substr[1])) {
78                                         value = substr[1];
79                                 }
80
81                                 if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)) {
82                                         params.put(key, value);
83                                 }
84                         }
85
86                         return params;
87                 }
88
89                 /**
90                  * Build a history token based on a HashMap of parameter names and values. This can later be parsed by
91                  * getTokenParams().
92                  * 
93                  * @param params
94                  *            A HashMap of names and values.
95                  * @return A history string for use by getToken() implementation.
96                  */
97                 protected String buildParamsToken(final HashMap<String, String> params) {
98                         String token = "";
99                         for (final Map.Entry<String, String> entry : params.entrySet()) {
100                                 final String key = entry.getKey();
101                                 final String value = entry.getValue();
102                                 if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) {
103                                         continue;
104                                 }
105
106                                 if (token != "") {
107                                         token += separator;
108                                 }
109
110                                 token += key + equals + value;
111                         }
112
113                         return token;
114                 }
115         }
116
117 }