View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.validator;
20  
21  import javax.el.ValueExpression;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.el.ValueBinding;
25  
26  import org.apache.myfaces.trinidad.bean.FacesBean;
27  import org.apache.myfaces.trinidad.bean.FacesBeanImpl;
28  import org.apache.myfaces.trinidad.bean.PropertyKey;
29  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
30  
31  /**
32   * Utility class for Validators. 
33   * 
34   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/validator/ValidatorUtils.java#0 $) $Date: 10-nov-2005.19:08:35 $
35   */
36  class ValidatorUtils 
37  {
38    private ValidatorUtils()
39    {
40    }  
41    
42    /**
43     * Checks if the parameter is of String type. If not throws a 
44     * IllegalArgumentException
45     * @param value value to the validated for String type
46     * @param message The message to be used to create IllegalArgumentException
47     */
48    static final void assertIsString(   
49      Object value,
50      String message) throws IllegalArgumentException
51    {
52      if (!(value instanceof String))
53        throw new IllegalArgumentException(message);         
54    }
55    
56    static Object getComponentLabel(UIComponent component)
57    { 
58        Object label = component.getAttributes().get("label");
59        if ( null == label)
60         label = component.getValueBinding("label");
61         
62        return label;
63    }  
64    
65    static FacesBean getFacesBean(final FacesBean.Type type)
66    {
67      FacesBeanImpl bean = new FacesBeanImpl()
68                             {
69                               @Override
70                               public FacesBean.Type getType()
71                               {
72                                 return type;
73                               }
74                             };
75      return bean;
76    }  
77   
78    static void setValueExpression(FacesBean bean, String name, ValueExpression expression)
79    {   
80      PropertyKey key = _getPropertyKey(bean, name, true);
81      bean.setValueExpression(key, expression);
82    }
83   
84    static ValueExpression getValueExpression(FacesBean bean, String name)
85    {
86      PropertyKey key = _getPropertyKey(bean, name, true);
87      return bean.getValueExpression(key);
88    }
89  
90    static void setValueBinding(FacesBean bean, String name, ValueBinding binding)
91    {   
92      PropertyKey key = _getPropertyKey(bean, name, true);
93      bean.setValueBinding(key, binding);
94    }
95   
96    static ValueBinding getValueBinding(FacesBean bean, String name)
97    {
98      PropertyKey key = _getPropertyKey(bean, name, true);
99      return bean.getValueBinding(key);
100   }
101   
102   static boolean equals(Object o1, Object o2)
103   {
104     return ( o1 == o2 || (o1 != null && o1.equals(o2)));
105   }
106   
107   private static PropertyKey _getPropertyKey(
108     FacesBean bean, 
109     String name,  
110     boolean isStrict)
111   {   
112     _assertNotNull(name, "attribute cannot be null");
113     FacesBean.Type type = bean.getType();
114     PropertyKey key = type.findKey(name);
115     if (isStrict && key == null)
116      throw new IllegalArgumentException(_LOG.getMessage(
117        "INVALID_ATTRIBUTE_NAME", name));
118     else 
119     return key;
120   }
121 
122   private static void _assertNotNull(Object object, String message)
123   {
124     if (object == null)
125     {
126        if (message == null)
127          throw new NullPointerException();
128        else 
129          throw new NullPointerException(message);
130     }
131   }
132   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
133     ValidatorUtils.class);
134 }