1 /*
2 * $Id: ListUIBean.java 497654 2007-01-19 00:21:57Z rgielen $
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 package org.apache.struts2.components;
22
23 import java.lang.reflect.Array;
24 import java.util.Collection;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.struts2.views.annotations.StrutsTagAttribute;
31 import org.apache.struts2.util.ContainUtil;
32 import org.apache.struts2.util.MakeIterator;
33
34 import com.opensymphony.xwork2.util.ValueStack;
35
36 /**
37 * DoubleListUIBean is the standard superclass of all Struts list handling components.
38 *
39 * <p/>
40 *
41 * <!-- START SNIPPET: javadoc -->
42 *
43 * Note that the listkey and listvalue attribute will default to "key" and "value"
44 * respectively only when the list attribute is evaluated to a Map or its decendant.
45 * Other thing else, will result in listkey and listvalue to be null and not used.
46 *
47 * <!-- END SNIPPET: javadoc -->
48 *
49 */
50 public abstract class ListUIBean extends UIBean {
51 protected Object list;
52 protected String listKey;
53 protected String listValue;
54
55 // indicate if an exception is to be thrown when value attribute is null
56 protected boolean throwExceptionOnNullValueAttribute = false;
57
58 protected ListUIBean(ValueStack stack, HttpServletRequest request, HttpServletResponse response) {
59 super(stack, request, response);
60 }
61
62 public void evaluateExtraParams() {
63 Object value = null;
64
65 if (list == null) {
66 list = parameters.get("list");
67 }
68
69 if (list instanceof String) {
70 value = findValue((String) list);
71 } else if (list instanceof Collection) {
72 value = list;
73 } else if (MakeIterator.isIterable(list)) {
74 value = MakeIterator.convert(list);
75 }
76 if (value == null) {
77 if (throwExceptionOnNullValueAttribute) {
78 // will throw an exception if not found
79 value = findValue((list == null) ? (String) list : list.toString(), "list",
80 "The requested list key '" + list + "' could not be resolved as a collection/array/map/enumeration/iterator type. " +
81 "Example: people or people.{name}");
82 }
83 else {
84 // ww-1010, allows value with null value to be compatible with ww
85 // 2.1.7 behaviour
86 value = findValue((list == null)?(String) list:list.toString());
87 }
88 }
89
90 if (value instanceof Collection) {
91 addParameter("list", value);
92 } else {
93 addParameter("list", MakeIterator.convert(value));
94 }
95
96 if (value instanceof Collection) {
97 addParameter("listSize", new Integer(((Collection) value).size()));
98 } else if (value instanceof Map) {
99 addParameter("listSize", new Integer(((Map) value).size()));
100 } else if (value != null && value.getClass().isArray()) {
101 addParameter("listSize", new Integer(Array.getLength(value)));
102 }
103
104 if (listKey != null) {
105 addParameter("listKey", listKey);
106 } else if (value instanceof Map) {
107 addParameter("listKey", "key");
108 }
109
110 if (listValue != null) {
111 if (altSyntax()) {
112 // the same logic as with findValue(String)
113 // if value start with %{ and end with }, just cut it off!
114 if (listValue.startsWith("%{") && listValue.endsWith("}")) {
115 listValue = listValue.substring(2, listValue.length() - 1);
116 }
117 }
118 addParameter("listValue", listValue);
119 } else if (value instanceof Map) {
120 addParameter("listValue", "value");
121 }
122 }
123
124 public boolean contains(Object obj1, Object obj2) {
125 return ContainUtil.contains(obj1, obj2);
126 }
127
128 protected Class getValueClassType() {
129 return null; // don't convert nameValue to anything, we need the raw value
130 }
131
132 @StrutsTagAttribute(description="Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value'" +
133 " parameter and the Map value will become the option body.", required=true)
134 public void setList(Object list) {
135 this.list = list;
136 }
137
138 @StrutsTagAttribute(description=" Property of list objects to get field value from")
139 public void setListKey(String listKey) {
140 this.listKey = listKey;
141 }
142
143 @StrutsTagAttribute(description="Property of list objects to get field content from")
144 public void setListValue(String listValue) {
145 this.listValue = listValue;
146 }
147
148
149 public void setThrowExceptionOnNullValueAttribute(boolean throwExceptionOnNullValueAttribute) {
150 this.throwExceptionOnNullValueAttribute = throwExceptionOnNullValueAttribute;
151 }
152 }