*/
@Override
public int doEndTag() throws JspException {
// Acquire the select tag we are associated with
SelectTag selectTag = (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
if (selectTag == null) {
throw new JspException(messages.getMessage("optionsTag.select"));
}
StringBuilder sb = new StringBuilder();
// If a collection was specified, use that mode to render options
if (collection != null) {
Iterator collIterator = getIterator(collection, null);
while (collIterator.hasNext()) {
Object bean = collIterator.next();
Object value = null;
Object label = null;
try {
value = PropertyUtils.getProperty(bean, property);
if (value == null) {
value = "";
}
} catch (IllegalAccessException e) {
throw new JspException(messages.getMessage("getter.access", property, collection));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(messages.getMessage("getter.result", property, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(messages.getMessage("getter.method", property, collection));
}
try {
if (labelProperty != null) {
label = PropertyUtils.getProperty(bean, labelProperty);
} else {
label = value;
}
if (label == null) {
label = "";
}
} catch (IllegalAccessException e) {
throw new JspException(messages.getMessage("getter.access", labelProperty, collection));
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
throw new JspException(messages.getMessage("getter.result", labelProperty, t.toString()));
} catch (NoSuchMethodException e) {
throw new JspException(messages.getMessage("getter.method", labelProperty, collection));
}
String stringValue = value.toString();
addOption(sb, stringValue, label.toString(), selectTag.isMatched(stringValue));
}
}
// Otherwise, use the separate iterators mode to render options
else {
// Construct iterators for the values and labels collections
Iterator valuesIterator = getIterator(name, property);
Iterator labelsIterator = null;
if ((labelName == null) && (labelProperty == null)) {
labelsIterator = getIterator(name, property); // Same coll.
} else {
labelsIterator = getIterator(labelName, labelProperty);
}
// Render the options tags for each element of the values coll.
while (valuesIterator.hasNext()) {
String value = valuesIterator.next().toString();
String label = value;
if (labelsIterator.hasNext()) {
label = labelsIterator.next().toString();
}
addOption(sb, value, label, selectTag.isMatched(value));
}
}
// Render this element to our writer
ResponseUtils.write(pageContext, sb.toString());