use of org.apache.struts.util.IteratorAdapter in project sonarqube by SonarSource.
the class OptionsTag method getIterator.
/**
* Return an iterator for the option labels or values, based on our
* configured properties.
*
* @param name Name of the bean attribute (if any)
* @param property Name of the bean property (if any)
* @throws JspException if an error occurs
*/
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;
}
Object bean = TagUtils.getInstance().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 new IteratorAdapter((Enumeration) collection);
} else {
throw new JspException(messages.getMessage("optionsTag.iterator", collection.toString()));
}
}
use of org.apache.struts.util.IteratorAdapter in project sonarqube by SonarSource.
the class IterateTag method doStartTag.
// --------------------------------------------------------- Public Methods
/**
* Construct an iterator for the specified collection, and begin looping
* through the body once per element.
*
* @throws JspException if a JSP exception has occurred
*/
public int doStartTag() throws JspException {
// Acquire the collection we are going to iterate over
Object collection = this.collection;
if (collection == null) {
collection = TagUtils.getInstance().lookup(pageContext, name, property, scope);
}
if (collection == null) {
JspException e = new JspException(messages.getMessage("iterate.collection"));
TagUtils.getInstance().saveException(pageContext, e);
throw e;
}
// 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
iterator = Arrays.asList((Object[]) collection).iterator();
} catch (ClassCastException e) {
// Rats -- it is an array of primitives
int length = Array.getLength(collection);
ArrayList c = new ArrayList(length);
for (int i = 0; i < length; i++) {
c.add(Array.get(collection, i));
}
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 = new IteratorAdapter((Enumeration) collection);
} else {
JspException e = new JspException(messages.getMessage("iterate.iterator"));
TagUtils.getInstance().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) TagUtils.getInstance().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) TagUtils.getInstance().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 {
pageContext.setAttribute(id, element);
}
lengthCount++;
started = true;
if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
}
return (EVAL_BODY_TAG);
} else {
return (SKIP_BODY);
}
}
Aggregations