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