public abstract class AbstractBasePlace extends Place {

private static final Logger logger = 
Logger.getLogger(AbstractBasePlace.class.getName());

private static final String VALIDATE_TOKENS_MESSAGE_PREFIX = "Missing 
token(s): ";

private String url;
private Map<String, String> params = new HashMap<String, String>();

/**
 * 
 * @param token
 *            will be parsed to extract the parameters passed as part of url
 */
protected AbstractBasePlace(String... tokens) {

if (tokens.length % 2 != 0) {
logger.warning("Invalid arguments received!"
+ " Must be name,value,name,value ... Ignoring arguments!");
return;
}

StringBuilder stringBuilder = new StringBuilder();

for (int i = 0; i < tokens.length; i++) {

final String paramName = tokens[i++];
final String paramValue = tokens[i];

// build params map
params.put(paramName, paramValue);

// build url
stringBuilder.append(paramName + "=" + paramValue + "&");
}

url = stringBuilder.toString();
}

protected AbstractBasePlace(String url) {

if (url == null || url.isEmpty()) {
logger.warning("Url empty, no-op");
return;
}

this.url = url;

List<String> list = Arrays.asList(url.split("&"));

if (list == null || list.size() < 1) {
logger.warning("Token empty, no-op");
return;
}

for (String listItem : list) {

List<String> nvPair = Arrays.asList(listItem.split("="));

if (nvPair == null || nvPair.size() != 2) {
logger.warning("Invalid paramters");
continue;
}

params.put(nvPair.get(0), nvPair.get(1));
}
}

/**
 * Checks whether 'this' is constructed with the required parameters. The
 * list of required params must be passed as argument.
 * 
 * @param requiredLiterals
 */
protected void validate(String... paramNames) {

StringBuffer message = new StringBuffer(VALIDATE_TOKENS_MESSAGE_PREFIX);

for (String name : paramNames) {
if (!params.containsKey(name))
message.append(name + " ");
}

// if something is added to the string show
if (!message.toString().equals(VALIDATE_TOKENS_MESSAGE_PREFIX)) {
logger.warning(message.toString());
// Status.failure(message.toString());
}
}

public String getUrl() {
return url;
}

public String getParameter(String name) {
return params.get(name);
}

public boolean hasParameter(String name) {
return params.containsKey(name);
}
}


All your places can extend this class and you can use 2 constructors 
depending upon the context:

   1. Use this if you are constructing the place, to be used with 
   placecontroller.
   2. Use this for constructing place within PlaceTokenizer.

And when ever you want a param, just call place.hasParam() and 
place.getParam()

Hope this helps.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-web-toolkit/-/ZlJGC2wqNyAJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to