001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.util;
006:
007: import com.opensymphony.xwork.Action;
008:
009: import java.util.ArrayList;
010: import java.util.Iterator;
011: import java.util.List;
012: import java.util.StringTokenizer;
013:
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016:
017: /**
018: * A bean that generates an iterator filled with a given object depending on the count,
019: * separator and converter defined. It is being used by IteratorGeneratorTag.
020: *
021: * @author Rickard �berg (rickard@middleware-company.com)
022: * @author tm_jee ( tm_jee(at)yahoo.co.uk )
023: * @version $Revision: 1419 $
024: */
025: public class IteratorGenerator implements Iterator, Action {
026:
027: private static final Log _log = LogFactory
028: .getLog(IteratorGenerator.class);
029:
030: List values;
031: Object value;
032: String separator;
033: Converter converter;
034:
035: // Attributes ----------------------------------------------------
036: int count = 0;
037: int currentCount = 0;
038:
039: public void setCount(int aCount) {
040: this .count = aCount;
041: }
042:
043: public boolean getHasNext() {
044: return hasNext();
045: }
046:
047: public Object getNext() {
048: return next();
049: }
050:
051: public void setSeparator(String aChar) {
052: separator = aChar;
053: }
054:
055: public void setConverter(Converter aConverter) {
056: converter = aConverter;
057: }
058:
059: // Public --------------------------------------------------------
060: public void setValues(Object aValue) {
061: value = aValue;
062: }
063:
064: // Action implementation -----------------------------------------
065: public String execute() {
066: if (value == null) {
067: return ERROR;
068: } else {
069: values = new ArrayList();
070:
071: if (separator != null) {
072: StringTokenizer tokens = new StringTokenizer(value
073: .toString(), separator);
074:
075: while (tokens.hasMoreTokens()) {
076: String token = tokens.nextToken().trim();
077: if (converter != null) {
078: try {
079: Object convertedObj = converter
080: .convert(token);
081: values.add(convertedObj);
082: } catch (Exception e) { // make sure things, goes on, we just ignore the bad ones
083: _log
084: .warn(
085: "unable to convert ["
086: + token
087: + "], skipping this token, it will not appear in the generated iterator",
088: e);
089: }
090: } else {
091: values.add(token);
092: }
093: }
094: } else {
095: values.add(value.toString());
096: }
097:
098: // Count default is the size of the list of values
099: if (count == 0) {
100: count = values.size();
101: }
102:
103: return SUCCESS;
104: }
105: }
106:
107: // Iterator implementation ---------------------------------------
108: public boolean hasNext() {
109: return (value == null) ? false
110: : ((currentCount < count) || (count == -1));
111: }
112:
113: public Object next() {
114: try {
115: return values.get(currentCount % values.size());
116: } finally {
117: currentCount++;
118: }
119: }
120:
121: public void remove() {
122: throw new UnsupportedOperationException(
123: "Remove is not supported in IteratorGenerator.");
124: }
125:
126: // Inner class --------------------------------------------------
127: /**
128: * Interface for converting each separated token into an Object of choice.
129: */
130: public static interface Converter {
131: Object convert(String token) throws Exception;
132: }
133: }
|