View Javadoc

1   /*
2    * $Id: IteratorGenerator.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.util;
22  
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.StringTokenizer;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  import com.opensymphony.xwork2.Action;
32  
33  
34  /***
35   * A bean that generates an iterator filled with a given object depending on the count,
36   * separator and converter defined. It is being used by IteratorGeneratorTag.
37   *
38   */
39  public class IteratorGenerator implements Iterator, Action {
40  
41      private static final Log _log = LogFactory.getLog(IteratorGenerator.class);
42  
43      List values;
44      Object value;
45      String separator;
46      Converter converter;
47  
48      // Attributes ----------------------------------------------------
49      int count = 0;
50      int currentCount = 0;
51  
52  
53      public void setCount(int aCount) {
54          this.count = aCount;
55      }
56  
57      public boolean getHasNext() {
58          return hasNext();
59      }
60  
61      public Object getNext() {
62          return next();
63      }
64  
65      public void setSeparator(String aChar) {
66          separator = aChar;
67      }
68  
69      public void setConverter(Converter aConverter) {
70          converter = aConverter;
71      }
72  
73      // Public --------------------------------------------------------
74      public void setValues(Object aValue) {
75          value = aValue;
76      }
77  
78      // Action implementation -----------------------------------------
79      public String execute() {
80          if (value == null) {
81              return ERROR;
82          } else {
83              values = new ArrayList();
84  
85              if (separator != null) {
86                  StringTokenizer tokens = new StringTokenizer(value.toString(), separator);
87  
88                  while (tokens.hasMoreTokens()) {
89                      String token = tokens.nextToken().trim();
90                      if (converter != null) {
91                          try {
92                              Object convertedObj = converter.convert(token);
93                              values.add(convertedObj);
94                          }
95                          catch(Exception e) { // make sure things, goes on, we just ignore the bad ones
96                              _log.warn("unable to convert ["+token+"], skipping this token, it will not appear in the generated iterator", e);
97                          }
98                      }
99                      else {
100                         values.add(token);
101                     }
102                 }
103             } else {
104                 values.add(value.toString());
105             }
106 
107             // Count default is the size of the list of values
108             if (count == 0) {
109                 count = values.size();
110             }
111 
112             return SUCCESS;
113         }
114     }
115 
116     // Iterator implementation ---------------------------------------
117     public boolean hasNext() {
118         return (value == null) ? false : ((currentCount < count) || (count == -1));
119     }
120 
121     public Object next() {
122         try {
123             return values.get(currentCount % values.size());
124         } finally {
125             currentCount++;
126         }
127     }
128 
129     public void remove() {
130         throw new UnsupportedOperationException("Remove is not supported in IteratorGenerator.");
131     }
132 
133 
134     // Inner class --------------------------------------------------
135     /***
136      * Interface for converting each separated token into an Object of choice.
137      */
138     public static interface Converter {
139         Object convert(String token) throws Exception;
140     }
141 }