1 /*
2 * $Id: ListUIBean.java 726715 2008-12-15 15:39:15Z musachy $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.struts2.components;
23
24 import java.lang.reflect.Array;
25 import java.util.Collection;
26 import java.util.Map;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.apache.struts2.views.annotations.StrutsTagAttribute;
32 import org.apache.struts2.util.ContainUtil;
33 import org.apache.struts2.util.MakeIterator;
34
35 import com.opensymphony.xwork2.util.ValueStack;
36
37 /**
38 * DoubleListUIBean is the standard superclass of all Struts list handling components.
39 *
40 * <p/>
41 *
42 * <!-- START SNIPPET: javadoc -->
43 *
44 * Note that the listkey and listvalue attribute will default to "key" and "value"
45 * respectively only when the list attribute is evaluated to a Map or its decendant.
46 * Everything else will result in listkey and listvalue to be null and not used.
47 *
48 * <!-- END SNIPPET: javadoc -->
49 *
50 */
51 public abstract class ListUIBean extends UIBean {
52 protected Object list;
53 protected String listKey;
54 protected String listValue;
55
56 // indicate if an exception is to be thrown when value attribute is null
57 protected boolean throwExceptionOnNullValueAttribute = false;
58
59 protected ListUIBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
60 super(stack, request, response);
61 }
62
63 public void evaluateExtraParams() {
64 Object value = null;
65
66 if (list == null) {
67 list = parameters.get("list");
68 }
69
70 if (list instanceof String) {
71 value = findValue((String) list);
72 } else if (list instanceof Collection) {
73 value = list;
74 } else if (MakeIterator.isIterable(list)) {
75 value = MakeIterator.convert(list);
76 }
77 if (value == null) {
78 if (throwExceptionOnNullValueAttribute) {
79 // will throw an exception if not found
80 value = findValue((list == null) ? (String) list : list.toString(), "list",
81 "The requested list key '" + list + "' could not be resolved as a collection/array/map/enumeration/iterator type. " +
82 "Example: people or people.{name}");
83 }
84 else {
85 // ww-1010, allows value with null value to be compatible with ww
86 // 2.1.7 behaviour
87 value = findValue((list == null)?(String) list:list.toString());
88 }
89 }
90
91 if (value instanceof Collection) {
92 addParameter("list", value);
93 } else {
94 addParameter("list", MakeIterator.convert(value));
95 }
96
97 if (value instanceof Collection) {
98 addParameter("listSize", Integer.valueOf(((Collection) value).size()));
99 } else if (value instanceof Map) {
100 addParameter("listSize", Integer.valueOf(((Map) value).size()));
101 } else if (value != null && value.getClass().isArray()) {
102 addParameter("listSize", Integer.valueOf(Array.getLength(value)));
103 }
104
105 if (listKey != null) {
106 listKey = stripExpressionIfAltSyntax(listKey);
107 addParameter("listKey", listKey);
108 } else if (value instanceof Map) {
109 addParameter("listKey", "key");
110 }
111
112 if (listValue != null) {
113 listValue = stripExpressionIfAltSyntax(listValue);
114 addParameter("listValue", listValue);
115 } else if (value instanceof Map) {
116 addParameter("listValue", "value");
117 }
118 }
119
120 public boolean contains(Object obj1, Object obj2) {
121 return ContainUtil.contains(obj1, obj2);
122 }
123
124 protected Class getValueClassType() {
125 return null; // don't convert nameValue to anything, we need the raw value
126 }
127
128 @StrutsTagAttribute(description="Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value'" +
129 " parameter and the Map value will become the option body.", required=true)
130 public void setList(Object list) {
131 this.list = list;
132 }
133
134 @StrutsTagAttribute(description=" Property of list objects to get field value from")
135 public void setListKey(String listKey) {
136 this.listKey = listKey;
137 }
138
139 @StrutsTagAttribute(description="Property of list objects to get field content from")
140 public void setListValue(String listValue) {
141 this.listValue = listValue;
142 }
143
144
145 public void setThrowExceptionOnNullValueAttribute(boolean throwExceptionOnNullValueAttribute) {
146 this.throwExceptionOnNullValueAttribute = throwExceptionOnNullValueAttribute;
147 }
148 }