1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }