[gatein-commits] gatein SVN: r5306 - in portal/branches/navcontroller/component/web/controller/src: test/java/org/exoplatform/web/controller/router and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Sat Nov 27 07:06:14 EST 2010


Author: julien_viet
Date: 2010-11-27 07:06:14 -0500 (Sat, 27 Nov 2010)
New Revision: 5306

Added:
   portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RegExpAnalyser.java
   portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRegExpAnalyser.java
Modified:
   portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java
   portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java
Log:
- start the regexp analyser based on the regexp parser
- start to write unit test for language matching in the url path


Added: portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RegExpAnalyser.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RegExpAnalyser.java	                        (rev 0)
+++ portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RegExpAnalyser.java	2010-11-27 12:06:14 UTC (rev 5306)
@@ -0,0 +1,180 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.web.controller.router;
+
+import org.exoplatform.web.controller.regexp.Quantifier;
+import org.exoplatform.web.controller.regexp.RENode;
+import org.exoplatform.web.controller.regexp.RegExpParser;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class RegExpAnalyser
+{
+
+   /** . */
+   private StringBuilder sb;
+
+   /** . */
+   private boolean groupContainer;
+
+   /** . */
+   private boolean needReset;
+
+   public RegExpAnalyser()
+   {
+      this.sb = new StringBuilder();
+      this.groupContainer = false;
+      this.needReset = false;
+   }
+
+   public boolean isGroupContainer()
+   {
+      return groupContainer;
+   }
+
+   public String getPattern()
+   {
+      return sb.toString();
+   }
+
+   public RegExpAnalyser reset()
+   {
+      sb.setLength(0);
+      groupContainer = false;
+      needReset = false;
+      return this;
+   }
+
+   public void process(CharSequence pattern)
+   {
+      RegExpParser parser = new RegExpParser(pattern);
+      RENode.Disjunction disjunction = parser.parseDisjunction();
+      process(disjunction);
+   }
+
+   public void process(RENode.Disjunction disjunction)
+   {
+      if (needReset)
+      {
+         throw new IllegalStateException();
+      }
+
+      //
+      needReset = true;
+      visit(disjunction);
+   }
+
+   private void visit(RENode.Disjunction disjunction)
+   {
+      visit(disjunction.getAlternative());
+      RENode.Disjunction next = disjunction.getNext();
+      if (next != null)
+      {
+         sb.append('|');
+         visit(next);
+      }
+   }
+
+   private void visit(RENode.Alternative alternative)
+   {
+      visit(alternative.getExp());
+      RENode.Alternative next = alternative.getNext();
+      if (next != null)
+      {
+         visit(next);
+      }
+   }
+
+   private void visit(RENode.Exp expression)
+   {
+      Quantifier quantifier = null;
+      if (expression instanceof RENode.Dot)
+      {
+         sb.append('.');
+         quantifier = expression.getQuantifier();
+      }
+      else if (expression instanceof RENode.Group)
+      {
+         RENode.Group group = (RENode.Group)expression;
+         sb.append(groupContainer ? "(?:" : "(");
+         groupContainer = true;
+         visit(group.getDisjunction());
+         sb.append(")");
+         quantifier = expression.getQuantifier();
+      }
+      else if (expression instanceof RENode.Character)
+      {
+         RENode.Character character = (RENode.Character)expression;
+         sb.append(character.getValue());
+         quantifier = expression.getQuantifier();
+      }
+      else if (expression instanceof RENode.CharacterClass)
+      {
+         RENode.CharacterClass characterClass = (RENode.CharacterClass)expression;
+         sb.append("[");
+         visit(characterClass.getExpr());
+         sb.append("]");
+      }
+
+      //
+      if (quantifier != null)
+      {
+         sb.append(quantifier);
+      }
+   }
+
+   private void visit(RENode.CharacterClassExpr expr)
+   {
+      if (expr instanceof RENode.CharacterClassExpr.Simple)
+      {
+         RENode.CharacterClass.CharacterClassExpr.Simple simple = (RENode.CharacterClass.CharacterClassExpr.Simple)expr;
+         sb.append(simple.getValue());
+      }
+      else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Range)
+      {
+         RENode.CharacterClass.CharacterClassExpr.Range range = (RENode.CharacterClass.CharacterClassExpr.Range)expr;
+         sb.append(range.getFrom());
+         sb.append('-');
+         sb.append(range.getTo());
+      }
+      else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.And)
+      {
+         RENode.CharacterClass.CharacterClassExpr.And and = (RENode.CharacterClass.CharacterClassExpr.And)expr;
+         visit(and.getLeft());
+         sb.append("&&");
+         visit(and.getRight());
+      }
+      else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Or)
+      {
+         RENode.CharacterClass.CharacterClassExpr.Or or = (RENode.CharacterClass.CharacterClassExpr.Or)expr;
+         visit(or.getLeft());
+         visit(or.getRight());
+      }
+      else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Not)
+      {
+         RENode.CharacterClass.CharacterClassExpr.Not not = (RENode.CharacterClass.CharacterClassExpr.Not)expr;
+         sb.append("[^");
+         visit(not.getNegated());
+         sb.append(']');
+      }
+   }
+}

Modified: portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java	2010-11-27 12:02:10 UTC (rev 5305)
+++ portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java	2010-11-27 12:06:14 UTC (rev 5306)
@@ -191,4 +191,15 @@
       assertEquals(expectedParameters, router.route("/a"));
       assertEquals(Collections.singletonMap(QualifiedName.create("a"), "a"), router.route("/a/b"));
    }
+
+   public void testLang() throws Exception
+   {
+      RouterDescriptor routerMD = new RouterDescriptor();
+      routerMD.addRoute(new RouteDescriptor("/{a}b").addPathParam(QualifiedName.parse("a"), "(?:[A-Za-z]{2}/)?", EncodingMode.PRESERVE_PATH));
+      Router router = new Router(routerMD);
+
+      //
+      assertEquals(Collections.singletonMap(QualifiedName.create("a"), "fr/"), router.route("/fr/b"));
+      assertEquals(Collections.singletonMap(QualifiedName.create("a"), ""), router.route("/b"));
+   }
 }

Added: portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRegExpAnalyser.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRegExpAnalyser.java	                        (rev 0)
+++ portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRegExpAnalyser.java	2010-11-27 12:06:14 UTC (rev 5306)
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2010 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.exoplatform.web.controller.router;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class TestRegExpAnalyser extends TestCase
+{
+
+   /** . */
+   private RegExpAnalyser analyser = new RegExpAnalyser();
+
+   private void assertAnalyse(String expectedPattern, boolean expectedGroupContainer, String pattern)
+   {
+      analyser.reset();
+      analyser.process(pattern);
+      assertEquals(expectedPattern, analyser.getPattern());
+      assertEquals(expectedGroupContainer, analyser.isGroupContainer());
+   }
+
+   public void testCharacterClass()
+   {
+      assertAnalyse("[a]", false, "[a]");
+      assertAnalyse("[ab]", false, "[ab]");
+      assertAnalyse("[ab]", false, "[a[b]]");
+      assertAnalyse("[abc]", false, "[abc]");
+      assertAnalyse("[abc]", false, "[[a]bc]");
+      assertAnalyse("[abc]", false, "[a[b]c]");
+      assertAnalyse("[abc]", false, "[ab[c]]");
+      assertAnalyse("[abc]", false, "[[ab]c]");
+      assertAnalyse("[abc]", false, "[a[bc]]");
+      assertAnalyse("[abc]", false, "[[abc]]");
+   }
+
+   public void testGroupContainer()
+   {
+      assertAnalyse("(abc)", true, "(abc)");
+      assertAnalyse("(a(?:bc))", true, "(a(bc))");
+      assertAnalyse("(a)(?:b)", true, "(a)(b)");
+   }
+}

Modified: portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java	2010-11-27 12:02:10 UTC (rev 5305)
+++ portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRender.java	2010-11-27 12:06:14 UTC (rev 5306)
@@ -149,4 +149,15 @@
       //
       assertEquals("/a/b", router.render(Collections.singletonMap(QualifiedName.create("p"), "a")));
    }
+
+   public void testLang() throws Exception
+   {
+      RouterDescriptor routerMD = new RouterDescriptor();
+      routerMD.addRoute(new RouteDescriptor("/{a}b").addPathParam(QualifiedName.parse("a"), "(?:[A-Za-z]{2}/)?", EncodingMode.PRESERVE_PATH));
+      Router router = new Router(routerMD);
+
+      //
+      assertEquals("/fr/b", router.render(Collections.singletonMap(QualifiedName.parse("a"), "fr/")));
+      assertEquals("/b", router.render(Collections.singletonMap(QualifiedName.parse("a"), "")));
+   }
 }



More information about the gatein-commits mailing list