001: /*
002: * $Id: ListUIBean.java 497654 2007-01-19 00:21:57Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.lang.reflect.Array;
024: import java.util.Collection;
025: import java.util.Map;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.struts2.views.annotations.StrutsTagAttribute;
031: import org.apache.struts2.util.ContainUtil;
032: import org.apache.struts2.util.MakeIterator;
033:
034: import com.opensymphony.xwork2.util.ValueStack;
035:
036: /**
037: * DoubleListUIBean is the standard superclass of all Struts list handling components.
038: *
039: * <p/>
040: *
041: * <!-- START SNIPPET: javadoc -->
042: *
043: * Note that the listkey and listvalue attribute will default to "key" and "value"
044: * respectively only when the list attribute is evaluated to a Map or its decendant.
045: * Other thing else, will result in listkey and listvalue to be null and not used.
046: *
047: * <!-- END SNIPPET: javadoc -->
048: *
049: */
050: public abstract class ListUIBean extends UIBean {
051: protected Object list;
052: protected String listKey;
053: protected String listValue;
054:
055: // indicate if an exception is to be thrown when value attribute is null
056: protected boolean throwExceptionOnNullValueAttribute = false;
057:
058: protected ListUIBean(ValueStack stack, HttpServletRequest request,
059: HttpServletResponse response) {
060: super (stack, request, response);
061: }
062:
063: public void evaluateExtraParams() {
064: Object value = null;
065:
066: if (list == null) {
067: list = parameters.get("list");
068: }
069:
070: if (list instanceof String) {
071: value = findValue((String) list);
072: } else if (list instanceof Collection) {
073: value = list;
074: } else if (MakeIterator.isIterable(list)) {
075: value = MakeIterator.convert(list);
076: }
077: if (value == null) {
078: if (throwExceptionOnNullValueAttribute) {
079: // will throw an exception if not found
080: value = findValue(
081: (list == null) ? (String) list : list
082: .toString(),
083: "list",
084: "The requested list key '"
085: + list
086: + "' could not be resolved as a collection/array/map/enumeration/iterator type. "
087: + "Example: people or people.{name}");
088: } else {
089: // ww-1010, allows value with null value to be compatible with ww
090: // 2.1.7 behaviour
091: value = findValue((list == null) ? (String) list : list
092: .toString());
093: }
094: }
095:
096: if (value instanceof Collection) {
097: addParameter("list", value);
098: } else {
099: addParameter("list", MakeIterator.convert(value));
100: }
101:
102: if (value instanceof Collection) {
103: addParameter("listSize", new Integer(((Collection) value)
104: .size()));
105: } else if (value instanceof Map) {
106: addParameter("listSize", new Integer(((Map) value).size()));
107: } else if (value != null && value.getClass().isArray()) {
108: addParameter("listSize",
109: new Integer(Array.getLength(value)));
110: }
111:
112: if (listKey != null) {
113: addParameter("listKey", listKey);
114: } else if (value instanceof Map) {
115: addParameter("listKey", "key");
116: }
117:
118: if (listValue != null) {
119: if (altSyntax()) {
120: // the same logic as with findValue(String)
121: // if value start with %{ and end with }, just cut it off!
122: if (listValue.startsWith("%{")
123: && listValue.endsWith("}")) {
124: listValue = listValue.substring(2, listValue
125: .length() - 1);
126: }
127: }
128: addParameter("listValue", listValue);
129: } else if (value instanceof Map) {
130: addParameter("listValue", "value");
131: }
132: }
133:
134: public boolean contains(Object obj1, Object obj2) {
135: return ContainUtil.contains(obj1, obj2);
136: }
137:
138: protected Class getValueClassType() {
139: return null; // don't convert nameValue to anything, we need the raw value
140: }
141:
142: @StrutsTagAttribute(description="Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value'" + " parameter and the Map value will become the option body.",required=true)
143: public void setList(Object list) {
144: this .list = list;
145: }
146:
147: @StrutsTagAttribute(description=" Property of list objects to get field value from")
148: public void setListKey(String listKey) {
149: this .listKey = listKey;
150: }
151:
152: @StrutsTagAttribute(description="Property of list objects to get field content from")
153: public void setListValue(String listValue) {
154: this .listValue = listValue;
155: }
156:
157: public void setThrowExceptionOnNullValueAttribute(
158: boolean throwExceptionOnNullValueAttribute) {
159: this.throwExceptionOnNullValueAttribute = throwExceptionOnNullValueAttribute;
160: }
161: }
|