001: /*
002: * $Id: OptionsTag.java 471754 2006-11-06 14:55:09Z husted $
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.struts.taglib.html;
022:
023: import org.apache.commons.beanutils.PropertyUtils;
024: import org.apache.struts.taglib.TagUtils;
025: import org.apache.struts.util.IteratorAdapter;
026: import org.apache.struts.util.MessageResources;
027:
028: import javax.servlet.jsp.JspException;
029: import javax.servlet.jsp.tagext.TagSupport;
030:
031: import java.lang.reflect.InvocationTargetException;
032:
033: import java.util.Arrays;
034: import java.util.Collection;
035: import java.util.Enumeration;
036: import java.util.Iterator;
037: import java.util.Map;
038:
039: /**
040: * Tag for creating multiple <select> options from a collection. The
041: * associated values displayed to the user may optionally be specified by a
042: * second collection, or will be the same as the values themselves. Each
043: * collection may be an array of objects, a Collection, an Enumeration, an
044: * Iterator, or a Map. <b>NOTE</b> - This tag requires a Java2 (JDK 1.2 or
045: * later) platform.
046: */
047: public class OptionsTag extends TagSupport {
048: /**
049: * The message resources for this package.
050: */
051: protected static MessageResources messages = MessageResources
052: .getMessageResources(Constants.Package + ".LocalStrings");
053:
054: /**
055: * The name of the collection containing beans that have properties to
056: * provide both the values and the labels (identified by the
057: * <code>property</code> and <code>labelProperty</code> attributes).
058: */
059: protected String collection = null;
060:
061: /**
062: * Should the label values be filtered for HTML sensitive characters?
063: */
064: protected boolean filter = true;
065:
066: /**
067: * The name of the bean containing the labels collection.
068: */
069: protected String labelName = null;
070:
071: /**
072: * The bean property containing the labels collection.
073: */
074: protected String labelProperty = null;
075:
076: /**
077: * The name of the bean containing the values collection.
078: */
079: protected String name = null;
080:
081: /**
082: * The name of the property to use to build the values collection.
083: */
084: protected String property = null;
085:
086: /**
087: * The style associated with this tag.
088: */
089: private String style = null;
090:
091: /**
092: * The named style class associated with this tag.
093: */
094: private String styleClass = null;
095:
096: public String getCollection() {
097: return (this .collection);
098: }
099:
100: public void setCollection(String collection) {
101: this .collection = collection;
102: }
103:
104: public boolean getFilter() {
105: return filter;
106: }
107:
108: public void setFilter(boolean filter) {
109: this .filter = filter;
110: }
111:
112: public String getLabelName() {
113: return labelName;
114: }
115:
116: public void setLabelName(String labelName) {
117: this .labelName = labelName;
118: }
119:
120: public String getLabelProperty() {
121: return labelProperty;
122: }
123:
124: public void setLabelProperty(String labelProperty) {
125: this .labelProperty = labelProperty;
126: }
127:
128: public String getName() {
129: return name;
130: }
131:
132: public void setName(String name) {
133: this .name = name;
134: }
135:
136: public String getProperty() {
137: return property;
138: }
139:
140: public void setProperty(String property) {
141: this .property = property;
142: }
143:
144: public String getStyle() {
145: return style;
146: }
147:
148: public void setStyle(String style) {
149: this .style = style;
150: }
151:
152: public String getStyleClass() {
153: return styleClass;
154: }
155:
156: public void setStyleClass(String styleClass) {
157: this .styleClass = styleClass;
158: }
159:
160: /**
161: * Process the start of this tag.
162: *
163: * @throws JspException if a JSP exception has occurred
164: */
165: public int doStartTag() throws JspException {
166: return SKIP_BODY;
167: }
168:
169: /**
170: * Process the end of this tag.
171: *
172: * @throws JspException if a JSP exception has occurred
173: */
174: public int doEndTag() throws JspException {
175: // Acquire the select tag we are associated with
176: SelectTag selectTag = (SelectTag) pageContext
177: .getAttribute(Constants.SELECT_KEY);
178:
179: if (selectTag == null) {
180: throw new JspException(messages
181: .getMessage("optionsTag.select"));
182: }
183:
184: StringBuffer sb = new StringBuffer();
185:
186: // If a collection was specified, use that mode to render options
187: if (collection != null) {
188: Iterator collIterator = getIterator(collection, null);
189:
190: while (collIterator.hasNext()) {
191: Object bean = collIterator.next();
192: Object value = null;
193: Object label = null;
194:
195: try {
196: value = PropertyUtils.getProperty(bean, property);
197:
198: if (value == null) {
199: value = "";
200: }
201: } catch (IllegalAccessException e) {
202: throw new JspException(messages.getMessage(
203: "getter.access", property, collection));
204: } catch (InvocationTargetException e) {
205: Throwable t = e.getTargetException();
206:
207: throw new JspException(messages.getMessage(
208: "getter.result", property, t.toString()));
209: } catch (NoSuchMethodException e) {
210: throw new JspException(messages.getMessage(
211: "getter.method", property, collection));
212: }
213:
214: try {
215: if (labelProperty != null) {
216: label = PropertyUtils.getProperty(bean,
217: labelProperty);
218: } else {
219: label = value;
220: }
221:
222: if (label == null) {
223: label = "";
224: }
225: } catch (IllegalAccessException e) {
226: throw new JspException(messages.getMessage(
227: "getter.access", labelProperty, collection));
228: } catch (InvocationTargetException e) {
229: Throwable t = e.getTargetException();
230:
231: throw new JspException(messages.getMessage(
232: "getter.result", labelProperty, t
233: .toString()));
234: } catch (NoSuchMethodException e) {
235: throw new JspException(messages.getMessage(
236: "getter.method", labelProperty, collection));
237: }
238:
239: String stringValue = value.toString();
240:
241: addOption(sb, stringValue, label.toString(), selectTag
242: .isMatched(stringValue));
243: }
244: }
245: // Otherwise, use the separate iterators mode to render options
246: else {
247: // Construct iterators for the values and labels collections
248: Iterator valuesIterator = getIterator(name, property);
249: Iterator labelsIterator = null;
250:
251: if ((labelName != null) || (labelProperty != null)) {
252: labelsIterator = getIterator(labelName, labelProperty);
253: }
254:
255: // Render the options tags for each element of the values coll.
256: while (valuesIterator.hasNext()) {
257: Object valueObject = valuesIterator.next();
258:
259: if (valueObject == null) {
260: valueObject = "";
261: }
262:
263: String value = valueObject.toString();
264: String label = value;
265:
266: if ((labelsIterator != null)
267: && labelsIterator.hasNext()) {
268: Object labelObject = labelsIterator.next();
269:
270: if (labelObject == null) {
271: labelObject = "";
272: }
273:
274: label = labelObject.toString();
275: }
276:
277: addOption(sb, value, label, selectTag.isMatched(value));
278: }
279: }
280:
281: TagUtils.getInstance().write(pageContext, sb.toString());
282:
283: return EVAL_PAGE;
284: }
285:
286: /**
287: * Release any acquired resources.
288: */
289: public void release() {
290: super .release();
291: collection = null;
292: filter = true;
293: labelName = null;
294: labelProperty = null;
295: name = null;
296: property = null;
297: style = null;
298: styleClass = null;
299: }
300:
301: // ------------------------------------------------------ Protected Methods
302:
303: /**
304: * Add an option element to the specified StringBuffer based on the
305: * specified parameters. <p> Note that this tag specifically does not
306: * support the <code>styleId</code> tag attribute, which causes the HTML
307: * <code>id</code> attribute to be emitted. This is because the HTML
308: * specification states that all "id" attributes in a document have to be
309: * unique. This tag will likely generate more than one
310: * <code>option</code> element element, but it cannot use the same
311: * <code>id</code> value. It's conceivable some sort of mechanism to
312: * supply an array of <code>id</code> values could be devised, but that
313: * doesn't seem to be worth the trouble.
314: *
315: * @param sb StringBuffer accumulating our results
316: * @param value Value to be returned to the server for this option
317: * @param label Value to be shown to the user for this option
318: * @param matched Should this value be marked as selected?
319: */
320: protected void addOption(StringBuffer sb, String value,
321: String label, boolean matched) {
322: sb.append("<option value=\"");
323:
324: if (filter) {
325: sb.append(TagUtils.getInstance().filter(value));
326: } else {
327: sb.append(value);
328: }
329:
330: sb.append("\"");
331:
332: if (matched) {
333: sb.append(" selected=\"selected\"");
334: }
335:
336: if (style != null) {
337: sb.append(" style=\"");
338: sb.append(style);
339: sb.append("\"");
340: }
341:
342: if (styleClass != null) {
343: sb.append(" class=\"");
344: sb.append(styleClass);
345: sb.append("\"");
346: }
347:
348: sb.append(">");
349:
350: if (filter) {
351: sb.append(TagUtils.getInstance().filter(label));
352: } else {
353: sb.append(label);
354: }
355:
356: sb.append("</option>\r\n");
357: }
358:
359: /**
360: * Return an iterator for the option labels or values, based on our
361: * configured properties.
362: *
363: * @param name Name of the bean attribute (if any)
364: * @param property Name of the bean property (if any)
365: * @throws JspException if an error occurs
366: */
367: protected Iterator getIterator(String name, String property)
368: throws JspException {
369: // Identify the bean containing our collection
370: String beanName = name;
371:
372: if (beanName == null) {
373: beanName = Constants.BEAN_KEY;
374: }
375:
376: Object bean = TagUtils.getInstance().lookup(pageContext,
377: beanName, null);
378:
379: if (bean == null) {
380: throw new JspException(messages.getMessage("getter.bean",
381: beanName));
382: }
383:
384: // Identify the collection itself
385: Object collection = bean;
386:
387: if (property != null) {
388: try {
389: collection = PropertyUtils.getProperty(bean, property);
390:
391: if (collection == null) {
392: throw new JspException(messages.getMessage(
393: "getter.property", property));
394: }
395: } catch (IllegalAccessException e) {
396: throw new JspException(messages.getMessage(
397: "getter.access", property, name));
398: } catch (InvocationTargetException e) {
399: Throwable t = e.getTargetException();
400:
401: throw new JspException(messages.getMessage(
402: "getter.result", property, t.toString()));
403: } catch (NoSuchMethodException e) {
404: throw new JspException(messages.getMessage(
405: "getter.method", property, name));
406: }
407: }
408:
409: // Construct and return an appropriate iterator
410: if (collection.getClass().isArray()) {
411: collection = Arrays.asList((Object[]) collection);
412: }
413:
414: if (collection instanceof Collection) {
415: return (((Collection) collection).iterator());
416: } else if (collection instanceof Iterator) {
417: return ((Iterator) collection);
418: } else if (collection instanceof Map) {
419: return (((Map) collection).entrySet().iterator());
420: } else if (collection instanceof Enumeration) {
421: return new IteratorAdapter((Enumeration) collection);
422: } else {
423: throw new JspException(messages.getMessage(
424: "optionsTag.iterator", collection.toString()));
425: }
426: }
427: }
|