Permalink
Browse files

Implemented generating routes.

  • Loading branch information...
1 parent 0665d9a commit a9c598e683464c7f57ea01ca6a6cf648aa4ceb2e @kawasima committed Jul 23, 2012
View
@@ -92,6 +92,12 @@
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-jsp_2.0_spec</artifactId>
+ <version>1.1</version>
+ <optional>true</optional>
+ </dependency>
</dependencies>
<groupId>net.unit8.sastruts</groupId>
<artifactId>sastruts-advanced-routes</artifactId>
@@ -116,21 +116,24 @@ public boolean matchesControllerAndAction(String controller, String action) {
}
public String generate(Options options, Options hash) {
- if (generationRequirements()) {
- for (Segment segment : segments) {
- if (segment.hasKey()) {
-
- }
- }
+ String path = null;
+ if (generationRequirements(hash)) {
+ path = segments.get(segments.size() - 1).stringStructure(segments.subList(0, segments.size() - 1), hash);
}
- return null;
+ return path;
}
- public boolean generationRequirements() {
+ public boolean generationRequirements(Options hash) {
+ boolean matched = true;
for(String key : requirements.keySet()) {
+ Object req = requirements.get(key);
+ if (req instanceof Pattern) {
+ } else {
+ matched &= StringUtil.equals(hash.getString(key), requirements.getString(key));
+ }
}
- return false;
+ return matched;
}
private String requirementFor(String key) {
if (requirements.containsKey(key))
@@ -1,7 +1,6 @@
package net.unit8.sastruts.routing;
-import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -14,6 +13,10 @@
private boolean isOptional;
public Segment() {
+ this(null);
+ }
+ public Segment(String value) {
+ this.value = value;
isOptional = false;
}
@@ -25,33 +28,36 @@ public String getExtractionCode() {
return null;
}
- public String continueStringStructure(LinkedList<Segment> priorSegments) {
- if (priorSegments.isEmpty()) {
- return interpolationStatement(priorSegments);
+ public String continueStringStructure(List<Segment> list, Options hash) {
+ if (list.isEmpty()) {
+ return interpolationStatement(list, hash);
} else {
- LinkedList<Segment> newPriors = priorSegments;
- return priorSegments.getLast().stringStructure(newPriors);
+ List<Segment> newPriors = list.subList(0, list.size() - 1);
+ return list.get(list.size() - 1).stringStructure(newPriors, hash);
}
}
- public String interpolationChunk() {
+ public String interpolationChunk(Options hash) {
return URLUtil.encode(value, "UTF-8" /* TODO enable to set charset */);
}
- public String interpolationStatement(LinkedList<Segment> priorSegments) {
+ public String interpolationStatement(List<Segment> list, Options hash) {
StringBuilder chunks = new StringBuilder(128);
- for(Segment seg : priorSegments) {
- chunks.append(seg.interpolationChunk());
+ for(Segment seg : list) {
+ chunks.append(seg.interpolationChunk(hash));
}
- chunks.append(interpolationChunk());
- return "\"" + chunks.toString() + "\"" + allOptionalsAvailableCondition(priorSegments);
+ chunks.append(interpolationChunk(hash));
+ return allOptionalsAvailableCondition(list) ? chunks.toString() : "";
}
- public String stringStructure(LinkedList<Segment> priorSegments) {
- return isOptional ? continueStringStructure(priorSegments) : interpolationStatement(priorSegments);
+ public String stringStructure(List<Segment> list, Options hash) {
+ return isOptional ? continueStringStructure(list, hash) : interpolationStatement(list, hash);
}
- public String allOptionalsAvailableCondition(LinkedList<Segment> priorSegments) {
- return null;
+ public boolean allOptionalsAvailableCondition(List<Segment> priorSegments) {
+ for (Segment segment : priorSegments) {
+ // TODO
+ }
+ return true;
}
public void matchExtraction(Options params, Matcher match, int nextCapture) {
@@ -64,6 +70,13 @@ public boolean hasKey() {
public String getKey() {
return null;
}
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
public abstract String regexpChunk();
public boolean isOptional() {
@@ -2,6 +2,8 @@
import java.util.Map;
+import org.seasar.framework.util.OgnlUtil;
+
public class UrlRewriter {
public static String urlFor(Map<String, Object> opts) {
Options options = new Options(opts);
@@ -20,7 +20,7 @@ public ControllerSegment(String key) {
public String regexpChunk() {
return "(?i-:(#{";
}
-
+
@Override
public void matchExtraction(Options params, Matcher match, int nextCapture) {
String key = getKey();
@@ -30,7 +30,12 @@ public void matchExtraction(Options params, Matcher match, int nextCapture) {
} else {
if (StringUtil.isNotEmpty(token))
params.put(key, token.toLowerCase());
- }
+ }
+ }
+ @Override
+ public String interpolationChunk(Options hash) {
+ String value = hash.getString(getKey());
+ return value;
}
}
@@ -1,5 +1,6 @@
package net.unit8.sastruts.routing.segment;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -62,7 +63,7 @@ public String defaultRegexpChunk() {
public String getDefault() {
return defaultValue;
}
-
+
@Override
public void matchExtraction(Options params, Matcher match, int nextCapture) {
String m = match.group(nextCapture);
@@ -75,10 +76,16 @@ public void matchExtraction(Options params, Matcher match, int nextCapture) {
}
params.put(key, value);
}
-
+
@Override
public String buildPattern(String pattern) {
pattern = regexpChunk() + pattern;
- return isOptional() ? RegexpUtil.optionalize(pattern) : pattern;
+ return isOptional() ? RegexpUtil.optionalize(pattern) : pattern;
+ }
+
+ @Override
+ public String interpolationChunk(Options hash) {
+ String value = hash.getString(getKey());
+ return URLUtil.encode(value, "UTF-8");
}
}
@@ -15,8 +15,8 @@ public OptionalFormatSegment() {
}
@Override
- public String interpolationChunk() {
- return "." + super.interpolationChunk();
+ public String interpolationChunk(Options hash) {
+ return "." + super.interpolationChunk(hash);
}
@Override
@@ -11,15 +11,10 @@ public PathSegment(String key, Options options) {
super(key, options);
}
- public String interpoationChunk() {
- return interpolationChunk(null);
- }
-
- public String interpolationChunk(String valueCode) {
- if (valueCode == null)
- valueCode = localName();
-
- return valueCode;
+ @Override
+ public String interpolationChunk(Options hash) {
+ String value = hash.getString(getKey());
+ return value;
}
public String getDefault() {
@@ -43,7 +38,7 @@ public int numberOfCaptures() {
public boolean optionalityImplied() {
return true;
}
-
+
@Override
public void matchExtraction(Options params, Matcher match, int nextCapture) {
String value = match.group(nextCapture);
@@ -1,37 +1,33 @@
package net.unit8.sastruts.routing.segment;
-import java.util.regex.Pattern;
-
-import org.seasar.framework.util.StringUtil;
-
import net.unit8.sastruts.routing.Options;
import net.unit8.sastruts.routing.RegexpUtil;
import net.unit8.sastruts.routing.Segment;
+import org.seasar.framework.util.StringUtil;
+
public class StaticSegment extends Segment {
- private String value;
private boolean raw;
public StaticSegment(String value) {
this(value, new Options());
}
public StaticSegment(String value, Options options) {
- super();
- this.value = value;
+ super(value);
if (options.containsKey("raw"))
this.raw = options.getBoolean("raw");
if (options.containsKey("optional"))
setOptional(options.getBoolean("optional"));
}
@Override
- public String interpolationChunk() {
- return raw ? value : super.interpolationChunk();
+ public String interpolationChunk(Options hash) {
+ return raw ? getValue() : super.interpolationChunk(hash);
}
@Override
public String regexpChunk() {
- String chunk = RegexpUtil.escape(value);
+ String chunk = RegexpUtil.escape(getValue());
return isOptional() ? chunk : chunk; // TODO
}
@@ -42,7 +38,7 @@ public int numberOfCaptures() {
@Override
public String buildPattern(String pattern) {
- String escaped = RegexpUtil.escape(value);
+ String escaped = RegexpUtil.escape(getValue());
if (isOptional() && StringUtil.isNotEmpty(pattern)) {
return "(?:" + RegexpUtil.optionalize(escaped) + "\\Z|" + escaped + RegexpUtil.unoptionalize(pattern) + ")";
} else if (isOptional()) {
@@ -54,6 +50,6 @@ public String buildPattern(String pattern) {
@Override
public String toString() {
- return value;
+ return getValue();
}
}

0 comments on commit a9c598e

Please sign in to comment.