1 /*
2 * Copyright 2005 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package javax.faces.component;
17
18 import java.util.Arrays;
19 import java.util.Iterator;
20
21 import javax.faces.context.FacesContext;
22 import javax.faces.model.SelectItem;
23 import javax.faces.model.SelectItemGroup;
24
25 /**
26 * @author Mathias Broekelmann (latest modification by $Author: mbr $)
27 * @version $Revision: 320684 $ $Date: 2005-10-13 04:05:06 -0400 (Thu, 13 Oct 2005) $
28 */
29 class _SelectItemsUtil
30 {
31 public static interface _ValueConverter
32 {
33 Object getConvertedValue(FacesContext context, String value);
34 }
35
36 /**
37 * @param context the faces context
38 * @param value the value to check
39 * @param converter
40 * @param iterator contains instances of SelectItem
41 * @return if the value of a selectitem is equal to the given value
42 */
43 public static boolean matchValue(FacesContext context, Object value,
44 Iterator selectItemsIter, _ValueConverter converter)
45 {
46 while (selectItemsIter.hasNext())
47 {
48 SelectItem item = (SelectItem) selectItemsIter.next();
49 if (item instanceof SelectItemGroup)
50 {
51 SelectItemGroup itemgroup = (SelectItemGroup) item;
52 SelectItem[] selectItems = itemgroup.getSelectItems();
53 if (selectItems != null
54 && selectItems.length > 0
55 && matchValue(context, value, Arrays.asList(
56 selectItems).iterator(), converter))
57 {
58 return true;
59 }
60 }
61 else
62 {
63 Object itemValue = item.getValue();
64 if(converter != null && itemValue instanceof String)
65 {
66 itemValue = converter.getConvertedValue(context, (String)itemValue);
67 }
68 if (value.equals(itemValue))
69 {
70 return true;
71 }
72 }
73 }
74 return false;
75 }
76 }