001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.webwork.util.ContainUtil;
004: import com.opensymphony.webwork.util.MakeIterator;
005: import com.opensymphony.xwork.util.OgnlValueStack;
006:
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009: import java.lang.reflect.Array;
010: import java.util.Collection;
011: import java.util.Map;
012:
013: /**
014: * DoubleListUIBean is the standard superclass of all webwork list handling components.
015: *
016: * <p/>
017: *
018: * <!-- START SNIPPET: javadoc -->
019: *
020: * Note that the listkey and listvalue attribute will default to "key" and "value"
021: * respectively only when the list attribute is evaluated to a Map or its decendant.
022: * Other thing else, will result in listkey and listvalue to be null and not used.
023: *
024: * <!-- END SNIPPET: javadoc -->
025: *
026: *
027: * @author Patrick Lightbody
028: * @author Rene Gielen
029: * @version $Revision: 2699 $
030: * @since 2.2
031: */
032: public abstract class ListUIBean extends UIBean {
033: protected Object list;
034: protected String listKey;
035: protected String listValue;
036:
037: // indicate if an exception is to be thrown when value attribute is null
038: protected boolean throwExceptionOnNullValueAttribute = false;
039:
040: protected ListUIBean(OgnlValueStack stack,
041: HttpServletRequest request, HttpServletResponse response) {
042: super (stack, request, response);
043: }
044:
045: public void evaluateExtraParams() {
046: Object value = null;
047:
048: if (list == null) {
049: list = parameters.get("list");
050: }
051:
052: if (list instanceof String) {
053: value = findValue((String) list);
054: } else if (list instanceof Collection) {
055: value = list;
056: } else if (MakeIterator.isIterable(list)) {
057: value = MakeIterator.convert(list);
058: }
059: if (value == null) {
060: if (throwExceptionOnNullValueAttribute) {
061: // will throw an exception if not found
062: value = findValue(
063: (list == null) ? (String) list : list
064: .toString(),
065: "list",
066: "The requested list key '"
067: + list
068: + "' could not be resolved as a collection/array/map/enumeration/iterator type. "
069: + "Example: people or people.{name}");
070: } else {
071: // ww-1010, allows value with null value to be compatible with ww
072: // 2.1.7 behaviour
073: value = findValue((list == null) ? (String) list : list
074: .toString());
075: }
076: }
077:
078: if (value instanceof Collection) {
079: addParameter("list", value);
080: } else {
081: addParameter("list", MakeIterator.convert(value));
082: }
083:
084: if (value instanceof Collection) {
085: addParameter("listSize", new Integer(((Collection) value)
086: .size()));
087: } else if (value instanceof Map) {
088: addParameter("listSize", new Integer(((Map) value).size()));
089: } else if (value != null && value.getClass().isArray()) {
090: addParameter("listSize",
091: new Integer(Array.getLength(value)));
092: }
093:
094: if (listKey != null) {
095: addParameter("listKey", listKey);
096: } else if (value instanceof Map) {
097: addParameter("listKey", "key");
098: }
099:
100: if (listValue != null) {
101:
102: if (altSyntax()) {
103: // the same logic as with findValue(String)
104: // if value start with %{ and end with }, just cut it off!
105: if (listValue.startsWith("%{")
106: && listValue.endsWith("}")) {
107: listValue = listValue.substring(2, listValue
108: .length() - 1);
109: }
110: }
111:
112: addParameter("listValue", listValue);
113: } else if (value instanceof Map) {
114: addParameter("listValue", "value");
115: }
116: }
117:
118: public boolean contains(Object obj1, Object obj2) {
119: return ContainUtil.contains(obj1, obj2);
120: }
121:
122: protected Class getValueClassType() {
123: return null; // don't convert nameValue to anything, we need the raw value
124: }
125:
126: /**
127: * 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.
128: * @ww.tagattribute required="true"
129: */
130: public void setList(Object list) {
131: this .list = list;
132: }
133:
134: /**
135: * Property of list objects to get field value from
136: * @ww.tagattribute required="false"
137: */
138: public void setListKey(String listKey) {
139: this .listKey = listKey;
140: }
141:
142: /**
143: * Property of list objects to get field content from
144: * @ww.tagattribute required="false"
145: */
146: public void setListValue(String listValue) {
147: this .listValue = listValue;
148: }
149:
150: public void setThrowExceptionOnNullValueAttribute(
151: boolean throwExceptionOnNullValueAttribute) {
152: this.throwExceptionOnNullValueAttribute = throwExceptionOnNullValueAttribute;
153: }
154: }
|