abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r620310 - in /incubator/abdera/java/trunk: dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Route.java server/src/test/java/org/apache/abdera/protocol/server/test/route/RouteTest.java
Date Sun, 10 Feb 2008 19:18:14 GMT
Author: jmsnell
Date: Sun Feb 10 11:18:13 2008
New Revision: 620310

URL: http://svn.apache.org/viewvc?rev=620310&view=rev
Log:
Per https://issues.apache.org/jira/browse/ABDERA-111

Modified:
    incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Route.java
    incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/protocol/server/test/route/RouteTest.java

Modified: incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Route.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Route.java?rev=620310&r1=620309&r2=620310&view=diff
==============================================================================
--- incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Route.java
(original)
+++ incubator/abdera/java/trunk/dependencies/i18n/src/main/java/org/apache/abdera/i18n/templates/Route.java
Sun Feb 10 11:18:13 2008
@@ -29,11 +29,14 @@
   
   private static final Evaluator EVALUATOR = new Evaluator();
   private static final Pattern VARIABLE = Pattern.compile("[\\*\\:](?:\\()?[0-9a-zA-Z]+(?:\\))?");
-  
+  private static final String VARIABLE_CONTENT_MATCH = "([^:/\\?#\\[\\]@!\\$&'\\(\\)\\*\\+,;\\=]+)";
+  private static final String VARIABLE_CONTENT_PARSE = "([^:/\\?#\\[\\]@!\\$&'\\(\\)\\*\\+,;\\=]*)";
   private final String name;
   private final String pattern;
   private final String[] tokens;
   private final String[] variables;
+  private final Pattern regexMatch;
+  private final Pattern regexParse;
 
   private Map<String, String> requirements;
 
@@ -50,6 +53,8 @@
     this.variables = initVariables();
     this.defaultValues = defaultValues;
     this.requirements = requirements;
+    this.regexMatch = initRegexMatch();
+    this.regexParse = initRegexParse();
   }
   
   private String[] initTokens() {
@@ -73,47 +78,36 @@
     Arrays.sort(vars);
     return vars;
   }
+
+  private Pattern initRegexMatch() {
+    StringBuffer match = new StringBuffer();
+    int cnt = 0;
+    for (String part : VARIABLE.split(pattern)) {
+      match.append(Pattern.quote(part));
+      if (cnt++ < tokens.length) {
+        match.append(VARIABLE_CONTENT_MATCH);
+      }
+    }
+    return Pattern.compile(match.toString());
+  }
+  private Pattern initRegexParse() {
+    StringBuffer parse = new StringBuffer();
+    int cnt = 0;
+    for (String part : VARIABLE.split(pattern)) {
+      parse.append(Pattern.quote(part));
+      if (cnt++ < tokens.length) {
+        parse.append(VARIABLE_CONTENT_PARSE);
+      }
+    }
+    return Pattern.compile(parse.toString());
+  }
+
   
   /**
    * Returns true if the given uri matches the route pattern
    */
   public boolean match(String uri) {
-    Matcher matcher = VARIABLE.matcher(pattern);
-    int uriStart = 0;
-    int prevPatternEnd = 0;
-    while (matcher.find()) {
-      int patternStart = matcher.start();
-      
-      String nonVariablePattern = pattern.substring(prevPatternEnd, patternStart);
-      if (prevPatternEnd == 0 && nonVariablePattern.length() == 0) {
-        prevPatternEnd = matcher.end();
-        continue;
-      }
-      
-      int idx = uri.indexOf(nonVariablePattern, uriStart);
-      if ((idx == -1 || idx+1 == uri.length()) && (defaultValues == null || !defaultValues.containsKey(var(matcher.group()))))
{
-        return false;
-      } else {
-        uriStart = idx + 1 + nonVariablePattern.length();
-      }
-      
-      // TODO: ensure requirements are met
-      
-      prevPatternEnd = matcher.end();
-    }
-    
-    // Check if the non variable, end segment matches
-    if (prevPatternEnd != pattern.length()) {
-      if (!uri.endsWith(pattern.substring(prevPatternEnd))) {
-        return false;
-      } else {
-        return true;
-      }
-    } else if (uriStart > uri.length()) {
-      return false;
-    }
-    
-    return uri.indexOf('/', uriStart) == -1;
+    return regexMatch.matcher(uri).matches();
   }
   
   /**
@@ -121,50 +115,11 @@
    */
   public Map<String,String> parse(String uri) {
     HashMap<String, String> vars = new HashMap<String, String>();
-    Matcher matcher = VARIABLE.matcher(pattern);
-    int uriStart = 0;
-    int prevPatternEnd = 0;
-    int valueStart = -1;
-    int nonVarIdx = -1; 
-    String prevVar = null;
-    while (matcher.find()) {
-      int patternStart = matcher.start();
-      String varName = var(matcher.group());
-      
-      String nonVariableSection = pattern.substring(prevPatternEnd, patternStart);
-      if (prevPatternEnd == 0 && nonVariableSection.length() == 0) {
-        prevPatternEnd = matcher.end();
-        valueStart = 0;
-        continue;
+    Matcher matcher = regexParse.matcher(uri);
+    if (matcher.matches()) {
+      for (int i = 0; i < matcher.groupCount(); i++) {
+        vars.put(var(tokens[i]), matcher.group(i+1).length() > 0 ? matcher.group(i+1)
: null);
       }
-
-      nonVarIdx = uri.indexOf(nonVariableSection, uriStart);
-      
-      if (valueStart != -1) {
-        vars.put(prevVar, uri.substring(valueStart, nonVarIdx));
-      }
-      
-      if ((nonVarIdx == -1 || nonVarIdx+1 == uri.length()) && (defaultValues == null
|| !defaultValues.containsKey(varName))) {
-        valueStart = -1;
-        break;
-      }
-
-      // TODO: ensure requirements are met
-      
-      valueStart = nonVarIdx + nonVariableSection.length();
-      
-      uriStart = valueStart + 1;
-      prevVar = varName;
-      prevPatternEnd = matcher.end();
-    }
-    
-    int tailEnd = uri.length();
-    if (prevPatternEnd != pattern.length() && uri.endsWith(pattern.substring(prevPatternEnd)))
{
-      tailEnd = tailEnd - (pattern.length() - prevPatternEnd);
-    }
-    
-    if (valueStart != uri.length() && valueStart != -1) {
-      vars.put(prevVar, uri.substring(valueStart, tailEnd));
     }
     
     return vars;

Modified: incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/protocol/server/test/route/RouteTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/protocol/server/test/route/RouteTest.java?rev=620310&r1=620309&r2=620310&view=diff
==============================================================================
--- incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/protocol/server/test/route/RouteTest.java
(original)
+++ incubator/abdera/java/trunk/server/src/test/java/org/apache/abdera/protocol/server/test/route/RouteTest.java
Sun Feb 10 11:18:13 2008
@@ -3,12 +3,12 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import junit.framework.Assert;
+
 import org.apache.abdera.i18n.templates.HashMapContext;
 import org.apache.abdera.i18n.templates.Route;
 import org.junit.Test;
 
-import junit.framework.Assert;
-
 public class RouteTest extends Assert {
   @Test
   public void testSimpleRoute() throws Exception {
@@ -52,6 +52,7 @@
     assertFalse(route.match("/foo"));
     assertTrue(route.match("/foo/test"));
     assertFalse(route.match("foo"));
+    assertFalse(route.match("/foo/test/bar"));
 
     Map<String, String> vars = route.parse("/1/2");
     assertEquals("1", vars.get("collection"));
@@ -129,4 +130,26 @@
     assertEquals("1", vars.get("collection"));
     assertEquals("2", vars.get("entry"));
   }
+  @Test
+  public void testSubDelims() throws Exception {
+    Route route = new Route("entry", "/base/:collection/:entry");
+
+    assertTrue(route.match("/base/test/123"));
+    assertFalse(route.match("/base/test/123;categories"));
+    
+    Map<String, String> vars = route.parse("/base/test/123");
+    assertEquals("test", vars.get("collection"));
+    assertEquals("123", vars.get("entry"));
+  }
+  @Test
+  public void testGenDelims() throws Exception {
+    Route route = new Route("entry", "/base/:collection/");
+
+    assertTrue(route.match("/base/test/"));
+    assertFalse(route.match("/base/test/123/"));
+    
+    Map<String, String> vars = route.parse("/base/test/");
+    assertEquals("test", vars.get("collection"));
+  }
+
 }



Mime
View raw message