Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » components » [javadoc | source]
    1   /*
    2    * $Id: AppendIterator.java 651946 2008-04-27 13:41:38Z apetrelli $
    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   
   22   package org.apache.struts2.components;
   23   
   24   import java.io.Writer;
   25   import java.util.ArrayList;
   26   import java.util.Iterator;
   27   import java.util.List;
   28   
   29   import org.apache.struts2.components.Param.UnnamedParametric;
   30   import org.apache.struts2.util.AppendIteratorFilter;
   31   import org.apache.struts2.util.MakeIterator;
   32   import org.apache.struts2.views.annotations.StrutsTag;
   33   import org.apache.struts2.views.annotations.StrutsTagAttribute;
   34   
   35   import com.opensymphony.xwork2.util.ValueStack;
   36   import com.opensymphony.xwork2.util.logging.Logger;
   37   import com.opensymphony.xwork2.util.logging.LoggerFactory;
   38   
   39   /**
   40    * <!-- START SNIPPET: javadoc -->
   41    * <p>Component for AppendIteratorTag, which jobs is to append iterators to form an
   42    * appended iterator whereby entries goes from one iterator to another after each
   43    * respective iterator is exhausted of entries.</p>
   44    *
   45    * <p>For example, if there are 3 iterator appended (each iterator has 3 entries),
   46    * the following will be how the appended iterator entries will be arranged:</p>
   47    *
   48    * <ol>
   49    *      <li>First Entry of the First Iterator</li>
   50    *      <li>Second Entry of the First Iterator</li>
   51    *      <li>Third Entry of the First Iterator</li>
   52    *      <li>First Entry of the Second Iterator</li>
   53    *      <li>Second Entry of the Second Iterator</li>
   54    *      <li>Third Entry of the Second Iterator</li>
   55    *      <li>First Entry of the Third Iterator</li>
   56    *      <li>Second Entry of the Third Iterator</li>
   57    *      <li>Third Entry of the Third ITerator</li>
   58    * </ol>
   59    * <!-- END SNIPPET: javadoc -->
   60    *
   61    * <!-- START SNIPPET: params -->
   62    * <ul>
   63    *      <li>id (String) - the id of which if supplied will have the resultant
   64    *                        appended iterator stored under in the stack's context</li>
   65    * </ul>
   66    * <!-- END SNIPPET: params -->
   67    *
   68    *
   69    * <!-- START SNIPPET: code -->
   70    * public class AppendIteratorTagAction extends ActionSupport {
   71    *
   72    *  private List myList1;
   73    *  private List myList2;
   74    *  private List myList3;
   75    *
   76    *
   77    *  public String execute() throws Exception {
   78    *
   79    *      myList1 = new ArrayList();
   80    *      myList1.add("1");
   81    *      myList1.add("2");
   82    *      myList1.add("3");
   83    *
   84    *      myList2 = new ArrayList();
   85    *      myList2.add("a");
   86    *      myList2.add("b");
   87    *      myList2.add("c");
   88    *
   89    *      myList3 = new ArrayList();
   90    *      myList3.add("A");
   91    *      myList3.add("B");
   92    *      myList3.add("C");
   93    *
   94    *      return "done";
   95    *  }
   96    *
   97    *  public List getMyList1() { return myList1; }
   98    *  public List getMyList2() { return myList2; }
   99    *  public List getMyList3() { return myList3; }
  100    *}
  101    * <!-- END SNIPPET: code -->
  102    *
  103    * <!-- START SNIPPET: example -->
  104    * &lt;s:append var="myAppendIterator"&gt;
  105    *      &lt;s:param value="%{myList1}" /&gt;
  106    *      &lt;s:param value="%{myList2}" /&gt;
  107    *      &lt;s:param value="%{myList3}" /&gt;
  108    * &lt;/s:append&gt;
  109    * &lt;s:iterator value="%{#myAppendIterator}"&gt;
  110    *      &lt;s:property /&gt;
  111    * &lt;/s:iterator&gt;
  112    * <!-- END SNIPPET: example -->
  113    *
  114    *
  115    * @see org.apache.struts2.util.AppendIteratorFilter
  116    * @see org.apache.struts2.views.jsp.iterator.AppendIteratorTag
  117    *
  118    */
  119   @StrutsTag(name="append", tldTagClass="org.apache.struts2.views.jsp.iterator.AppendIteratorTag", description="Append the values of a list of iterators to one iterator")
  120   public class AppendIterator extends ContextBean implements UnnamedParametric {
  121   
  122       private static final Logger LOG = LoggerFactory.getLogger(AppendIterator.class);
  123   
  124       private AppendIteratorFilter appendIteratorFilter= null;
  125       private List _parameters;
  126   
  127       public AppendIterator(ValueStack stack) {
  128           super(stack);
  129       }
  130   
  131       public boolean start(Writer writer) {
  132           _parameters = new ArrayList();
  133           appendIteratorFilter = new AppendIteratorFilter();
  134   
  135           return super.start(writer);
  136       }
  137   
  138       public boolean end(Writer writer, String body) {
  139   
  140           for (Iterator paramEntries = _parameters.iterator(); paramEntries.hasNext(); ) {
  141   
  142               Object iteratorEntryObj = paramEntries.next();
  143               if (! MakeIterator.isIterable(iteratorEntryObj)) {
  144                   LOG.warn("param with value resolved as "+iteratorEntryObj+" cannot be make as iterator, it will be ignored and hence will not appear in the merged iterator");
  145                   continue;
  146               }
  147               appendIteratorFilter.setSource(MakeIterator.convert(iteratorEntryObj));
  148           }
  149   
  150           appendIteratorFilter.execute();
  151   
  152           putInContext(appendIteratorFilter);
  153   
  154           appendIteratorFilter = null;
  155   
  156           return super.end(writer, body);
  157       }
  158   
  159       // UnnamedParametric implementation --------------------------------------
  160       public void addParameter(Object value) {
  161           _parameters.add(value);
  162       }
  163   
  164       @StrutsTagAttribute(description="The name of which if supplied will have the resultant appended iterator stored under in the stack's context")
  165       public void setVar(String var) {
  166           super.setVar(var);
  167       }
  168   }
  169   
  170   

Save This Page
Home » struts-2.1.8.1-src » org.apache » struts2 » components » [javadoc | source]