Author: julien_viet
Date: 2010-12-01 17:00:26 -0500 (Wed, 01 Dec 2010)
New Revision: 5430
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/GroupType.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestRegExpAnalyser.java
Removed:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRegExpAnalyser.java
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpRenderer.java
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RouteEscaper.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java
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/TestRouteEscaper.java
Log:
support for more special constructs
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/GroupType.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/GroupType.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/GroupType.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -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.regexp;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public enum GroupType
+{
+
+ CAPTURING_GROUP("(", ")"),
+
+ NON_CAPTURING_GROUP("(?:", ")"),
+
+ POSITIVE_LOOKAHEAD("(?=", ")"),
+
+ NEGATIVE_LOOKAHEAD("(?!", ")"),
+
+ POSITIVE_LOOKBEHIND("(?<=", ")"),
+
+ NEGATIVE_LOOKBEHIND("(?<!", ")");
+
+ /** . */
+ private final String open;
+
+ /** . */
+ private final String close;
+
+ GroupType(String open, String close)
+ {
+ this.open = open;
+ this.close = close;
+ }
+
+ public String getOpen()
+ {
+ return open;
+ }
+
+ public String getClose()
+ {
+ return close;
+ }
+}
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java 2010-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RENode.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -19,6 +19,8 @@
package org.exoplatform.web.controller.regexp;
+import java.util.EnumMap;
+
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
@@ -235,15 +237,15 @@
{
/** . */
- private final Ref<Disjunction> disjunction;
+ private GroupType type;
/** . */
- private boolean capturing;
+ private final Ref<Disjunction> disjunction;
- public Group(Disjunction disjunction, boolean capturing)
+ public Group(Disjunction disjunction, GroupType type)
{
this.disjunction = new NonNullableRef<Disjunction>(Disjunction.class,
disjunction);
- this.capturing = capturing;
+ this.type = type;
}
public Disjunction getDisjunction()
@@ -256,20 +258,20 @@
this.disjunction.set(disjunction);
}
- public boolean isCapturing()
+ public GroupType getType()
{
- return capturing;
+ return type;
}
- public void setCapturing(boolean capturing)
+ public void setType(GroupType type)
{
- this.capturing = capturing;
+ this.type = type;
}
@Override
protected void writeTo(StringBuilder sb)
{
- sb.append("<").append(capturing ? "(" :
"(?:").append('>').append(disjunction.get()).append("</").append(capturing
? ")" : ":?)").append(">");
+
sb.append("<").append(type.getOpen()).append('>').append(disjunction.get()).append("</").append(type.getOpen()).append(">");
}
}
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java 2010-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -151,23 +151,52 @@
// Do we have a special construct ?
int startGroup = index + 1;
- boolean capturing = true;
+ GroupType type = GroupType.CAPTURING_GROUP;
if (startGroup < endGroup)
{
if (s.charAt(startGroup) == '?')
{
if (startGroup + 1 < endGroup)
{
- if (s.charAt(startGroup + 1) == ':')
+ switch (s.charAt(startGroup + 1))
{
- // It's a non capturing group, so it's ok
- startGroup += 2;
- capturing = false;
+ case ':':
+ startGroup += 2;
+ type = GroupType.NON_CAPTURING_GROUP;
+ break;
+ case '=':
+ startGroup += 2;
+ type = GroupType.POSITIVE_LOOKAHEAD;
+ break;
+ case '!':
+ startGroup += 2;
+ type = GroupType.NEGATIVE_LOOKAHEAD;
+ break;
+ case '<':
+ if (startGroup + 2 < endGroup)
+ {
+ switch (s.charAt(startGroup + 2))
+ {
+ case '=':
+ startGroup += 3;
+ type = GroupType.POSITIVE_LOOKBEHIND;
+ break;
+ case '!':
+ startGroup += 3;
+ type = GroupType.NEGATIVE_LOOKBEHIND;
+ break;
+ default:
+ throw createSyntaxException("Syntax not
supported", index + 1, index + 4);
+ }
+ }
+ else
+ {
+ throw createSyntaxException("Syntax not
supported", index + 1, index + 3);
+ }
+ break;
+ default:
+ throw createSyntaxException("Syntax not supported",
index + 1, index + 3);
}
- else
- {
- throw createSyntaxException("Only non capturing group syntax
is supported", index + 1, index + 3);
- }
}
else
{
@@ -178,7 +207,7 @@
//
RENode.Disjunction grouped = new RegExpParser(s, startGroup,
endGroup).parseDisjunction();
- exp = new RENode.Group(grouped, capturing);
+ exp = new RENode.Group(grouped, type);
index = endGroup + 1;
break;
case '*':
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpRenderer.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpRenderer.java 2010-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpRenderer.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -99,9 +99,9 @@
else if (expression instanceof RENode.Group)
{
RENode.Group group = (RENode.Group)expression;
- appendable.append("(?:");
+ appendable.append(group.getType().getOpen());
this.render(group.getDisjunction(), appendable);
- appendable.append(")");
+ appendable.append(group.getType().getClose());
quantifier = expression.getQuantifier();
}
else if (expression instanceof RENode.Char)
Modified:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RouteEscaper.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RouteEscaper.java 2010-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/RouteEscaper.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -19,6 +19,7 @@
package org.exoplatform.web.controller.router;
+import org.exoplatform.web.controller.regexp.GroupType;
import org.exoplatform.web.controller.regexp.RENode;
/**
@@ -76,7 +77,7 @@
}
}
- public void visit(RENode.Expr expr)
+ public void visit(RENode.Expr expr) throws MalformedRegExpException
{
if (expr instanceof RENode.Char)
{
@@ -86,8 +87,19 @@
c.setValue(dst);
}
}
- if (expr instanceof RENode.Any)
+ else if (expr instanceof RENode.Group)
{
+ RENode.Group group = (RENode.Group)expr;
+/*
+ if (group.getType() == GroupType.CAPTURING_GROUP)
+ {
+ group.setType(GroupType.NON_CAPTURING_GROUP);
+ }
+*/
+ visit(group.getDisjunction());
+ }
+ else if (expr instanceof RENode.Any)
+ {
RENode.CharacterClass repl = new RENode.CharacterClass(new
RENode.CharacterClassExpr.Not(new RENode.CharacterClassExpr.Char('/')));
repl.setQuantifier(expr.getQuantifier());
expr.replaceBy(repl);
Modified:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java 2010-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestParser.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -184,11 +184,16 @@
public void testGroup()
{
- new
ParserTester("(a)").assertParseExpression("<(><c>a</c></)>",
3);
- new
ParserTester("(a(b)c)").assertParseExpression("<(><c>a</c><(><c>b</c></)><c>c</c></)>",
7);
- new
ParserTester("(?:a)").assertParseExpression("<(?:><c>a</c></:?)>",
5);
+ new
ParserTester("(a)").assertParseExpression("<(><c>a</c></(>",
3);
+ new
ParserTester("(a(b)c)").assertParseExpression("<(><c>a</c><(><c>b</c></(><c>c</c></(>",
7);
+ new
ParserTester("(?:a)").assertParseExpression("<(?:><c>a</c></(?:>",
5);
+ new
ParserTester("(?=a)").assertParseExpression("<(?=><c>a</c></(?=>",
5);
+ new
ParserTester("(?!a)").assertParseExpression("<(?!><c>a</c></(?!>",
5);
+ new
ParserTester("(?<=a)").assertParseExpression("<(?<=><c>a</c></(?<=>",
6);
+ new
ParserTester("(?<!a)").assertParseExpression("<(?<!><c>a</c></(?<!>",
6);
new ParserTester("(?)").assertNotParseExpression();
- new ParserTester("(?a)").assertNotParseExpression();
+ new ParserTester("(?_)").assertNotParseExpression();
+ new ParserTester("(?<_)").assertNotParseExpression();
}
// missing stuff:
Copied:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestRegExpAnalyser.java
(from rev 5377,
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/regexp/TestRegExpAnalyser.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestRegExpAnalyser.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -0,0 +1,85 @@
+/*
+ * 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 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>
+ * @version $Revision$
+ */
+public class TestRegExpAnalyser extends BaseGateInTest
+{
+
+ /** . */
+ private RegExpRenderer renderer = new RegExpRenderer();
+
+ private void assertAnalyse(String expectedPattern, String pattern)
+ {
+ try
+ {
+ RENode.Disjunction disjunction = new RegExpParser(pattern).parseDisjunction();
+ assertEquals(expectedPattern, renderer.render(disjunction, new
StringBuilder()).toString());
+ }
+ catch (Exception e)
+ {
+ fail(e);
+ }
+ }
+
+ public void testCharacterClass()
+ {
+ assertAnalyse("[a]", "[a]");
+ assertAnalyse("[ab]", "[ab]");
+ assertAnalyse("[ab]", "[a[b]]");
+ assertAnalyse("[abc]", "[abc]");
+ assertAnalyse("[abc]", "[[a]bc]");
+ assertAnalyse("[abc]", "[a[b]c]");
+ assertAnalyse("[abc]", "[ab[c]]");
+ assertAnalyse("[abc]", "[[ab]c]");
+ assertAnalyse("[abc]", "[a[bc]]");
+ assertAnalyse("[abc]", "[[abc]]");
+ assertAnalyse("[^a]", "[^a]");
+ }
+
+ public void testGroupContainer()
+ {
+ assertAnalyse("(a)", "(a)");
+ assertAnalyse("(a(?:b))", "(a(?:b))");
+ assertAnalyse("(?:a(b))", "(?:a(b))");
+ assertAnalyse("(a)(?:b)", "(a)(?:b)");
+ assertAnalyse("(a(b))", "(a(b))");
+ assertAnalyse("(a)(b)", "(a)(b)");
+
+ //
+ assertAnalyse("(?=a)", "(?=a)");
+ assertAnalyse("(?!a)", "(?!a)");
+ assertAnalyse("(?<=a)", "(?<=a)");
+ assertAnalyse("(?<!a)", "(?<!a)");
+ }
+
+ public void testBilto()
+ {
+ assertAnalyse("[a]+", "[a]+");
+ }
+}
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-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestMatch.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -191,6 +191,31 @@
assertNull(router.route("/abcdef"));
}
+/*
+ public void testLookAhead() throws Exception
+ {
+ Router router = router().
+ add(route("/{a}").
+ with(
+
pathParam("a").matchedBy("(.(?=/))?").preservingPath()).
+ sub(route("/{b}").
+ with(pathParam("b").matchedBy(".").preservingPath()))
+ ).build();
+
+ //
+ Map<QualifiedName, String> expectedParameters = new HashMap<QualifiedName,
String>();
+ expectedParameters.put(QualifiedName.create("a"), "");
+ expectedParameters.put(QualifiedName.create("b"), "b");
+ assertEquals(expectedParameters, router.route("/b"));
+ assertEquals("/b", router.render(expectedParameters));
+
+ //
+ expectedParameters.put(QualifiedName.create("a"), "a");
+ assertEquals(expectedParameters, router.route("/a/b"));
+ assertEquals("/a/b", router.render(expectedParameters));
+ }
+*/
+
public void testZeroOrOneFollowedBySubRoute() throws Exception
{
Router router = router().
Deleted:
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-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRegExpAnalyser.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -1,79 +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.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>
- * @version $Revision$
- */
-public class TestRegExpAnalyser extends BaseGateInTest
-{
-
- /** . */
- private RegExpRenderer analyser = new RegExpRenderer();
-
- private void assertAnalyse(String expectedPattern, String pattern)
- {
- try
- {
- RENode.Disjunction disjunction = new RegExpParser(pattern).parseDisjunction();
- assertEquals(expectedPattern, analyser.render(disjunction, new
StringBuilder()).toString());
- }
- catch (Exception e)
- {
- fail(e);
- }
- }
-
- public void testCharacterClass()
- {
- assertAnalyse("[a]", "[a]");
- assertAnalyse("[ab]", "[ab]");
- assertAnalyse("[ab]", "[a[b]]");
- assertAnalyse("[abc]", "[abc]");
- assertAnalyse("[abc]", "[[a]bc]");
- assertAnalyse("[abc]", "[a[b]c]");
- assertAnalyse("[abc]", "[ab[c]]");
- assertAnalyse("[abc]", "[[ab]c]");
- assertAnalyse("[abc]", "[a[bc]]");
- assertAnalyse("[abc]", "[[abc]]");
- assertAnalyse("[^a]", "[^a]");
- }
-
- public void testGroupContainer()
- {
- assertAnalyse("(?:a)", "(a)");
- assertAnalyse("(?:a(?:b))", "(a(?:b))");
- assertAnalyse("(?:a(?:b))", "(?:a(b))");
- assertAnalyse("(?:a)(?:b)", "(a)(?:b)");
- assertAnalyse("(?:a(?:b))", "(a(b))");
- assertAnalyse("(?:a)(?:b)", "(a)(b)");
- }
-
- public void testBilto()
- {
- 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-12-01
17:48:13 UTC (rev 5429)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestRouteEscaper.java 2010-12-01
22:00:26 UTC (rev 5430)
@@ -56,4 +56,14 @@
match("[/a]*", "_a_/_", "_a_");
match("[,-1&&[^/]]*", "_/_", "");
}
+
+ public void testGroup() throws Exception
+ {
+ match("(/)", "_", "_");
+ match("(?:/)", "_", "_");
+ match(".(?=/)", "a_", "a");
+ match("a(?!/)", "ab", "a");
+ match(".(?<=/)a", "ba_a", "_a");
+ match(".(?<!/)a", "_aba", "ba");
+ }
}