dgraham 2003/01/31 21:30:28
Modified: src/share/org/apache/struts/taglib/html
OptionsCollectionTag.java OptionsTag.java
OptionTag.java
src/share/org/apache/struts/taglib/logic IterateTag.java
Log:
Formatting and if statement cleanup, no code changes.
Revision Changes Path
1.9 +44 -66
jakarta-struts/src/share/org/apache/struts/taglib/html/OptionsCollectionTag.java
Index: OptionsCollectionTag.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/OptionsCollectionTag.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- OptionsCollectionTag.java 1 Feb 2003 05:12:24 -0000 1.8
+++ OptionsCollectionTag.java 1 Feb 2003 05:30:28 -0000 1.9
@@ -77,7 +77,6 @@
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
-
/**
* Tag for creating multiple <select> options from a collection. The
* collection may be part of the enclosing form, or may be independent of
@@ -97,21 +96,16 @@
public class OptionsCollectionTag extends TagSupport {
-
// ----------------------------------------------------- Instance Variables
-
/**
* The message resources for this package.
*/
protected static MessageResources messages =
- MessageResources.getMessageResources(
- Constants.Package + ".LocalStrings");
-
+ MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
// ------------------------------------------------------------- Properties
-
/**
* Should the label values be filtered for HTML sensitive characters?
*/
@@ -125,7 +119,6 @@
this.filter = filter;
}
-
/**
* The name of the bean property containing the label.
*/
@@ -139,7 +132,6 @@
this.label = label;
}
-
/**
* The name of the bean containing the values collection.
*/
@@ -153,7 +145,6 @@
this.name = name;
}
-
/**
* The name of the property to use to build the values collection.
*/
@@ -167,7 +158,6 @@
this.property = property;
}
-
/**
* The style associated with this tag.
*/
@@ -181,7 +171,6 @@
this.style = style;
}
-
/**
* The named style class associated with this tag.
*/
@@ -195,7 +184,6 @@
this.styleClass = styleClass;
}
-
/**
* The name of the bean property containing the value.
*/
@@ -209,10 +197,8 @@
this.value = value;
}
-
// --------------------------------------------------------- Public Methods
-
/**
* Process the start of this tag.
*
@@ -221,23 +207,20 @@
public int doStartTag() throws JspException {
// Acquire the select tag we are associated with
- SelectTag selectTag =
- (SelectTag)pageContext.getAttribute(Constants.SELECT_KEY);
+ SelectTag selectTag = (SelectTag)
pageContext.getAttribute(Constants.SELECT_KEY);
if (selectTag == null) {
- JspException e = new JspException(
- messages.getMessage("optionsCollectionTag.select"));
+ JspException e = new
JspException(messages.getMessage("optionsCollectionTag.select"));
RequestUtils.saveException(pageContext, e);
throw e;
}
// Acquire the collection containing our options
- Object collection = RequestUtils.lookup(pageContext,
- name, property, null);
+ Object collection = RequestUtils.lookup(pageContext, name, property, null);
if (collection == null) {
- JspException e = new JspException(
- messages.getMessage("optionsCollectionTag.collection"));
+ JspException e =
+ new
JspException(messages.getMessage("optionsCollectionTag.collection"));
RequestUtils.saveException(pageContext, e);
throw e;
}
@@ -261,20 +244,19 @@
beanLabel = "";
}
} catch (IllegalAccessException e) {
- JspException jspe = new JspException(
- messages.getMessage("getter.access", label, bean));
+ JspException jspe =
+ new JspException(messages.getMessage("getter.access", label,
bean));
RequestUtils.saveException(pageContext, jspe);
throw jspe;
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
- JspException jspe = new JspException(
- messages.getMessage("getter.result", label,
- t.toString()));
+ JspException jspe =
+ new JspException(messages.getMessage("getter.result", label,
t.toString()));
RequestUtils.saveException(pageContext, jspe);
throw jspe;
} catch (NoSuchMethodException e) {
- JspException jspe = new JspException(
- messages.getMessage("getter.method", label, bean));
+ JspException jspe =
+ new JspException(messages.getMessage("getter.method", label,
bean));
RequestUtils.saveException(pageContext, jspe);
throw jspe;
}
@@ -286,20 +268,19 @@
beanValue = "";
}
} catch (IllegalAccessException e) {
- JspException jspe = new JspException(
- messages.getMessage("getter.access", value, bean));
+ JspException jspe =
+ new JspException(messages.getMessage("getter.access", value,
bean));
RequestUtils.saveException(pageContext, jspe);
throw jspe;
} catch (InvocationTargetException e) {
Throwable t = e.getTargetException();
- JspException jspe = new JspException(
- messages.getMessage("getter.result", value,
- t.toString()));
+ JspException jspe =
+ new JspException(messages.getMessage("getter.result", value,
t.toString()));
RequestUtils.saveException(pageContext, jspe);
throw jspe;
} catch (NoSuchMethodException e) {
- JspException jspe = new JspException(
- messages.getMessage("getter.method", value, bean));
+ JspException jspe =
+ new JspException(messages.getMessage("getter.method", value,
bean));
RequestUtils.saveException(pageContext, jspe);
throw jspe;
}
@@ -308,18 +289,15 @@
String stringValue = beanValue.toString();
// Render this option
- addOption(sb, stringLabel, stringValue,
- selectTag.isMatched(stringValue));
+ addOption(sb, stringLabel, stringValue,
selectTag.isMatched(stringValue));
}
// Render this element to our writer
ResponseUtils.write(pageContext, sb.toString());
return SKIP_BODY;
-
}
-
/**
* Release any acquired resources.
*/
@@ -334,10 +312,8 @@
value = "value";
}
-
// ------------------------------------------------------ Protected Methods
-
/**
* Add an option element to the specified StringBuffer based on the
* specified parameters.
@@ -356,14 +332,14 @@
* @param label Value to be shown to the user for this option
* @param matched Should this value be marked as selected?
*/
- protected void addOption(StringBuffer sb, String label, String value,
- boolean matched) {
+ protected void addOption(StringBuffer sb, String label, String value, boolean
matched) {
sb.append("<option value=\"");
sb.append(value);
sb.append("\"");
- if (matched)
+ if (matched) {
sb.append(" selected=\"selected\"");
+ }
if (style != null) {
sb.append(" style=\"");
sb.append(style);
@@ -384,7 +360,6 @@
}
-
/**
* Return an iterator for the options collection.
*
@@ -392,25 +367,28 @@
*
* @exception JspException if an error occurs
*/
- protected Iterator getIterator(Object collection)
- throws JspException {
+ protected Iterator getIterator(Object collection) throws JspException {
- if (collection.getClass().isArray())
+ if (collection.getClass().isArray()) {
collection = Arrays.asList((Object[]) collection);
+ }
- if (collection instanceof Collection)
- return (((Collection)collection).iterator());
- else if (collection instanceof Iterator)
- return ((Iterator)collection);
- else if (collection instanceof Map)
- return (((Map)collection).entrySet().iterator());
- else if (collection instanceof Enumeration)
- return IteratorUtils.asIterator((Enumeration)collection);
- else
- throw new JspException(
- messages.getMessage("optionsCollectionTag.iterator",
- collection.toString()));
+ if (collection instanceof Collection) {
+ return (((Collection) collection).iterator());
+ } else if (collection instanceof Iterator) {
+ return ((Iterator) collection);
+
+ } else if (collection instanceof Map) {
+ return (((Map) collection).entrySet().iterator());
+
+ } else if (collection instanceof Enumeration) {
+ return IteratorUtils.asIterator((Enumeration) collection);
+
+ } else {
+ throw new JspException(
+ messages.getMessage("optionsCollectionTag.iterator",
collection.toString()));
+ }
}
}
1.22 +132 -151
jakarta-struts/src/share/org/apache/struts/taglib/html/OptionsTag.java
Index: OptionsTag.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/OptionsTag.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- OptionsTag.java 1 Feb 2003 05:12:25 -0000 1.21
+++ OptionsTag.java 1 Feb 2003 05:30:28 -0000 1.22
@@ -77,7 +77,6 @@
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
-
/**
* Tag for creating multiple <select> options from a collection. The
* associated values displayed to the user may optionally be specified by a
@@ -96,7 +95,7 @@
* The message resources for this package.
*/
protected static MessageResources messages =
- MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
+ MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
/**
* The name of the collection containing beans that have properties to
@@ -113,7 +112,6 @@
this.collection = collection;
}
-
/**
* Should the label values be filtered for HTML sensitive characters?
*/
@@ -127,7 +125,6 @@
this.filter = filter;
}
-
/**
* The name of the bean containing the labels collection.
*/
@@ -138,7 +135,7 @@
}
public void setLabelName(String labelName) {
- this.labelName = labelName;
+ this.labelName = labelName;
}
/**
@@ -147,41 +144,39 @@
protected String labelProperty = null;
public String getLabelProperty() {
- return labelProperty;
+ return labelProperty;
}
public void setLabelProperty(String labelProperty) {
- this.labelProperty = labelProperty;
+ this.labelProperty = labelProperty;
}
/**
* The name of the bean containing the values collection.
*/
- protected String name=null;
+ protected String name = null;
public String getName() {
- return name;
+ return name;
}
public void setName(String name) {
- this.name = name;
+ this.name = name;
}
-
/**
* The name of the property to use to build the values collection.
*/
- protected String property=null;
+ protected String property = null;
public String getProperty() {
- return property;
+ return property;
}
public void setProperty(String property) {
- this.property = property;
+ this.property = property;
}
-
/**
* The style associated with this tag.
*/
@@ -195,7 +190,6 @@
this.style = style;
}
-
/**
* The named style class associated with this tag.
*/
@@ -209,7 +203,6 @@
this.styleClass = styleClass;
}
-
/**
* Process the start of this tag.
*
@@ -217,7 +210,7 @@
*/
public int doStartTag() throws JspException {
- return SKIP_BODY;
+ return SKIP_BODY;
}
/**
@@ -227,13 +220,12 @@
*/
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"));
- StringBuffer sb = new StringBuffer();
+ // 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"));
+ }
+ StringBuffer sb = new StringBuffer();
// If a collection was specified, use that mode to render options
if (collection != null) {
@@ -246,50 +238,45 @@
try {
value = PropertyUtils.getProperty(bean, property);
- if (value == null)
+ if (value == null) {
value = "";
+ }
} catch (IllegalAccessException e) {
- throw new JspException
- (messages.getMessage("getter.access",
- property, collection));
+ 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()));
+ throw new JspException(
+ messages.getMessage("getter.result", property,
t.toString()));
} catch (NoSuchMethodException e) {
- throw new JspException
- (messages.getMessage("getter.method",
- property, collection));
+ throw new JspException(
+ messages.getMessage("getter.method", property, collection));
}
try {
- if (labelProperty != null)
- label =
- PropertyUtils.getProperty(bean, labelProperty);
- else
+ if (labelProperty != null) {
+ label = PropertyUtils.getProperty(bean, labelProperty);
+ } else {
label = value;
- if (label == null)
+ }
+
+ if (label == null) {
label = "";
+ }
} catch (IllegalAccessException e) {
- throw new JspException
- (messages.getMessage("getter.access",
- labelProperty, collection));
+ 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()));
+ throw new JspException(
+ messages.getMessage("getter.result", labelProperty,
t.toString()));
} catch (NoSuchMethodException e) {
- throw new JspException
- (messages.getMessage("getter.method",
- labelProperty, collection));
+ throw new JspException(
+ messages.getMessage("getter.method", labelProperty,
collection));
}
-
String stringValue = value.toString();
- addOption(sb, stringValue, label.toString(),
- selectTag.isMatched(stringValue));
+ addOption(sb, stringValue, label.toString(),
selectTag.isMatched(stringValue));
}
@@ -298,64 +285,60 @@
// 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()) {
- Object valueObject = valuesIterator.next();
- if (valueObject == null) {
- valueObject = "";
- }
- String value = valueObject.toString();
- String label = value;
- if (labelsIterator.hasNext()) {
- Object labelObject = labelsIterator.next();
- if (labelObject == null) {
- labelObject = "";
- }
- label = labelObject.toString();
- }
- addOption(sb, value, label,
- selectTag.isMatched(value));
- }
- }
+ // 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()) {
+ Object valueObject = valuesIterator.next();
+ if (valueObject == null) {
+ valueObject = "";
+ }
+ String value = valueObject.toString();
+ String label = value;
+ if (labelsIterator.hasNext()) {
+ Object labelObject = labelsIterator.next();
+ if (labelObject == null) {
+ labelObject = "";
+ }
+ label = labelObject.toString();
+ }
+ addOption(sb, value, label, selectTag.isMatched(value));
+ }
+ }
- // Render this element to our writer
+ // Render this element to our writer
ResponseUtils.write(pageContext, sb.toString());
// Evaluate the remainder of this page
- return EVAL_PAGE;
+ return EVAL_PAGE;
}
-
/**
* Release any acquired resources.
*/
public void release() {
- super.release();
+ super.release();
collection = null;
filter = true;
- labelName = null;
- labelProperty = null;
- name = null;
- property = null;
- style = null;
- styleClass = null;
-
+ labelName = null;
+ labelProperty = null;
+ name = null;
+ property = null;
+ style = null;
+ styleClass = null;
}
-
// ------------------------------------------------------ Protected Methods
-
/**
* Add an option element to the specified StringBuffer based on the
* specified parameters.
@@ -374,14 +357,14 @@
* @param label Value to be shown to the user for this option
* @param matched Should this value be marked as selected?
*/
- protected void addOption(StringBuffer sb, String value, String label,
- boolean matched) {
+ protected void addOption(StringBuffer sb, String value, String label, boolean
matched) {
sb.append("<option value=\"");
sb.append(value);
sb.append("\"");
- if (matched)
+ if (matched) {
sb.append(" selected=\"selected\"");
+ }
if (style != null) {
sb.append(" style=\"");
sb.append(style);
@@ -402,7 +385,6 @@
}
-
/**
* Return an iterator for the option labels or values, based on our
* configured properties.
@@ -412,60 +394,59 @@
*
* @exception JspException if an error occurs
*/
- protected Iterator getIterator(String name, String property)
- throws JspException {
+ protected Iterator getIterator(String name, String property) throws
JspException {
+
+ // Identify the bean containing our collection
+ String beanName = name;
+ if (beanName == null) {
+ beanName = Constants.BEAN_KEY;
+ }
- // Identify the bean containing our collection
- String beanName = name;
- if (beanName == null)
- beanName = Constants.BEAN_KEY;
Object bean = RequestUtils.lookup(pageContext, beanName, null);
- if (bean == null)
- throw new JspException
- (messages.getMessage("getter.bean", beanName));
-
- // Identify the collection itself
- Object collection = bean;
- if (property != null) {
- try {
- collection = PropertyUtils.getProperty(bean, property);
- if (collection == null)
- throw new JspException
- (messages.getMessage("getter.property", property));
- } catch (IllegalAccessException e) {
- throw new JspException
- (messages.getMessage("getter.access", property, name));
- } 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, name));
- }
- }
-
- // Construct and return an appropriate iterator
- if (collection.getClass().isArray()) {
- collection = Arrays.asList((Object[]) collection);
- }
-
- if (collection instanceof Collection) {
- return (((Collection) collection).iterator());
-
- } else if (collection instanceof Iterator) {
- return ((Iterator) collection);
-
- } else if (collection instanceof Map) {
- return (((Map) collection).entrySet().iterator());
-
- } else if (collection instanceof Enumeration) {
- return IteratorUtils.asIterator((Enumeration) collection);
-
- } else {
- throw new JspException(messages.getMessage("optionsTag.iterator",
collection.toString()));
- }
+ if (bean == null) {
+ throw new JspException(messages.getMessage("getter.bean", beanName));
+ }
+
+ // Identify the collection itself
+ Object collection = bean;
+ if (property != null) {
+ try {
+ collection = PropertyUtils.getProperty(bean, property);
+ if (collection == null) {
+ throw new JspException(messages.getMessage("getter.property",
property));
+ }
+ } catch (IllegalAccessException e) {
+ throw new JspException(messages.getMessage("getter.access",
property, name));
+ } 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, name));
+ }
+ }
+
+ // Construct and return an appropriate iterator
+ if (collection.getClass().isArray()) {
+ collection = Arrays.asList((Object[]) collection);
+ }
+
+ if (collection instanceof Collection) {
+ return (((Collection) collection).iterator());
+
+ } else if (collection instanceof Iterator) {
+ return ((Iterator) collection);
+
+ } else if (collection instanceof Map) {
+ return (((Map) collection).entrySet().iterator());
+
+ } else if (collection instanceof Enumeration) {
+ return IteratorUtils.asIterator((Enumeration) collection);
+
+ } else {
+ throw new JspException(
+ messages.getMessage("optionsTag.iterator", collection.toString()));
+ }
}
}
1.15 +40 -60
jakarta-struts/src/share/org/apache/struts/taglib/html/OptionTag.java
Index: OptionTag.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/OptionTag.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- OptionTag.java 8 Dec 2002 06:54:51 -0000 1.14
+++ OptionTag.java 1 Feb 2003 05:30:28 -0000 1.15
@@ -7,7 +7,7 @@
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -59,10 +59,8 @@
*
*/
-
package org.apache.struts.taglib.html;
-
import java.util.Locale;
import javax.servlet.jsp.JspException;
@@ -73,7 +71,6 @@
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
-
/**
* Tag for select options. The body of this tag is presented to the user
* in the option list, while the value attribute is the value returned to
@@ -85,32 +82,26 @@
public class OptionTag extends BodyTagSupport {
-
// ----------------------------------------------------- Instance Variables
-
/**
* The default locale for our server.
*/
protected static final Locale defaultLocale = Locale.getDefault();
-
/**
* The message resources for this package.
*/
protected static MessageResources messages =
- MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
-
+ MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
/**
* The message text to be displayed to the user for this tag (if any)
*/
protected String text = null;
-
// ------------------------------------------------------------- Properties
-
/**
* The name of the servlet context attribute containing our message
* resources.
@@ -125,7 +116,6 @@
this.bundle = bundle;
}
-
/**
* Is this option disabled?
*/
@@ -139,7 +129,6 @@
this.disabled = disabled;
}
-
/**
* The key used to look up the text displayed to the user for this
* option, if any.
@@ -154,7 +143,6 @@
this.key = key;
}
-
/**
* The name of the attribute containing the Locale to be used for
* looking up internationalized messages.
@@ -169,7 +157,6 @@
this.locale = locale;
}
-
/**
* The style associated with this tag.
*/
@@ -183,7 +170,6 @@
this.style = style;
}
-
/**
* The named style class associated with this tag.
*/
@@ -230,17 +216,15 @@
protected String value = null;
public String getValue() {
- return (this.value);
+ return (this.value);
}
public void setValue(String value) {
- this.value = value;
+ this.value = value;
}
-
// --------------------------------------------------------- Public Methods
-
/**
* Process the start of this tag.
*
@@ -251,12 +235,11 @@
// Initialize the placeholder for our body content
this.text = null;
- // Do nothing until doEndTag() is called
- return (EVAL_BODY_TAG);
+ // Do nothing until doEndTag() is called
+ return (EVAL_BODY_TAG);
}
-
/**
* Process the body text of this tag (if any).
*
@@ -268,15 +251,15 @@
String text = bodyContent.getString();
if (text != null) {
text = text.trim();
- if (text.length() > 0)
+ if (text.length() > 0) {
this.text = text;
+ }
}
}
return (SKIP_BODY);
}
-
/**
* Process the end of this tag.
*
@@ -284,25 +267,25 @@
*/
public int doEndTag() throws JspException {
- // Acquire the select tag we are associated with
- SelectTag selectTag =
- (SelectTag) pageContext.getAttribute(Constants.SELECT_KEY);
- if (selectTag == null) {
- JspException e = new JspException
- (messages.getMessage("optionTag.select"));
+ // Acquire the select tag we are associated with
+ SelectTag selectTag = (SelectTag)
pageContext.getAttribute(Constants.SELECT_KEY);
+ if (selectTag == null) {
+ JspException e = new
JspException(messages.getMessage("optionTag.select"));
RequestUtils.saveException(pageContext, e);
throw e;
}
- // Generate an HTML <option> element
- StringBuffer results = new StringBuffer();
- results.append("<option value=\"");
- results.append(value);
- results.append("\"");
- if (disabled)
+ // Generate an HTML <option> element
+ StringBuffer results = new StringBuffer();
+ results.append("<option value=\"");
+ results.append(value);
+ results.append("\"");
+ if (disabled) {
results.append(" disabled=\"disabled\"");
- if (selectTag.isMatched(value))
- results.append(" selected=\"selected\"");
+ }
+ if (selectTag.isMatched(value)) {
+ results.append(" selected=\"selected\"");
+ }
if (style != null) {
results.append(" style=\"");
results.append(style);
@@ -318,23 +301,23 @@
results.append(styleClass);
results.append("\"");
}
- results.append(">");
+ results.append(">");
String text = text();
- if (text == null)
- results.append(value);
- else
- results.append(text);
- results.append("</option>");
+ if (text == null) {
+ results.append(value);
+ } else {
+ results.append(text);
+ }
+ results.append("</option>");
- // Render this element to our writer
+ // Render this element to our writer
ResponseUtils.write(pageContext, results.toString());
- // Continue evaluating this page
- return (EVAL_PAGE);
+ // Continue evaluating this page
+ return (EVAL_PAGE);
}
-
/**
* Release any acquired resources.
*/
@@ -350,10 +333,8 @@
value = null;
}
-
// ------------------------------------------------------ Protected Methods
-
/**
* Return the text to be displayed to the user for this option (if any).
*
@@ -361,13 +342,12 @@
*/
protected String text() throws JspException {
- if (this.text != null)
+ if (this.text != null) {
return (this.text);
- else
- return (RequestUtils.message(pageContext, bundle,
- locale, key));
+ } else {
+ return (RequestUtils.message(pageContext, bundle, locale, key));
+ }
}
-
}
1.20 +117 -138
jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java
Index: IterateTag.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- IterateTag.java 1 Feb 2003 05:12:25 -0000 1.19
+++ IterateTag.java 1 Feb 2003 05:30:28 -0000 1.20
@@ -59,10 +59,8 @@
*
*/
-
package org.apache.struts.taglib.logic;
-
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
@@ -79,7 +77,6 @@
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
-
/**
* Custom tag that iterates the elements of a collection, which can be
* either an attribute or the property of an attribute. The collection
@@ -93,81 +90,68 @@
public class IterateTag extends BodyTagSupport {
-
// ----------------------------------------------------- Instance Variables
-
/**
* Iterator of the elements of this collection, while we are actually
* running.
*/
protected Iterator iterator = null;
-
/**
* The number of elements we have already rendered.
*/
protected int lengthCount = 0;
-
/**
* The actual length value (calculated in the start tag).
*/
protected int lengthValue = 0;
-
/**
* The message resources for this package.
*/
protected static MessageResources messages =
- MessageResources.getMessageResources
- ("org.apache.struts.taglib.logic.LocalStrings");
-
-
+
MessageResources.getMessageResources("org.apache.struts.taglib.logic.LocalStrings");
/**
* The actual offset value (calculated in the start tag).
*/
protected int offsetValue = 0;
-
/**
* Has this tag instance been started?
*/
protected boolean started = false;
-
// ------------------------------------------------------------- Properties
-
/**
* The collection over which we will be iterating.
*/
protected Object collection = null;
public Object getCollection() {
- return (this.collection);
+ return (this.collection);
}
public void setCollection(Object collection) {
- this.collection = collection;
+ this.collection = collection;
}
-
/**
* The name of the scripting variable to be exposed.
*/
protected String id = null;
public String getId() {
- return (this.id);
+ return (this.id);
}
public void setId(String id) {
- this.id = id;
+ this.id = id;
}
-
/**
* <p>Return the zero-relative index of the current iteration through the
* loop. If you specify an <code>offset</code>, the first iteration
@@ -186,35 +170,32 @@
return (0);
}
-
/**
* The name of the scripting variable to be exposed as the current index.
*/
protected String indexId = null;
public String getIndexId() {
- return (this.indexId);
+ return (this.indexId);
}
public void setIndexId(String indexId) {
- this.indexId = indexId;
+ this.indexId = indexId;
}
-
/**
* The length value or attribute name (<=0 means no limit).
*/
protected String length = null;
public String getLength() {
- return (this.length);
+ return (this.length);
}
public void setLength(String length) {
- this.length = length;
+ this.length = length;
}
-
/**
* The name of the collection or owning bean.
*/
@@ -225,38 +206,35 @@
}
public void setName(String name) {
- this.name = name;
+ this.name = name;
}
-
/**
* The starting offset (zero relative).
*/
protected String offset = null;
public String getOffset() {
- return (this.offset);
+ return (this.offset);
}
public void setOffset(String offset) {
- this.offset = offset;
+ this.offset = offset;
}
-
/**
* The property name containing the collection.
*/
protected String property = null;
public String getProperty() {
- return (this.property);
+ return (this.property);
}
public void setProperty(String property) {
- this.property = property;
+ this.property = property;
}
-
/**
* The scope of the bean specified by the name property, if any.
*/
@@ -270,7 +248,6 @@
this.scope = scope;
}
-
/**
* The Java class of each exposed element of the collection.
*/
@@ -284,10 +261,8 @@
this.type = type;
}
-
// --------------------------------------------------------- Public Methods
-
/**
* Construct an iterator for the specified collection, and begin
* looping through the body once per element.
@@ -296,21 +271,20 @@
*/
public int doStartTag() throws JspException {
- // Acquire the collection we are going to iterate over
+ // Acquire the collection we are going to iterate over
Object collection = this.collection;
- if (collection == null)
- collection =
- RequestUtils.lookup(pageContext, name, property, scope);
if (collection == null) {
- JspException e = new JspException
- (messages.getMessage("iterate.collection"));
+ collection = RequestUtils.lookup(pageContext, name, property, scope);
+ }
+
+ if (collection == null) {
+ JspException e = new
JspException(messages.getMessage("iterate.collection"));
RequestUtils.saveException(pageContext, e);
throw e;
}
-
- // Construct an iterator for this collection
- if (collection.getClass().isArray()) {
+ // Construct an iterator for this collection
+ if (collection.getClass().isArray()) {
try {
// If we're lucky, it is an array of objects
// that we can iterate over with no copying
@@ -324,83 +298,86 @@
}
iterator = c.iterator();
}
- } else if (collection instanceof Collection)
- iterator = ((Collection) collection).iterator();
- else if (collection instanceof Iterator)
- iterator = (Iterator) collection;
- else if (collection instanceof Map)
- iterator = ((Map) collection).entrySet().iterator();
- else if (collection instanceof Enumeration)
- iterator = IteratorUtils.asIterator((Enumeration)collection);
- else {
- JspException e = new JspException
- (messages.getMessage("iterate.iterator"));
+ } else if (collection instanceof Collection) {
+ iterator = ((Collection) collection).iterator();
+ } else if (collection instanceof Iterator) {
+ iterator = (Iterator) collection;
+ } else if (collection instanceof Map) {
+ iterator = ((Map) collection).entrySet().iterator();
+ } else if (collection instanceof Enumeration) {
+ iterator = IteratorUtils.asIterator((Enumeration) collection);
+ } else {
+ JspException e = new
JspException(messages.getMessage("iterate.iterator"));
RequestUtils.saveException(pageContext, e);
throw e;
}
- // Calculate the starting offset
- if (offset == null)
- offsetValue = 0;
- else {
- try {
- offsetValue = Integer.parseInt(offset);
- } catch (NumberFormatException e) {
- Integer offsetObject = (Integer)
- RequestUtils.lookup(pageContext, offset, null);
- if (offsetObject == null)
- offsetValue = 0;
- else
- offsetValue = offsetObject.intValue();
- }
- }
- if (offsetValue < 0)
- offsetValue = 0;
-
- // Calculate the rendering length
- if (length == null)
- lengthValue = 0;
- else {
- try {
- lengthValue = Integer.parseInt(length);
- } catch (NumberFormatException e) {
- Integer lengthObject = (Integer)
- RequestUtils.lookup(pageContext, length, null);
- if (lengthObject == null)
- lengthValue = 0;
- else
- lengthValue = lengthObject.intValue();
- }
- }
- if (lengthValue < 0)
- lengthValue = 0;
- lengthCount = 0;
-
- // Skip the leading elements up to the starting offset
- for (int i = 0; i < offsetValue; i++) {
- if (iterator.hasNext()) {
- iterator.next();
- }
- }
-
- // Store the first value and evaluate, or skip the body if none
- if (iterator.hasNext()) {
- Object element = iterator.next();
- if (element == null)
+ // Calculate the starting offset
+ if (offset == null) {
+ offsetValue = 0;
+ } else {
+ try {
+ offsetValue = Integer.parseInt(offset);
+ } catch (NumberFormatException e) {
+ Integer offsetObject = (Integer) RequestUtils.lookup(pageContext,
offset, null);
+ if (offsetObject == null) {
+ offsetValue = 0;
+ } else {
+ offsetValue = offsetObject.intValue();
+ }
+ }
+ }
+ if (offsetValue < 0) {
+ offsetValue = 0;
+ }
+
+ // Calculate the rendering length
+ if (length == null) {
+ lengthValue = 0;
+ } else {
+ try {
+ lengthValue = Integer.parseInt(length);
+ } catch (NumberFormatException e) {
+ Integer lengthObject = (Integer) RequestUtils.lookup(pageContext,
length, null);
+ if (lengthObject == null) {
+ lengthValue = 0;
+ } else {
+ lengthValue = lengthObject.intValue();
+ }
+ }
+ }
+ if (lengthValue < 0) {
+ lengthValue = 0;
+ }
+ lengthCount = 0;
+
+ // Skip the leading elements up to the starting offset
+ for (int i = 0; i < offsetValue; i++) {
+ if (iterator.hasNext()) {
+ iterator.next();
+ }
+ }
+
+ // Store the first value and evaluate, or skip the body if none
+ if (iterator.hasNext()) {
+ Object element = iterator.next();
+ if (element == null) {
pageContext.removeAttribute(id);
- else
+ } else {
pageContext.setAttribute(id, element);
- lengthCount++;
+ }
+ lengthCount++;
started = true;
- if (indexId != null)
+ if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
- return (EVAL_BODY_TAG);
- } else
+ }
+ return (EVAL_BODY_TAG);
+ } else {
return (SKIP_BODY);
+ }
}
-
/**
* Make the next collection element available and loop, or
* finish the iterations if there are no more elements.
@@ -416,24 +393,28 @@
}
// Decide whether to iterate or quit
- if ((lengthValue > 0) && (lengthCount >= lengthValue))
- return (SKIP_BODY);
- if (iterator.hasNext()) {
- Object element = iterator.next();
- if (element == null)
+ if ((lengthValue > 0) && (lengthCount >= lengthValue)) {
+ return (SKIP_BODY);
+ }
+
+ if (iterator.hasNext()) {
+ Object element = iterator.next();
+ if (element == null) {
pageContext.removeAttribute(id);
- else
+ } else {
pageContext.setAttribute(id, element);
- lengthCount++;
- if (indexId != null)
+ }
+ lengthCount++;
+ if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
- return (EVAL_BODY_TAG);
- } else
- return (SKIP_BODY);
+ }
+ return (EVAL_BODY_TAG);
+ } else {
+ return (SKIP_BODY);
+ }
}
-
/**
* Clean up after processing this enumeration.
*
@@ -444,23 +425,22 @@
// Clean up our started state
started = false;
- // Continue processing this page
- return (EVAL_PAGE);
+ // Continue processing this page
+ return (EVAL_PAGE);
}
-
/**
* Release all allocated resources.
*/
public void release() {
- super.release();
+ super.release();
- iterator = null;
- lengthCount = 0;
- lengthValue = 0;
- offsetValue = 0;
+ iterator = null;
+ lengthCount = 0;
+ lengthValue = 0;
+ offsetValue = 0;
id = null;
collection = null;
@@ -472,6 +452,5 @@
started = false;
}
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]