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.convert;
20  
21  import javax.el.ValueExpression;
22  import javax.faces.component.UIComponent;
23  import javax.faces.el.ValueBinding;
24  
25  import org.apache.myfaces.trinidad.bean.FacesBean;
26  import org.apache.myfaces.trinidad.bean.FacesBeanImpl;
27  import org.apache.myfaces.trinidad.bean.PropertyKey;
28  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
29  
30  final class ConverterUtils 
31  {
32    private ConverterUtils()
33    {
34    }
35    
36    static Object getComponentLabel(UIComponent component)
37    { 
38        Object label = component.getAttributes().get("label");
39        if ( null == label)
40         label = component.getValueBinding("label");
41         
42        return label;
43    }
44    
45    static boolean equals(Object o1, Object o2)
46    {
47      return ( o1 == o2 || (o1 != null && o1.equals(o2)));
48    }
49    
50    static FacesBean getFacesBean(final FacesBean.Type type)
51   {
52     FacesBeanImpl bean = new FacesBeanImpl()
53                             {
54                               @Override
55                               public FacesBean.Type getType()
56                               {
57                                 return type;
58                               }
59                             };
60     return bean;
61   } 
62   
63    static void setValueExpression(FacesBean bean, String name, ValueExpression expression)
64    {   
65      PropertyKey key = _getPropertyKey(bean, name, true);
66      bean.setValueExpression(key, expression);
67    }
68   
69    static ValueExpression getValueExpression(FacesBean bean, String name)
70    {
71      PropertyKey key = _getPropertyKey(bean, name, true);
72      return bean.getValueExpression(key);
73    }
74  
75  
76    static void setValueBinding(FacesBean bean, String name, ValueBinding binding)
77    {   
78      PropertyKey key = _getPropertyKey(bean, name, true);
79      bean.setValueBinding(key, binding);
80    }
81   
82    static ValueBinding getValueBinding(FacesBean bean, String name)
83    {
84      PropertyKey key = _getPropertyKey(bean, name, true);
85      return bean.getValueBinding(key);
86    }
87    
88    private static PropertyKey _getPropertyKey(
89      FacesBean bean, 
90      String name,  
91      boolean isStrict)
92    {   
93      _assertNotNull(name, "attribute cannot be null");
94      FacesBean.Type type = bean.getType();
95      PropertyKey key = type.findKey(name);
96      if (isStrict && key == null)
97        throw new IllegalArgumentException(_LOG.getMessage(
98          "INVALID_ATTRIBUTE_NAME", name));
99      else 
100      return key;
101   }
102   
103   private static void _assertNotNull(Object object, String message)
104   {
105     if (object == null)
106     {
107        if (message == null)
108          throw new NullPointerException();
109        else 
110          throw new NullPointerException(message);
111     }
112   }
113   
114   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
115     ConverterUtils.class);
116 }