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

InterpreterIf.java

Go to the documentation of this file.
00001 //$Id: InterpreterIf_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.Vector;
00036 
00037 import org.argoprint.ArgoPrintDataSource;
00038 import org.argoprint.engine.Environment;
00039 import org.w3c.dom.*;
00040 
00041 /**
00042  * Handles the if construct through the ap:if tag.
00043  */
00044 public class InterpreterIf extends Interpreter {
00045 
00046     public InterpreterIf(ArgoPrintDataSource dataSource) {
00047         super("if", dataSource);
00048     }
00049 
00050     /**
00051      * Processes the if tag.
00052      * 
00053      * @param tagNode The tag to process.
00054      * @param env The environment in which to process it.
00055      */
00056     protected void processTag(Node tagNode, Environment env) throws Exception {
00057         // TODO: divide into some private methods
00058 
00059         // Evaluate condition
00060         NamedNodeMap attributes = tagNode.getAttributes();
00061         Object returnValue = callDataSource("cond", attributes, env);
00062         if (!(returnValue instanceof Boolean))
00063             throw new BadTemplateException("The condition did not evaluate to a Boolean.");
00064         
00065         NodeList ifChildren = tagNode.getChildNodes();
00066         
00067         // Find then and else nodes
00068         Node thenNode = null;
00069         Node elseNode = null;
00070         for (int i = 0; i < ifChildren.getLength(); i++) {
00071             if (isNodeNamed(ifChildren.item(i), PREFIX, "then"))
00072                 thenNode = ifChildren.item(i);
00073             else if (isNodeNamed(ifChildren.item(i), PREFIX, "else"))
00074                 elseNode = ifChildren.item(i);
00075         }
00076                         
00077         // Decide what part of the sub tree (if any) shall be used
00078         NodeList resultChildren;
00079         if (((Boolean)returnValue).booleanValue()) {
00080             if (thenNode == null)
00081                 resultChildren = ifChildren;
00082             else
00083                 resultChildren = thenNode.getChildNodes();
00084         }
00085         else {
00086             if (elseNode == null)
00087                 resultChildren = null;
00088             else
00089                 resultChildren = elseNode.getChildNodes();
00090         }
00091         
00092         // Attach the result to parent and remove if tag
00093         Node ifParent = tagNode.getParentNode();
00094         if (!(resultChildren == null || resultChildren.getLength() == 0)) { 
00095             Node resultParent = resultChildren.item(0).getParentNode();
00096             Vector resultVector = getVector(resultChildren);
00097             for (int i = 0; i < resultVector.size(); i++) {
00098                 Node child = (Node)resultVector.get(i);
00099                 resultParent.removeChild(child);
00100                 ifParent.insertBefore(child, tagNode);
00101             }
00102             recurse(resultVector, env);
00103         }
00104         ifParent.removeChild(tagNode);
00105     }
00106     
00107 
00108 }
00109     

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