Reviewers: ,
Description:
Added a WithDelimiter Annotation to allow for custom delimiter for use
with the AbstractPlaceHistoryMapper.
Example useage:
@WithTokenizers(...)
@WithDelimiter("/")
public interface AppPlaceHistoryMapper extends PlaceHistoryMapper {}
http://code.google.com/p/google-web-toolkit/issues/detail?id=5899
Supports multiple character delimiter.
@WithDelimiter("::")
Please review this at http://gwt-code-reviews.appspot.com/1710803/
Affected files:
place/impl/AbstractPlaceHistoryMapper.java
place/rebind/PlaceHistoryGeneratorContext.java
place/rebind/PlaceHistoryMapperGenerator.java
place/shared/WithDelimiter.java
Index: place/impl/AbstractPlaceHistoryMapper.java
===================================================================
--- place/impl/AbstractPlaceHistoryMapper.java (revision 10971)
+++ place/impl/AbstractPlaceHistoryMapper.java (working copy)
@@ -31,31 +31,31 @@
* Return value for
* {@link AbstractPlaceHistoryMapper#getPrefixAndToken(Place)}.
*/
- public static class PrefixAndToken {
+ public class PrefixAndToken {
public final String prefix;
public final String token;
public PrefixAndToken(String prefix, String token) {
- assert prefix != null && !prefix.contains(":");
+ assert prefix != null && !prefix.contains(getDelimiter());
this.prefix = prefix;
this.token = token;
}
@Override
public String toString() {
- return (prefix.length() == 0) ? token : prefix + ":" + token;
+ return (prefix.length() == 0) ? token : prefix + getDelimiter() +
token;
}
}
protected F factory;
public Place getPlace(String token) {
- int colonAt = token.indexOf(':');
+ int colonAt = token.indexOf(getDelimiter());
String initial;
String rest;
if (colonAt >= 0) {
initial = token.substring(0, colonAt);
- rest = token.substring(colonAt + 1);
+ rest = token.substring(colonAt + getDelimiter().length());
} else {
initial = "";
rest = token;
@@ -79,6 +79,8 @@
this.factory = factory;
}
+ protected abstract String getDelimiter();
+
/**
* @param newPlace what needs tokenizing
* @return the token, or null
Index: place/rebind/PlaceHistoryGeneratorContext.java
===================================================================
--- place/rebind/PlaceHistoryGeneratorContext.java (revision 10971)
+++ place/rebind/PlaceHistoryGeneratorContext.java (working copy)
@@ -25,6 +25,7 @@
import com.google.gwt.place.shared.PlaceHistoryMapperWithFactory;
import com.google.gwt.place.shared.PlaceTokenizer;
import com.google.gwt.place.shared.Prefix;
+import com.google.gwt.place.shared.WithDelimiter;
import com.google.gwt.place.shared.WithTokenizers;
import java.util.ArrayList;
@@ -76,9 +77,11 @@
String implName = interfaceType.getName().replace(".", "_") + "Impl";
+ String delimiter = addDelimiter(interfaceType);
+
return new PlaceHistoryGeneratorContext(logger, typeOracle,
interfaceType,
factoryType, stringType, placeTokenizerType,
- interfaceType.getPackage().getName(), implName);
+ interfaceType.getPackage().getName(), implName, delimiter);
}
private static JClassType findFactoryType(
@@ -118,6 +121,8 @@
final String packageName;
+ final String delimiter;
+
/**
* All tokenizers, either as a {@link JMethod} for factory getters or as
a
* {@link JClassType} for types that must be GWT.create()d, by prefix.
@@ -134,7 +139,7 @@
PlaceHistoryGeneratorContext(TreeLogger logger, TypeOracle typeOracle,
JClassType interfaceType, JClassType factoryType, JClassType
stringType,
- JClassType placeTokenizerType, String packageName, String implName) {
+ JClassType placeTokenizerType, String packageName, String implName,
String delimiter) {
this.logger = logger;
this.typeOracle = typeOracle;
this.interfaceType = interfaceType;
@@ -143,6 +148,7 @@
this.placeTokenizerType = placeTokenizerType;
this.packageName = packageName;
this.implName = implName;
+ this.delimiter = delimiter;
}
public Set<JClassType> getPlaceTypes() throws UnableToCompleteException {
@@ -190,6 +196,15 @@
}
}
+ private static String addDelimiter(JClassType interfaceType) {
+
+ WithDelimiter annotation =
interfaceType.getAnnotation(WithDelimiter.class);
+ if (annotation == null) {
+ return ":";
+ }
+ return annotation.value();
+ }
+
private void addPlaceTokenizer(Object tokenizerClassOrGetter, String
prefix,
JClassType tokenizerType) throws UnableToCompleteException {
if (prefix.contains(":")) {
@@ -220,6 +235,10 @@
placeTypes.put(placeType, prefix);
}
+ public String getDelimiter() {
+ return delimiter;
+ }
+
private String getLogMessage(Object methodOrClass) {
if (methodOrClass instanceof JMethod) {
JMethod method = (JMethod) methodOrClass;
@@ -343,4 +362,4 @@
}
return rtn;
}
-}
\ No newline at end of file
+}
Index: place/rebind/PlaceHistoryMapperGenerator.java
===================================================================
--- place/rebind/PlaceHistoryMapperGenerator.java (revision 10971)
+++ place/rebind/PlaceHistoryMapperGenerator.java (working copy)
@@ -90,6 +90,9 @@
SourceWriter sw = f.createSourceWriter(generatorContext, out);
sw.println();
+ writeGetDelimiter(context, sw);
+ sw.println();
+
writeGetPrefixAndToken(context, sw);
sw.println();
@@ -101,6 +104,15 @@
generatorContext.commit(logger, out);
}
+ private void writeGetDelimiter(PlaceHistoryGeneratorContext context,
+ SourceWriter sw) throws UnableToCompleteException {
+ sw.println("public String getDelimiter() {");
+ sw.indent();
+ sw.println("return \"" + context.getDelimiter() + "\";");
+ sw.outdent();
+ sw.println("}");
+ }
+
private void writeGetPrefixAndToken(PlaceHistoryGeneratorContext context,
SourceWriter sw) throws UnableToCompleteException {
sw.println("protected PrefixAndToken getPrefixAndToken(Place newPlace)
{");
Index: place/shared/WithDelimiter.java
===================================================================
--- place/shared/WithDelimiter.java (revision 0)
+++ place/shared/WithDelimiter.java (revision 0)
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+ * use this file except in compliance with the License. You may obtain a
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+package com.google.gwt.place.shared;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates {@link PlaceTokenizer} types used by an implementation of
+ * {@link PlaceHistoryMapper} generated by
+ * {@code com.google.gwt.place.rebind.PlaceHistoryMapperGenerator}.
+ */
+@Target({ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface WithDelimiter {
+ String value() default ":";
+}
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors