myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i...@apache.org
Subject svn commit: r638959 - in /myfaces/tobago/trunk/core/src: main/java/org/apache/myfaces/tobago/component/LayoutTokens.java test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
Date Wed, 19 Mar 2008 18:21:18 GMT
Author: idus
Date: Wed Mar 19 11:21:16 2008
New Revision: 638959

URL: http://svn.apache.org/viewvc?rev=638959&view=rev
Log:
TOBAGO-639: Avoid usage of implicit regular expressions in parsing.

Added:
    myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
Modified:
    myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java

Modified: myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java?rev=638959&r1=638958&r2=638959&view=diff
==============================================================================
--- myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java
(original)
+++ myfaces/tobago/trunk/core/src/main/java/org/apache/myfaces/tobago/component/LayoutTokens.java
Wed Mar 19 11:21:16 2008
@@ -19,6 +19,7 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.commons.lang.math.NumberUtils;
 
 import java.util.StringTokenizer;
 import java.util.List;
@@ -29,8 +30,13 @@
  * Time: 1:11:25 PM
  */
 public class LayoutTokens {
+
   private static final Log LOG = LogFactory.getLog(LayoutTokens.class);
 
+  private static final String PIXEL_TOKEN_SUFFIX = "px";
+  private static final String PERCENT_TOKEN_SUFFIX = "%";
+  private static final String RELATIVE_TOKEN_SUFFIX = "*";
+
   private List<LayoutToken> tokens = new ArrayList<LayoutToken>();
 
   public int getSize() {
@@ -67,7 +73,6 @@
 
   public static LayoutTokens parse(String[] tokens) {
     LayoutTokens layoutTokens = new LayoutTokens();
-
     for (String token : tokens) {
       parseToken(token, layoutTokens);
     }
@@ -78,7 +83,6 @@
     return parse(tokens, null);
   }
 
-
   public static LayoutTokens parse(String tokens, LayoutToken defaultToken) {
     LayoutTokens layoutTokens = new LayoutTokens();
     if (tokens == null) {
@@ -99,33 +103,48 @@
     if (layoutToken != null) {
       layoutTokens.addToken(layoutToken);
     }
-
   }
 
   public static LayoutToken parseToken(String token) {
     try {
-      // TODO optimize me
       if ("*".equals(token)) {
         return RelativeLayoutToken.DEFAULT_INSTANCE;
       } else if (token.equals("fixed")) {
         return FixedLayoutToken.INSTANCE;
       } else if (token.equals("minimum")) {
         return new MinimumLayoutToken();
-      } else if (token.matches("\\d+px")) {
+      } else if (isPixelToken(token)) {
         return new PixelLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")));
-      } else if (token.matches("^\\d+\\%")) {
+      } else if (isPercentToken(token)) {
         return new PercentLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")));
-      } else if (token.matches("^\\d+\\*")) {
+      } else if (isRelativeToken(token)) {
         return new RelativeLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")));
       } else {
-        LOG.error("Unknown layout token " + token + " ignoring");
+        LOG.error("Ignoring unknown layout token '" + token + "'");
       }
     } catch (NumberFormatException e) {
-      LOG.error("Error parsing layout token " + token, e);
+      LOG.error("Error parsing layout token '" + token + "'", e);
     }
     return null;
   }
 
+  static boolean isPixelToken(String token) {
+    return isNumberAndSuffix(token, PIXEL_TOKEN_SUFFIX);
+  }
+
+  static boolean isPercentToken(String token) {
+    return isNumberAndSuffix(token, PERCENT_TOKEN_SUFFIX);
+  }
+
+  static boolean isRelativeToken(String token) {
+    return isNumberAndSuffix(token, RELATIVE_TOKEN_SUFFIX);
+  }
+
+  static boolean isNumberAndSuffix(String token, String suffix) {
+    return token.endsWith(suffix)
+        && NumberUtils.isDigits(token.substring(0, token.length() - suffix.length()));
+  }
+
   public String toString() {
     StringBuilder str = new StringBuilder();
     for (LayoutToken token : tokens) {
@@ -134,7 +153,6 @@
     }
     return str.toString();
   }
-
 
 }
 

Added: myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java?rev=638959&view=auto
==============================================================================
--- myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
(added)
+++ myfaces/tobago/trunk/core/src/test/java/org/apache/myfaces/tobago/component/LayoutTokensUnitTest.java
Wed Mar 19 11:21:16 2008
@@ -0,0 +1,41 @@
+package org.apache.myfaces.tobago.component;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import junit.framework.TestCase;
+
+public class LayoutTokensUnitTest extends TestCase {
+
+  public void testIsPixelToken() {
+    assertTrue(LayoutTokens.isPixelToken("120px"));
+  }
+
+  public void testIsPercentToken() {
+    assertTrue(LayoutTokens.isPercentToken("50%"));
+  }
+
+  public void testIsRelativeToken() {
+    assertTrue(LayoutTokens.isRelativeToken("3*"));
+  }
+
+  public void testIsNumberAndSuffix() {
+    assertTrue(LayoutTokens.isNumberAndSuffix("34cm", "cm"));
+    assertFalse(LayoutTokens.isNumberAndSuffix("acm", "cm"));
+    assertFalse(LayoutTokens.isNumberAndSuffix("cm", "cm"));
+  }
+}



Mime
View raw message