[gatein-commits] gatein SVN: r5251 - 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
Wed Nov 24 10:54:31 EST 2010


Author: julien_viet
Date: 2010-11-24 10:54:30 -0500 (Wed, 24 Nov 2010)
New Revision: 5251

Added:
   portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestPatternBuilder.java
Modified:
   portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java
Log:
make char encoding in the pattern more readable


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-24 15:42:47 UTC (rev 5250)
+++ portal/branches/navcontroller/component/web/controller/src/main/java/org/exoplatform/web/controller/router/PatternBuilder.java	2010-11-24 15:54:30 UTC (rev 5251)
@@ -29,9 +29,6 @@
 {
 
    /** . */
-   private static final char[] TABLE = "0123456789ABCDEF".toCharArray();
-   
-   /** . */
    private final StringBuilder buffer = new StringBuilder();
 
    public PatternBuilder expr(String s)
@@ -72,11 +69,26 @@
 
    public PatternBuilder litteral(char c)
    {
-      buffer.append("\\u");
-      buffer.append(TABLE[(c & 0xF000) >> 12]);
-      buffer.append(TABLE[(c & 0x0F00) >> 8]);
-      buffer.append(TABLE[(c & 0x00F0) >> 4]);
-      buffer.append(TABLE[c & 0x000F]);
+      switch (c)
+      {
+         case '*':
+         case '[':
+         case '\\':
+         case '^':
+         case '$':
+         case '.':
+         case '|':
+         case '?':
+         case '+':
+         case '(':
+         case ')':
+            buffer.append("\\");
+            buffer.append(c);
+            break;
+         default:
+            buffer.append(c);
+            break;
+      }
       return this;
    }
 
@@ -84,5 +96,4 @@
    {
       return Pattern.compile(buffer.toString());
    }
-   
 }

Added: portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestPatternBuilder.java
===================================================================
--- portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestPatternBuilder.java	                        (rev 0)
+++ portal/branches/navcontroller/component/web/controller/src/test/java/org/exoplatform/web/controller/router/TestPatternBuilder.java	2010-11-24 15:54:30 UTC (rev 5251)
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+import java.util.regex.Pattern;
+
+/**
+ * @author <a href="mailto:julien.viet at exoplatform.com">Julien Viet</a>
+ * @version $Revision$
+ */
+public class TestPatternBuilder extends TestCase
+{
+
+   public void testEscapeReservedChar() throws Exception
+   {
+      assertLiteral('^');
+      assertLiteral('*');
+      assertLiteral('$');
+      assertLiteral('[');
+      assertLiteral(']');
+      assertLiteral('.');
+      assertLiteral('|');
+      assertLiteral('+');
+      assertLiteral('(');
+      assertLiteral(')');
+      assertLiteral('?');
+   }
+
+   private void assertLiteral(char c)
+   {
+      PatternBuilder pb = new PatternBuilder();
+      pb.expr("^");
+      pb.litteral(c);
+      pb.expr("$");
+      Pattern pattern = pb.build();
+      assertTrue(pattern.matcher(Character.toString(c)).matches());
+   }
+}



More information about the gatein-commits mailing list