Author: julien_viet
Date: 2010-11-29 16:08:43 -0500 (Mon, 29 Nov 2010)
New Revision: 5358
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RE.java
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestRE.java
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/router/PatternBuilder.java
Log:
class for handling RegExp related static methods
Added:
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RE.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RE.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RE.java 2010-11-29
21:08:43 UTC (rev 5358)
@@ -0,0 +1,147 @@
+/*
+ * 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.gatein.common.io.UndeclaredIOException;
+
+import java.io.IOException;
+
+/**
+ * Various utilities related to the regular expression package.
+ *
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class RE
+{
+
+ private RE()
+ {
+ }
+
+ /**
+ * Appends a char sequence to an appendable. Chars are appended as is unless they are
regular expression meta
+ * characters. Meta characters are escaped by appending a <i>\</i>
character before the actual character.
+ *
+ * @param appendable the appendable
+ * @param s the char sequence
+ * @param start the start offset
+ * @param end the end offset
+ * @throws UndeclaredIOException any io exception
+ * @throws IndexOutOfBoundsException when index go out of bounds
+ * @return the appendable argument
+ * @param <A> the appendable parameter type
+ */
+ public static <A extends Appendable> A appendLiteral(A appendable, CharSequence
s, int start, int end) throws UndeclaredIOException, IndexOutOfBoundsException
+ {
+ if (appendable == null)
+ {
+ throw new NullPointerException("No null appendable argument");
+ }
+ if (s == null)
+ {
+ throw new NullPointerException("No null char sequence argument");
+ }
+ try
+ {
+ while (start < end)
+ {
+ char c = s.charAt(start++);
+ if (isMeta(c))
+ {
+ appendable.append('\\');
+ }
+ appendable.append(c);
+ }
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ return appendable;
+ }
+
+ /**
+ * @see #appendLiteral(Appendable, CharSequence, int, int)
+ * @param appendable the appendable
+ * @param s the char sequence
+ * @throws UndeclaredIOException any io exception
+ * @throws IndexOutOfBoundsException when index go out of bounds
+ * @return the appendable argument
+ * @param <A> the appendable parameter type
+ */
+ public static <A extends Appendable> A appendLiteral(A appendable, CharSequence
s) throws UndeclaredIOException, IndexOutOfBoundsException
+ {
+ if (s == null)
+ {
+ throw new NullPointerException("No null char sequence argument");
+ }
+ return appendLiteral(appendable, s, 0, s.length());
+ }
+
+ /**
+ * @see #appendLiteral(Appendable, CharSequence, int, int)
+ * @param appendable the appendable
+ * @param c the char to append
+ * @throws UndeclaredIOException any io exception
+ * @throws IndexOutOfBoundsException when index go out of bounds
+ * @return the appendable argument
+ * @param <A> the appendable parameter type
+ */
+ public static <A extends Appendable> A appendLiteral(A appendable, char c)
throws UndeclaredIOException
+ {
+ if (appendable == null)
+ {
+ throw new NullPointerException("No null appendable argument");
+ }
+ try
+ {
+ if (isMeta(c))
+ {
+ appendable.append('\\');
+ }
+ appendable.append(c);
+ }
+ catch (IOException e)
+ {
+ throw new UndeclaredIOException(e);
+ }
+ return appendable;
+ }
+
+ /**
+ * Returns a regular expression meta character.
+ *
+ * @param c the char to test
+ * @return true for a meta character, false otherwise
+ */
+ public static boolean isMeta(char c)
+ {
+ return c >= '(' && c <= '+' // ()*+
+ || c == '?'
+ || c == '{'
+ || c == '|'
+ || c == '$'
+ || c == '^'
+ || c == '.'
+ || c == '['
+ || c == '\\';
+ }
+}
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-11-29
19:02:26 UTC (rev 5357)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/regexp/RegExpParser.java 2010-11-29
21:08:43 UTC (rev 5358)
@@ -19,8 +19,6 @@
package org.exoplatform.web.controller.regexp;
-import java.util.HashSet;
-import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -32,17 +30,6 @@
{
/** . */
- private static final Set<Character> ESCAPABLE = new HashSet<Character>();
-
- static
- {
- for (char c : "^.[$()|*+?{\\".toCharArray())
- {
- ESCAPABLE.add(c);
- }
- }
-
- /** . */
private static final Pattern pattern = Pattern.compile("^([0-9]+)" +
"(?:" + "(,)([0-9]+)?" + ")?$");
/** . */
@@ -210,7 +197,7 @@
{
index++;
char escaped = s.charAt(index);
- if (!ESCAPABLE.contains(escaped))
+ if (!RE.isMeta(escaped))
{
throw new SyntaxException();
}
@@ -396,7 +383,7 @@
}
else
{
- throw new UnsupportedOperationException();
+ throw new UnsupportedOperationException("not yet implemented");
}
}
}
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-29
19:02:26 UTC (rev 5357)
+++
portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java 2010-11-29
21:08:43 UTC (rev 5358)
@@ -19,6 +19,8 @@
package org.exoplatform.web.controller.router;
+import org.exoplatform.web.controller.regexp.RE;
+
import java.util.regex.Pattern;
/**
@@ -63,7 +65,7 @@
}
if (from < to)
{
- buffer.append(Pattern.quote(s.substring(from, to)));
+ RE.appendLiteral(buffer, s, from, to);
}
return this;
}
Added:
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestRE.java
===================================================================
---
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestRE.java
(rev 0)
+++
portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/regexp/TestRE.java 2010-11-29
21:08:43 UTC (rev 5358)
@@ -0,0 +1,159 @@
+/*
+ * 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.gatein.common.io.UndeclaredIOException;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+
+/**
+ * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
+ * @version $Revision$
+ */
+public class TestRE extends BaseGateInTest
+{
+
+ public void testAppendLiteral1()
+ {
+ StringBuilder sb = new StringBuilder();
+ assertSame(sb, RE.appendLiteral(sb, "a\\b"));
+ assertEquals("a\\\\b", sb.toString());
+ }
+
+ public void testAppendLiteral2()
+ {
+ StringBuilder sb = new StringBuilder();
+ assertSame(sb, RE.appendLiteral(sb, "a\\b", 0, 2));
+ assertEquals("a\\\\", sb.toString());
+ }
+
+ public void testAppendLiteral3()
+ {
+ StringBuilder sb = new StringBuilder();
+ assertSame(sb, RE.appendLiteral(sb, 'a'));
+ assertSame(sb, RE.appendLiteral(sb, '\\'));
+ assertEquals("a\\\\", sb.toString());
+ }
+
+ public void testAppendLiteralThrowsIOOBE()
+ {
+ try
+ {
+ RE.appendLiteral(new StringBuilder(), "a\\b", -1, 2);
+ fail();
+ }
+ catch (IndexOutOfBoundsException expected)
+ {
+ }
+ try
+ {
+ RE.appendLiteral(new StringBuilder(), "a\\b", 1, 4);
+ fail();
+ }
+ catch (IndexOutOfBoundsException expected)
+ {
+ }
+ }
+
+ public void testAppendLiteralThrowsNPE()
+ {
+ try
+ {
+ RE.appendLiteral(null, "a", 0, 1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ RE.appendLiteral(new StringBuilder(), null, 0, 1);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ RE.appendLiteral(null, "a");
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ RE.appendLiteral(new StringBuilder(), null);
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ try
+ {
+ RE.appendLiteral(null, 'a');
+ fail();
+ }
+ catch (NullPointerException expected)
+ {
+ }
+ }
+
+ public void testAppendLiteralThrowsIOE()
+ {
+ final IOException e = new IOException();
+ Writer appendable = new Writer()
+ {
+ public void write(char[] cbuf, int off, int len) throws IOException { throw e;
}
+ public void flush() throws IOException { }
+ public void close() throws IOException { }
+ };
+ try
+ {
+ RE.appendLiteral(appendable, "a", 0, 1);
+ fail();
+ }
+ catch (UndeclaredIOException expected)
+ {
+ assertSame(e, expected.getCause());
+ }
+ try
+ {
+ RE.appendLiteral(appendable, "a");
+ fail();
+ }
+ catch (UndeclaredIOException expected)
+ {
+ assertSame(e, expected.getCause());
+ }
+ try
+ {
+ RE.appendLiteral(appendable, 'a');
+ fail();
+ }
+ catch (UndeclaredIOException expected)
+ {
+ assertSame(e, expected.getCause());
+ }
+ }
+}