1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36 class ValidatorUtils
37 {
38 private ValidatorUtils()
39 {
40 }
41
42
43
44
45
46
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 }