From do-not-reply at jboss.org Mon Nov 29 16:08:44 2010 Content-Type: multipart/mixed; boundary="===============7262781305134584539==" MIME-Version: 1.0 From: do-not-reply at jboss.org To: gatein-commits at lists.jboss.org Subject: [gatein-commits] gatein SVN: r5358 - in portal/branches/navcontroller/component/web/controller/src: main/java/org/exoplatform/web/controller/router and 1 other directories. Date: Mon, 29 Nov 2010 16:08:44 -0500 Message-ID: <201011292108.oATL8iUR028000@svn01.web.mwc.hst.phx2.redhat.com> --===============7262781305134584539== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- portal/branches/navcontroller/component/web/controller/src/main/java/or= g/exoplatform/web/controller/regexp/RE.java (rev 0) +++ portal/branches/navcontroller/component/web/controller/src/main/java/or= g/exoplatform/web/controller/regexp/RE.java 2010-11-29 21:08:43 UTC (rev 53= 58) @@ -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 Julien Viet + * @version $Revision$ + */ +public class RE +{ + + private RE() + { + } + + /** + * Appends a char sequence to an appendable. Chars are appended as is u= nless they are regular expression meta + * characters. Meta characters are escaped by appending a \ char= acter 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 the appendable parameter type + */ + public static A appendLiteral(A appendable, Char= Sequence s, int start, int end) throws UndeclaredIOException, IndexOutOfBou= ndsException + { + if (appendable =3D=3D null) + { + throw new NullPointerException("No null appendable argument"); + } + if (s =3D=3D null) + { + throw new NullPointerException("No null char sequence argument"); + } + try + { + while (start < end) + { + char c =3D 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 the appendable parameter type + */ + public static A appendLiteral(A appendable, Char= Sequence s) throws UndeclaredIOException, IndexOutOfBoundsException + { + if (s =3D=3D 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 the appendable parameter type + */ + public static A appendLiteral(A appendable, char= c) throws UndeclaredIOException + { + if (appendable =3D=3D 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 >=3D '(' && c <=3D '+' // ()*+ + || c =3D=3D '?' + || c =3D=3D '{' + || c =3D=3D '|' + || c =3D=3D '$' + || c =3D=3D '^' + || c =3D=3D '.' + || c =3D=3D '[' + || c =3D=3D '\\'; + } +} Modified: portal/branches/navcontroller/component/web/controller/src/main/j= ava/org/exoplatform/web/controller/regexp/RegExpParser.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- portal/branches/navcontroller/component/web/controller/src/main/java/or= g/exoplatform/web/controller/regexp/RegExpParser.java 2010-11-29 19:02:26 U= TC (rev 5357) +++ portal/branches/navcontroller/component/web/controller/src/main/java/or= g/exoplatform/web/controller/regexp/RegExpParser.java 2010-11-29 21:08:43 U= TC (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 ESCAPABLE =3D new HashSet(); - - static - { - for (char c : "^.[$()|*+?{\\".toCharArray()) - { - ESCAPABLE.add(c); - } - } - - /** . */ private static final Pattern pattern =3D Pattern.compile("^([0-9]+)" + = "(?:" + "(,)([0-9]+)?" + ")?$"); = /** . */ @@ -210,7 +197,7 @@ { index++; char escaped =3D 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/j= ava/org/exoplatform/web/controller/router/PatternBuilder.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- portal/branches/navcontroller/component/web/controller/src/main/java/or= g/exoplatform/web/controller/router/PatternBuilder.java 2010-11-29 19:02:26= UTC (rev 5357) +++ portal/branches/navcontroller/component/web/controller/src/main/java/or= g/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 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- portal/branches/navcontroller/component/web/controller/src/test/java/or= g/exoplatform/web/controller/regexp/TestRE.java (re= v 0) +++ portal/branches/navcontroller/component/web/controller/src/test/java/or= g/exoplatform/web/controller/regexp/TestRE.java 2010-11-29 21:08:43 UTC (re= v 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 Julien Viet + * @version $Revision$ + */ +public class TestRE extends BaseGateInTest +{ + + public void testAppendLiteral1() + { + StringBuilder sb =3D new StringBuilder(); + assertSame(sb, RE.appendLiteral(sb, "a\\b")); + assertEquals("a\\\\b", sb.toString()); + } + + public void testAppendLiteral2() + { + StringBuilder sb =3D new StringBuilder(); + assertSame(sb, RE.appendLiteral(sb, "a\\b", 0, 2)); + assertEquals("a\\\\", sb.toString()); + } + + public void testAppendLiteral3() + { + StringBuilder sb =3D 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 =3D new IOException(); + Writer appendable =3D new Writer() + { + public void write(char[] cbuf, int off, int len) throws IOExcepti= on { 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()); + } + } +} --===============7262781305134584539==--