Main Page | Packages | Class Hierarchy | Class List | File List | Class Members

InterpreterIterate.java

Go to the documentation of this file.
00001 //$Id: InterpreterIterate_8java-source.html 336 2010-01-12 20:03:17Z linus $
00002 //Copyright (c) 2003, Mikael Albertsson, Mattias Danielsson, Per Engström, 
00003 //Fredrik Gröndahl, Martin Gyllensten, Anna Kent, Anders Olsson, 
00004 //Mattias Sidebäck.
00005 //All rights reserved.
00006 //
00007 //Redistribution and use in source and binary forms, with or without 
00008 //modification, are permitted provided that the following conditions are met:
00009 //
00010 //* Redistributions of source code must retain the above copyright notice, 
00011 //  this list of conditions and the following disclaimer.
00012 // 
00013 //* Redistributions in binary form must reproduce the above copyright 
00014 //  notice, this list of conditions and the following disclaimer in the 
00015 //  documentation and/or other materials provided with the distribution.
00016 //
00017 //* Neither the name of the University of Linköping nor the names of its 
00018 //  contributors may be used to endorse or promote products derived from 
00019 //  this software without specific prior written permission. 
00020 //
00021 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00022 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00023 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00024 //ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
00025 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00026 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00027 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00028 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00029 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00030 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
00031 //THE POSSIBILITY OF SUCH DAMAGE.
00032 
00033 package org.argoprint.engine.interpreters;
00034 
00035 import java.util.Collection;
00036 import java.util.ArrayList;
00037 import java.util.TreeMap;
00038 import java.util.Iterator;
00039 
00040 import org.argoprint.ArgoPrintDataSource;
00041 import org.argoprint.engine.ArgoPrintIterator;
00042 import org.argoprint.engine.Environment;
00043 import org.w3c.dom.*;
00044 
00045 /**
00046  * Handles iteration through the ap:iterate tag.
00047  */
00048 public class InterpreterIterate extends Interpreter {
00049 
00050     public InterpreterIterate(ArgoPrintDataSource dataSource) {
00051         super("iterate", dataSource);
00052     }
00053 
00054     /**
00055      * Processes the iterate tag.
00056      * 
00057      * @param tagNode The tag to process.
00058      * @param env The environment in which to process it.
00059      */
00060 
00061     protected void processTag(Node tagNode, Environment env) throws Exception {
00062         NamedNodeMap attributes = tagNode.getAttributes();
00063         
00064         // Get the collection
00065         Object callReturnValue = callDataSource("what", attributes, env);
00066         if (!(callReturnValue instanceof Collection) && 
00067             !(callReturnValue instanceof Object)){
00068             throw new Exception("The object returned from the call to the data source is not a collection.");
00069         } else if (!(callReturnValue instanceof Collection) && 
00070                (callReturnValue instanceof Object)){
00071             ArrayList tmpCollection = new ArrayList();
00072             tmpCollection.add(callReturnValue);
00073             callReturnValue = tmpCollection;
00074         }
00075         
00076         
00077         Collection iterateCollection = (Collection) callReturnValue;
00078 
00079         Node sortvalueAttribute = attributes.getNamedItem("sortvalue");
00080         if (sortvalueAttribute != null) {
00081             String sortvalue = sortvalueAttribute.getNodeValue();
00082             iterateCollection = sortCollection(iterateCollection, sortvalue);
00083         }
00084             
00085         Node iteratornameAttribute = attributes.getNamedItem("iteratorname");
00086         if (iteratornameAttribute == null) 
00087             throw new BadTemplateException("Iterate tag contains no iteratorname attribute.");
00088         String iteratorname = iteratornameAttribute.getNodeValue();
00089         
00090         ArgoPrintIterator iterator = new ArgoPrintIterator(iterateCollection.iterator());
00091         env.addIterator(iteratorname, iterator);
00092 
00093         Document document = tagNode.getOwnerDocument();
00094         Node parentNode = tagNode.getParentNode();
00095         NodeList children = tagNode.getChildNodes();
00096         Node newNode;
00097 
00098         // For every object in the iterator, clone every child, attach it to the parent and recurse
00099         while (iterator.hasNext()) {
00100             iterator.next();
00101             for (int i = 0; i < children.getLength(); i++) {
00102                 newNode = children.item(i).cloneNode(true);
00103                 parentNode.insertBefore(newNode, tagNode);
00104                 _firstHandler.handleTag(newNode, env);
00105             }
00106         }
00107         env.removeIterator(iteratorname);
00108         parentNode.removeChild(tagNode);
00109     }
00110     
00111     /**
00112      * Sorts a Collection according to a String returned by a call to the data source for each object in the collection. 
00113      * 
00114      * @param collection The collection to sort.
00115      * @param sortvalue The call that will be applied to each object providing a value to sort on.
00116      * @return A sorted Collection.
00117      * @throws Exception
00118      */
00119     private Collection sortCollection(Collection collection, String sortvalue) throws Exception {
00120         Iterator iterator = collection.iterator();
00121         TreeMap sortedMap = new TreeMap();
00122         Object object;
00123         Object returned;
00124         while (iterator.hasNext()) {
00125             object = iterator.next();
00126             returned = _dataSource.caller(sortvalue, object);
00127             if (returned == null)
00128                 returned = "null";
00129             else if (!(returned instanceof String))
00130                 throw new Exception("The sortvalue function did not return a String.");
00131             sortedMap.put(returned, object);
00132         }
00133         return sortedMap.values();
00134     }
00135 }

Generated on Tue Dec 2 22:50:56 2003 for ArgoPrint by doxygen 1.3.4