You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ar...@apache.org on 2012/09/28 21:00:51 UTC

svn commit: r1391596 - in /myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp: StringArrayValueExpression.java TagUtils.java UIXComponentELTag.java UIXComponentTag.java

Author: arobinson74
Date: Fri Sep 28 19:00:51 2012
New Revision: 1391596

URL: http://svn.apache.org/viewvc?rev=1391596&view=rev
Log:
TRINIDAD-2318

Added a TagUtils class to share the splitting code across different classes (package private). Added a wrapper for value expressions to handle the coercion from string to string array. Altered the tag classes to use the new tag utils and finally, altered the setStringArrayProperty method to wrap any non-literal value expressions to support the coercion.

Added:
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/StringArrayValueExpression.java   (with props)
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/TagUtils.java   (with props)
Modified:
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentELTag.java
    myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentTag.java

Added: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/StringArrayValueExpression.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/StringArrayValueExpression.java?rev=1391596&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/StringArrayValueExpression.java (added)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/StringArrayValueExpression.java Fri Sep 28 19:00:51 2012
@@ -0,0 +1,106 @@
+/*
+ * 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.
+ */
+package org.apache.myfaces.trinidad.webapp;
+
+import java.io.Serializable;
+
+import javax.el.ELContext;
+import javax.el.ValueExpression;
+
+/**
+ * Value expression to coerce String to String[] by splitting on whitespace.
+ */
+class StringArrayValueExpression
+  extends ValueExpression
+  implements Serializable
+{
+  public StringArrayValueExpression(
+    ValueExpression wrapped)
+  {
+    _wrapped = wrapped;
+  }
+
+  @Override
+  public Object getValue(ELContext context)
+  {
+    Object value = _wrapped.getValue(context);
+
+    if (value instanceof String)
+    {
+      return TagUtils.parseNameTokens(value);
+    }
+
+    return value;
+  }
+
+  @Override
+  public void setValue(ELContext context, Object value)
+  {
+    _wrapped.setValue(context, value);
+  }
+
+  @Override
+  public boolean isReadOnly(ELContext context)
+  {
+    return _wrapped.isReadOnly(context);
+  }
+
+  @Override
+  public Class<?> getType(ELContext context)
+  {
+    return _wrapped.getType(context);
+  }
+
+  @Override
+  public Class<?> getExpectedType()
+  {
+    return _wrapped.getExpectedType();
+  }
+
+  @Override
+  public String getExpressionString()
+  {
+    return _wrapped.getExpressionString();
+  }
+
+  @Override
+  public boolean equals(Object obj)
+  {
+    return obj == this ||
+           (obj instanceof StringArrayValueExpression &&
+            ((StringArrayValueExpression)obj).getExpressionString().equals(getExpressionString()));
+  }
+
+  @Override
+  public int hashCode()
+  {
+    return _wrapped.hashCode();
+  }
+
+  @Override
+  public boolean isLiteralText()
+  {
+    return _wrapped.isLiteralText();
+  }
+
+  @SuppressWarnings("compatibility:-2817575277544016450")
+  private static final long serialVersionUID = 1L;
+
+  private final ValueExpression _wrapped;
+}

Propchange: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/StringArrayValueExpression.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/TagUtils.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/TagUtils.java?rev=1391596&view=auto
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/TagUtils.java (added)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/TagUtils.java Fri Sep 28 19:00:51 2012
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+package org.apache.myfaces.trinidad.webapp;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+
+final class TagUtils
+{
+  private TagUtils() {}
+
+
+  /**
+   * Parses a whitespace separated series of name tokens.
+   * @param o the full string
+   * @return an array of each constituent value, or null
+   *  if there are no tokens (that is, the string is empty or
+   *  all whitespace)
+   * @todo Move to utility function somewhere (ADF Share?)
+   */
+  @SuppressWarnings("oracle.jdeveloper.java.null-array-return")
+  static final String[] parseNameTokens(Object o)
+  {
+    List<String> list = parseNameTokensAsList(o);
+
+    if (list == null)
+      return null;
+
+    return list.toArray(new String[list.size()]);
+  }
+
+  @SuppressWarnings("oracle.jdeveloper.java.null-collection-return")
+  static final List<String> parseNameTokensAsList(Object o)
+  {
+    if (o == null)
+      return null;
+
+    String stringValue = o.toString();
+    ArrayList<String> list = new ArrayList<String>(5);
+
+    int     length = stringValue.length();
+    boolean inSpace = true;
+    int     start = 0;
+    for (int i = 0; i < length; i++)
+    {
+      char ch = stringValue.charAt(i);
+
+      // We're in whitespace;  if we've just departed
+      // a run of non-whitespace, append a string.
+      // Now, why do we use the supposedly deprecated "Character.isSpace()"
+      // function instead of "isWhitespace"?  We're following XML rules
+      // here for the meaning of whitespace, which specifically
+      // EXCLUDES general Unicode spaces.
+      if (Character.isWhitespace(ch))
+      {
+        if (!inSpace)
+        {
+          list.add(stringValue.substring(start, i));
+          inSpace = true;
+        }
+      }
+      // We're out of whitespace;  if we've just departed
+      // a run of whitespace, start keeping track of this string
+      else
+      {
+        if (inSpace)
+        {
+          start = i;
+          inSpace = false;
+        }
+      }
+    }
+
+    if (!inSpace)
+      list.add(stringValue.substring(start));
+
+    if (list.isEmpty())
+      return null;
+
+    return list;
+  }
+
+  @SuppressWarnings("oracle.jdeveloper.java.null-collection-return")
+  static final Set<String> parseNameTokensAsSet (Object o)
+  {
+    List<String> list = parseNameTokensAsList(o);
+
+    if (list == null)
+      return null;
+    else
+      return new HashSet<String>(list);
+  }
+}

Propchange: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/TagUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentELTag.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentELTag.java?rev=1391596&r1=1391595&r2=1391596&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentELTag.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentELTag.java Fri Sep 28 19:00:51 2012
@@ -22,12 +22,8 @@ import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 
-import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
 import java.util.TimeZone;
 
 import javax.el.MethodExpression;
@@ -131,16 +127,18 @@ abstract public class UIXComponentELTag 
 
     if (expression.isLiteralText())
     {
-      bean.setProperty(key, _parseNameTokens(expression.getValue(null)));
+      bean.setProperty(key, TagUtils.parseNameTokens(expression.getValue(null)));
     }
     else
     {
+      // Support coercion from a string to a string array
+      expression = new StringArrayValueExpression(expression);
       bean.setValueExpression(key, expression);
     }
   }
 
   /**
-   * Set a property of type java.util.List<java.lang.String>.  If the value
+   * Set a property of type java.util.List&lt;java.lang.String>.  If the value
    * is an EL expression, it will be stored as a ValueExpression.
    * Otherwise, it will parsed as a whitespace-separated series
    * of strings.
@@ -156,8 +154,7 @@ abstract public class UIXComponentELTag 
 
     if (expression.isLiteralText())
     {
-      bean.setProperty(key, 
-                       _parseNameTokensAsList(expression.getValue(null)));
+      bean.setProperty(key, TagUtils.parseNameTokensAsList(expression.getValue(null)));
     }
     else
     {
@@ -166,7 +163,7 @@ abstract public class UIXComponentELTag 
   }
 
   /**
-   * Set a property of type java.util.Set<java.lang.String>.  If the value
+   * Set a property of type java.util.Set&lt;java.lang.String>.  If the value
    * is an EL expression, it will be stored as a ValueExpression.
    * Otherwise, it will parsed as a whitespace-separated series
    * of strings.
@@ -182,8 +179,7 @@ abstract public class UIXComponentELTag 
 
     if (expression.isLiteralText())
     {
-      bean.setProperty(key, 
-                       _parseNameTokensAsSet(expression.getValue(null)));
+      bean.setProperty(key, TagUtils.parseNameTokensAsSet(expression.getValue(null)));
     }
     else
     {
@@ -209,7 +205,7 @@ abstract public class UIXComponentELTag 
     {
       Object value = expression.getValue(null);
       if (value != null)
-      { 
+      {
         if (value instanceof Number)
         {
           bean.setProperty(key, value);
@@ -250,7 +246,7 @@ abstract public class UIXComponentELTag 
       Object value = expression.getValue(null);
       if (value != null)
       {
-        String[] strings = _parseNameTokens(value);
+        String[] strings = TagUtils.parseNameTokens(value);
         final int[] ints;
         if (strings != null)
         {
@@ -339,7 +335,9 @@ abstract public class UIXComponentELTag 
     }
   }
 
-  protected void setProperties(FacesBean bean)
+  protected void setProperties(
+    @SuppressWarnings("unused")
+    FacesBean bean)
   {
     // Could be abstract, but it's easier to *always* call super.setProperties(),
     // and perhaps we'll have something generic in here, esp. if we take
@@ -378,85 +376,7 @@ abstract public class UIXComponentELTag 
     }
   }
 
-  /**
-   * Parses a whitespace separated series of name tokens.
-   * @param stringValue the full string
-   * @return an array of each constituent value, or null
-   *  if there are no tokens (that is, the string is empty or
-   *  all whitespace)
-   * @todo Move to utility function somewhere (ADF Share?)
-   */
-  static private final String[] _parseNameTokens(Object o)
-  {
-    List<String> list = _parseNameTokensAsList (o);
-
-    if (list == null)
-      return null;
-
-    return list.toArray(new String[list.size()]);
-  }
-
-  static private final List<String> _parseNameTokensAsList (Object o)
-  {
-    if (o == null)
-      return null;
-
-    String stringValue = o.toString();
-    ArrayList<String> list = new ArrayList<String>(5);
-
-    int     length = stringValue.length();
-    boolean inSpace = true;
-    int     start = 0;
-    for (int i = 0; i < length; i++)
-    {
-      char ch = stringValue.charAt(i);
-
-      // We're in whitespace;  if we've just departed
-      // a run of non-whitespace, append a string.
-      // Now, why do we use the supposedly deprecated "Character.isSpace()"
-      // function instead of "isWhitespace"?  We're following XML rules
-      // here for the meaning of whitespace, which specifically
-      // EXCLUDES general Unicode spaces.
-      if (Character.isWhitespace(ch))
-      {
-        if (!inSpace)
-        {
-          list.add(stringValue.substring(start, i));
-          inSpace = true;
-        }
-      }
-      // We're out of whitespace;  if we've just departed
-      // a run of whitespace, start keeping track of this string
-      else
-      {
-        if (inSpace)
-        {
-          start = i;
-          inSpace = false;
-        }
-      }
-    }
-
-    if (!inSpace)
-      list.add(stringValue.substring(start));
-
-    if (list.isEmpty())
-      return null;
-
-    return list;
-  }
-
-  static private final Set<String> _parseNameTokensAsSet (Object o)
-  {
-    List<String> list = _parseNameTokensAsList(o);
-
-    if (list == null)
-      return null;
-    else
-      return new HashSet(list);
-  }
-
-  private static final TrinidadLogger _LOG = 
+  private static final TrinidadLogger _LOG =
     TrinidadLogger.createTrinidadLogger(UIXComponentELTag.class);
 
   // We rely strictly on ISO 8601 formats
@@ -475,5 +395,4 @@ abstract public class UIXComponentELTag 
 
   private MethodExpression  _attributeChangeListener;
   private String            _validationError;
-  
 }

Modified: myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentTag.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentTag.java?rev=1391596&r1=1391595&r2=1391596&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentTag.java (original)
+++ myfaces/trinidad/trunk/trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/UIXComponentTag.java Fri Sep 28 19:00:51 2012
@@ -22,7 +22,6 @@ import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 
-import java.util.ArrayList;
 import java.util.Date;
 
 import javax.el.MethodExpression;
@@ -47,6 +46,7 @@ import org.apache.myfaces.trinidad.loggi
  * Subclass of UIComponentTag to add convenience methods,
  * and optimize where appropriate.
  */
+@SuppressWarnings("deprecation")
 abstract public class UIXComponentTag extends UIComponentTag
 {
   public UIXComponentTag()
@@ -116,7 +116,7 @@ abstract public class UIXComponentTag ex
     }
 
     super.setProperties(component);
-    
+
     UIXComponent uixComponent = (UIXComponent) component;
 
     if (_attributeChangeListener != null)
@@ -131,7 +131,6 @@ abstract public class UIXComponentTag ex
       uixComponent.setAttributeChangeListener(me);
     }
 
-
     setProperties(uixComponent.getFacesBean());
   }
 
@@ -204,12 +203,12 @@ abstract public class UIXComponentTag ex
     }
   }
  /**
-  * Set a property of type java.lang.Integer.  If the value
-  * is an EL expression, it will be stored as a ValueBinding.
-  * Otherwise, it will parsed with Integer.valueOf().
-  * Null values are ignored.
-  */
- protected void setIntegerProperty(
+   * Set a property of type java.lang.Integer.  If the value
+   * is an EL expression, it will be stored as a ValueBinding.
+   * Otherwise, it will parsed with Integer.valueOf().
+   * Null values are ignored.
+   */
+  protected void setIntegerProperty(
    FacesBean   bean,
    PropertyKey key,
    String      value)
@@ -349,7 +348,7 @@ abstract public class UIXComponentTag ex
     }
     else
     {
-      bean.setProperty(key, _parseNameTokens(value));
+      bean.setProperty(key, TagUtils.parseNameTokens(value));
     }
   }
 
@@ -374,7 +373,7 @@ abstract public class UIXComponentTag ex
     }
     else
     {
-      String[] strings = _parseNameTokens(value);
+      String[] strings = TagUtils.parseNameTokens(value);
       final int[] ints;
       if (strings != null)
       {
@@ -425,7 +424,6 @@ abstract public class UIXComponentTag ex
     }
   }
 
-
   // TODO Handle syntax exceptions gracefully?
   protected final ValueBinding createValueBinding(String string)
   {
@@ -447,7 +445,9 @@ abstract public class UIXComponentTag ex
                                                                   types);
   }
 
-  protected void setProperties(FacesBean bean)
+  protected void setProperties(
+    @SuppressWarnings("unused")
+    FacesBean bean)
   {
     // Could be abstract, but it's easier to *always* call super.setProperties(),
     // and perhaps we'll have something generic in here, esp. if we take
@@ -482,63 +482,6 @@ abstract public class UIXComponentTag ex
     }
   }
 
-  /**
-   * Parses a whitespace separated series of name tokens.
-   * @param stringValue the full string
-   * @return an array of each constituent value, or null
-   *  if there are no tokens (that is, the string is empty or
-   *  all whitespace)
-   */
-  // TODO Move to utility function somewhere 
-  static private final String[] _parseNameTokens(String stringValue)
-  {
-    if (stringValue == null)
-      return null;
-
-    ArrayList<String> list = new ArrayList<String>(5);
-
-    int     length = stringValue.length();
-    boolean inSpace = true;
-    int     start = 0;
-    for (int i = 0; i < length; i++)
-    {
-      char ch = stringValue.charAt(i);
-
-      // We're in whitespace;  if we've just departed
-      // a run of non-whitespace, append a string.
-      // Now, why do we use the supposedly deprecated "Character.isSpace()"
-      // function instead of "isWhitespace"?  We're following XML rules
-      // here for the meaning of whitespace, which specifically
-      // EXCLUDES general Unicode spaces.
-      if (Character.isWhitespace(ch))
-      {
-        if (!inSpace)
-        {
-          list.add(stringValue.substring(start, i));
-          inSpace = true;
-        }
-      }
-      // We're out of whitespace;  if we've just departed
-      // a run of whitespace, start keeping track of this string
-      else
-      {
-        if (inSpace)
-        {
-          start = i;
-          inSpace = false;
-        }
-      }
-    }
-
-    if (!inSpace)
-      list.add(stringValue.substring(start));
-
-    if (list.isEmpty())
-      return null;
-
-    return list.toArray(new String[list.size()]);
-  }
-
   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(UIXComponentTag.class);
 
   // We rely strictly on ISO 8601 formats