Author: julien_viet
Date: 2010-11-30 03:46:11 -0500 (Tue, 30 Nov 2010)
New Revision: 5377
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpRenderer.java
Removed:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RegExpAnalyser.java
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
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/TestRouteEscaper.java
Log:
improve
Copied:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpRenderer.java
(from rev 5351,
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/regexp/RegExpRenderer.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpRenderer.java 2010-11-30
08:46:11 UTC (rev 5377)
@@ -0,0 +1,176 @@
+/*
+ * 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.regexp;
+
+import java.io.IOException;
+
+/**
+ * Renders a {@link RENode} to its pattern representation.
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class RegExpRenderer
+{
+
+ public <A extends Appendable> A render(RENode re, A appendable) throws
IOException, NullPointerException
+ {
+ if (re == null)
+ {
+ throw new NullPointerException("No null disjunction accepted");
+ }
+ if (appendable == null)
+ {
+ throw new NullPointerException("No null appendable accepted");
+ }
+
+ //
+ if (re instanceof RENode.Disjunction)
+ {
+ render((RENode.Disjunction)re, appendable);
+ }
+ else if (re instanceof RENode.Alternative)
+ {
+ render((RENode.Alternative)re, appendable);
+ }
+ else if (re instanceof RENode.Expr)
+ {
+ render((RENode.Expr)re, appendable);
+ }
+ else if (re instanceof RENode.CharacterClassExpr)
+ {
+ render((RENode.CharacterClassExpr)re, appendable);
+ }
+ else
+ {
+ throw new AssertionError();
+ }
+
+ //
+ return appendable;
+ }
+
+ private void render(RENode.Disjunction disjunction, Appendable appendable) throws
IOException, NullPointerException
+ {
+ render(disjunction.getAlternative(), appendable);
+ RENode.Disjunction next = disjunction.getNext();
+ if (next != null)
+ {
+ appendable.append('|');
+ render(next, appendable);
+ }
+ }
+
+ private void render(RENode.Alternative alternative, Appendable appendable) throws
IOException, NullPointerException
+ {
+ render(alternative.getExp(), appendable);
+ RENode.Alternative next = alternative.getNext();
+ if (next != null)
+ {
+ render(next, appendable);
+ }
+ }
+
+ private void render(RENode.Expr expression, Appendable appendable) throws IOException,
NullPointerException
+ {
+ Quantifier quantifier = null;
+ if (expression instanceof RENode.Any)
+ {
+ appendable.append('.');
+ quantifier = expression.getQuantifier();
+ }
+ else if (expression instanceof RENode.Group)
+ {
+ RENode.Group group = (RENode.Group)expression;
+ appendable.append("(?:");
+ this.render(group.getDisjunction(), appendable);
+ appendable.append(")");
+ quantifier = expression.getQuantifier();
+ }
+ else if (expression instanceof RENode.Char)
+ {
+ RENode.Char character = (RENode.Char)expression;
+ appendable.append(character.getValue());
+ quantifier = expression.getQuantifier();
+ }
+ else if (expression instanceof RENode.CharacterClass)
+ {
+ RENode.CharacterClass characterClass = (RENode.CharacterClass)expression;
+ render(characterClass.getExpr(), appendable);
+ quantifier = expression.getQuantifier();
+ }
+
+ //
+ if (quantifier != null)
+ {
+ appendable.append(quantifier.toString());
+ }
+ }
+
+ private void render(RENode.CharacterClassExpr expr, Appendable appendable) throws
IOException, NullPointerException
+ {
+ appendable.append("[");
+ render(expr, true, appendable);
+ appendable.append("]");
+ }
+
+ private void render(RENode.CharacterClassExpr expr, boolean braced, Appendable
appendable) throws IOException, NullPointerException
+ {
+ if (expr instanceof RENode.CharacterClassExpr.Char)
+ {
+ RENode.CharacterClassExpr.Char simple = (RENode.CharacterClassExpr.Char)expr;
+ appendable.append(simple.getValue());
+ }
+ else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Range)
+ {
+ RENode.CharacterClass.CharacterClassExpr.Range range =
(RENode.CharacterClass.CharacterClassExpr.Range)expr;
+ appendable.append(range.getFrom());
+ appendable.append('-');
+ appendable.append(range.getTo());
+ }
+ else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.And)
+ {
+ RENode.CharacterClass.CharacterClassExpr.And and =
(RENode.CharacterClass.CharacterClassExpr.And)expr;
+ render(and.getLeft(), false, appendable);
+ appendable.append("&&");
+ render(and.getRight(), false, appendable);
+ }
+ else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Or)
+ {
+ RENode.CharacterClass.CharacterClassExpr.Or or =
(RENode.CharacterClass.CharacterClassExpr.Or)expr;
+ render(or.getLeft(), false, appendable);
+ render(or.getRight(), false, appendable);
+ }
+ else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Not)
+ {
+ RENode.CharacterClass.CharacterClassExpr.Not not =
(RENode.CharacterClass.CharacterClassExpr.Not)expr;
+ if (!braced)
+ {
+ appendable.append("[");
+ }
+ appendable.append("^");
+ render(not.getNegated(), false, appendable);
+ if (!braced)
+ {
+ appendable.append(']');
+ }
+ }
+ }
+}
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java 2010-11-30
08:34:43 UTC (rev 5376)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java 2010-11-30
08:46:11 UTC (rev 5377)
@@ -33,7 +33,7 @@
/** . */
private final StringBuilder buffer = new StringBuilder();
- public PatternBuilder expr(String s)
+ public PatternBuilder expr(CharSequence s)
{
if (s == null)
{
Deleted:
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 2010-11-30
08:34:43 UTC (rev 5376)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RegExpAnalyser.java 2010-11-30
08:46:11 UTC (rev 5377)
@@ -1,185 +0,0 @@
-/*
- * 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;
-import org.exoplatform.web.controller.regexp.SyntaxException;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class RegExpAnalyser
-{
-
- /** . */
- private StringBuilder pattern;
-
- /** . */
- private boolean needReset;
-
- public RegExpAnalyser()
- {
- this.pattern = new StringBuilder();
- this.needReset = false;
- }
-
- public String getPattern()
- {
- return pattern.toString();
- }
-
- public RegExpAnalyser reset()
- {
- pattern.setLength(0);
- needReset = false;
- return this;
- }
-
- public void process(CharSequence pattern) throws MalformedRegExpException
- {
- try
- {
- RegExpParser parser = new RegExpParser(pattern);
- RENode.Disjunction disjunction = parser.parseDisjunction();
- process(disjunction);
- }
- catch (SyntaxException e)
- {
- throw new MalformedRegExpException(e);
- }
- }
-
- public void process(RENode.Disjunction disjunction) throws MalformedRegExpException
- {
- if (needReset)
- {
- throw new IllegalStateException();
- }
-
- //
- needReset = true;
- visit(disjunction);
- }
-
- private void visit(RENode.Disjunction disjunction) throws MalformedRegExpException
- {
- visit(disjunction.getAlternative());
- RENode.Disjunction next = disjunction.getNext();
- if (next != null)
- {
- pattern.append('|');
- visit(next);
- }
- }
-
- private void visit(RENode.Alternative alternative) throws MalformedRegExpException
- {
- visit(alternative.getExp());
- RENode.Alternative next = alternative.getNext();
- if (next != null)
- {
- visit(next);
- }
- }
-
- private void visit(RENode.Expr expression) throws MalformedRegExpException
- {
- Quantifier quantifier = null;
- if (expression instanceof RENode.Any)
- {
- pattern.append('.');
- quantifier = expression.getQuantifier();
- }
- else if (expression instanceof RENode.Group)
- {
- RENode.Group group = (RENode.Group)expression;
- pattern.append("(?:");
- visit(group.getDisjunction());
- pattern.append(")");
- quantifier = expression.getQuantifier();
- }
- else if (expression instanceof RENode.Char)
- {
- RENode.Char character = (RENode.Char)expression;
- pattern.append(character.getValue());
- quantifier = expression.getQuantifier();
- }
- else if (expression instanceof RENode.CharacterClass)
- {
- RENode.CharacterClass characterClass = (RENode.CharacterClass)expression;
- pattern.append("[");
- visit(characterClass.getExpr(), true);
- pattern.append("]");
- quantifier = expression.getQuantifier();
- }
-
- //
- if (quantifier != null)
- {
- pattern.append(quantifier);
- }
- }
-
- private void visit(RENode.CharacterClassExpr expr, boolean braced)
- {
- if (expr instanceof RENode.CharacterClassExpr.Char)
- {
- RENode.CharacterClassExpr.Char simple = (RENode.CharacterClassExpr.Char)expr;
- pattern.append(simple.getValue());
- }
- else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Range)
- {
- RENode.CharacterClass.CharacterClassExpr.Range range =
(RENode.CharacterClass.CharacterClassExpr.Range)expr;
- pattern.append(range.getFrom());
- pattern.append('-');
- pattern.append(range.getTo());
- }
- else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.And)
- {
- RENode.CharacterClass.CharacterClassExpr.And and =
(RENode.CharacterClass.CharacterClassExpr.And)expr;
- visit(and.getLeft(), false);
- pattern.append("&&");
- visit(and.getRight(), false);
- }
- else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Or)
- {
- RENode.CharacterClass.CharacterClassExpr.Or or =
(RENode.CharacterClass.CharacterClassExpr.Or)expr;
- visit(or.getLeft(), false);
- visit(or.getRight(), false);
- }
- else if (expr instanceof RENode.CharacterClass.CharacterClassExpr.Not)
- {
- RENode.CharacterClass.CharacterClassExpr.Not not =
(RENode.CharacterClass.CharacterClassExpr.Not)expr;
- if (!braced)
- {
- pattern.append("[");
- }
- pattern.append("^");
- visit(not.getNegated(), false);
- if (!braced)
- {
- pattern.append(']');
- }
- }
- }
-}
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-11-30
08:34:43 UTC (rev 5376)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/Route.java 2010-11-30
08:46:11 UTC (rev 5377)
@@ -24,10 +24,12 @@
import org.exoplatform.web.controller.metadata.RequestParamDescriptor;
import org.exoplatform.web.controller.metadata.RouteDescriptor;
import org.exoplatform.web.controller.metadata.RouteParamDescriptor;
+import org.exoplatform.web.controller.regexp.RegExpRenderer;
import org.exoplatform.web.controller.regexp.RENode;
import org.exoplatform.web.controller.regexp.RegExpParser;
import org.exoplatform.web.controller.regexp.SyntaxException;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -653,8 +655,8 @@
}
// Now work on the regex
- String renderingRegex;
- String routingRegex;
+ StringBuilder renderingRegex = new StringBuilder();
+ StringBuilder routingRegex = new StringBuilder();
try
{
RegExpParser parser = new RegExpParser(regex);
@@ -666,17 +668,17 @@
RouteEscaper escaper = new RouteEscaper('/', '_');
escaper.visit(routingDisjunction);
}
- RegExpAnalyser routingAnalyser = new RegExpAnalyser();
- routingAnalyser.process(routingDisjunction);
- routingRegex = routingAnalyser.getPattern();
+ new RegExpRenderer().render(routingDisjunction, routingRegex);
//
parser.reset();
RENode.Disjunction renderingDisjunction = parser.parseDisjunction();
- RegExpAnalyser renderingAnalyser = new RegExpAnalyser();
- renderingAnalyser.process(renderingDisjunction);
- renderingRegex = renderingAnalyser.getPattern();
+ new RegExpRenderer().render(renderingDisjunction, renderingRegex);
}
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
catch (SyntaxException e)
{
throw new RuntimeException(e);
@@ -686,10 +688,10 @@
throw new RuntimeException(e);
}
- //
+ // Append routing regex to the route regex
builder.expr("(").expr(routingRegex).expr(")");
- //
+ // Add the path param with the rendering regex
parameterPatterns.add(new PathParam(
parameterQName,
encodingMode,
@@ -700,10 +702,7 @@
//
builder.litteral(path, previous, pos);
chunks.add(path.substring(previous, pos));
- // Julien : should the pattern end with a $ ?????? I don't see that for
now
- // we need to figure out clearly
- Pattern pattern = builder.build();
- PatternRoute route = new PatternRoute(pattern, parameterPatterns, chunks);
+ PatternRoute route = new PatternRoute(builder.build(), parameterPatterns,
chunks);
// Wire
add(route);
Modified:
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 2010-11-30
08:34:43 UTC (rev 5376)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRegExpAnalyser.java 2010-11-30
08:46:11 UTC (rev 5377)
@@ -20,6 +20,9 @@
package org.exoplatform.web.controller.router;
import org.exoplatform.component.test.BaseGateInTest;
+import org.exoplatform.web.controller.regexp.RegExpRenderer;
+import org.exoplatform.web.controller.regexp.RENode;
+import org.exoplatform.web.controller.regexp.RegExpParser;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
@@ -29,35 +32,21 @@
{
/** . */
- private RegExpAnalyser analyser = new RegExpAnalyser();
+ private RegExpRenderer analyser = new RegExpRenderer();
private void assertAnalyse(String expectedPattern, String pattern)
{
try
{
- analyser.reset();
- analyser.process(pattern);
- assertEquals(expectedPattern, analyser.getPattern());
+ RENode.Disjunction disjunction = new RegExpParser(pattern).parseDisjunction();
+ assertEquals(expectedPattern, analyser.render(disjunction, new
StringBuilder()).toString());
}
- catch (MalformedRegExpException e)
+ catch (Exception e)
{
fail(e);
}
}
- private void assertAnalyseFails(String pattern)
- {
- analyser.reset();
- try
- {
- analyser.process(pattern);
- fail();
- }
- catch (MalformedRegExpException e)
- {
- }
- }
-
public void testCharacterClass()
{
assertAnalyse("[a]", "[a]");
Modified:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRouteEscaper.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRouteEscaper.java 2010-11-30
08:34:43 UTC (rev 5376)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRouteEscaper.java 2010-11-30
08:46:11 UTC (rev 5377)
@@ -20,6 +20,7 @@
package org.exoplatform.web.controller.router;
import org.exoplatform.component.test.BaseGateInTest;
+import org.exoplatform.web.controller.regexp.RegExpRenderer;
import org.exoplatform.web.controller.regexp.RENode;
import org.exoplatform.web.controller.regexp.RegExpParser;
@@ -39,9 +40,7 @@
RouteEscaper escaper = new RouteEscaper('/', '_');
RENode.Disjunction re = parser.parseDisjunction();
escaper.visit(re);
- RegExpAnalyser analyser = new RegExpAnalyser();
- analyser.process(re);
- Pattern p = Pattern.compile(analyser.getPattern());
+ Pattern p = Pattern.compile(new RegExpRenderer().render(re, new
StringBuilder()).toString());
Matcher matcher = p.matcher(test);
assertTrue(matcher.find());
assertEquals(expectedValue, matcher.group());