001: /*
002: * $Id: IteratorGenerator.java 471756 2006-11-06 15:01:43Z 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.struts2.util;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.StringTokenizer;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import com.opensymphony.xwork2.Action;
032:
033: /**
034: * A bean that generates an iterator filled with a given object depending on the count,
035: * separator and converter defined. It is being used by IteratorGeneratorTag.
036: *
037: */
038: public class IteratorGenerator implements Iterator, Action {
039:
040: private static final Log _log = LogFactory
041: .getLog(IteratorGenerator.class);
042:
043: List values;
044: Object value;
045: String separator;
046: Converter converter;
047:
048: // Attributes ----------------------------------------------------
049: int count = 0;
050: int currentCount = 0;
051:
052: public void setCount(int aCount) {
053: this .count = aCount;
054: }
055:
056: public boolean getHasNext() {
057: return hasNext();
058: }
059:
060: public Object getNext() {
061: return next();
062: }
063:
064: public void setSeparator(String aChar) {
065: separator = aChar;
066: }
067:
068: public void setConverter(Converter aConverter) {
069: converter = aConverter;
070: }
071:
072: // Public --------------------------------------------------------
073: public void setValues(Object aValue) {
074: value = aValue;
075: }
076:
077: // Action implementation -----------------------------------------
078: public String execute() {
079: if (value == null) {
080: return ERROR;
081: } else {
082: values = new ArrayList();
083:
084: if (separator != null) {
085: StringTokenizer tokens = new StringTokenizer(value
086: .toString(), separator);
087:
088: while (tokens.hasMoreTokens()) {
089: String token = tokens.nextToken().trim();
090: if (converter != null) {
091: try {
092: Object convertedObj = converter
093: .convert(token);
094: values.add(convertedObj);
095: } catch (Exception e) { // make sure things, goes on, we just ignore the bad ones
096: _log
097: .warn(
098: "unable to convert ["
099: + token
100: + "], skipping this token, it will not appear in the generated iterator",
101: e);
102: }
103: } else {
104: values.add(token);
105: }
106: }
107: } else {
108: values.add(value.toString());
109: }
110:
111: // Count default is the size of the list of values
112: if (count == 0) {
113: count = values.size();
114: }
115:
116: return SUCCESS;
117: }
118: }
119:
120: // Iterator implementation ---------------------------------------
121: public boolean hasNext() {
122: return (value == null) ? false
123: : ((currentCount < count) || (count == -1));
124: }
125:
126: public Object next() {
127: try {
128: return values.get(currentCount % values.size());
129: } finally {
130: currentCount++;
131: }
132: }
133:
134: public void remove() {
135: throw new UnsupportedOperationException(
136: "Remove is not supported in IteratorGenerator.");
137: }
138:
139: // Inner class --------------------------------------------------
140: /**
141: * Interface for converting each separated token into an Object of choice.
142: */
143: public static interface Converter {
144: Object convert(String token) throws Exception;
145: }
146: }
|