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

InterpreterBind.java

Go to the documentation of this file.
00001 //$Id: InterpreterBind_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 import java.util.Vector;
00035 
00036 import org.argoprint.ArgoPrintDataSource;
00037 import org.argoprint.engine.Environment;
00038 import org.w3c.dom.Document;
00039 import org.w3c.dom.Element;
00040 import org.w3c.dom.NamedNodeMap;
00041 import org.w3c.dom.Node;
00042 import org.w3c.dom.NodeList;
00043 
00044 /**
00045  * Creates new elements through the ap:bind tag.
00046  */
00047 public class InterpreterBind extends Interpreter {
00048 
00049     public InterpreterBind(ArgoPrintDataSource dataSource) {
00050         super("bind", dataSource);
00051     }
00052 
00053     /**
00054      * Processes the bind tag.
00055      * 
00056      * @param tagNode The tag to process.
00057      * @param env The environment in which to process it.
00058      */
00059     protected void processTag(Node tagNode, Environment env) throws Exception {
00060         
00061         // Create the new element
00062         NamedNodeMap attributes = tagNode.getAttributes();
00063         Node attr = attributes.getNamedItem("name");
00064         if (attr == null)
00065             throw new BadTemplateException("Bind tag contains no name attribute.");
00066         Document document = tagNode.getOwnerDocument();
00067         Element newElement = document.createElement(attr.getNodeValue());
00068 
00069         NodeList children;
00070         NodeList bindChildren = tagNode.getChildNodes();
00071         for (int i = 0; i < bindChildren.getLength();) {
00072             Node child = bindChildren.item(i);
00073             if (isNodeNamed(child, PREFIX, "attr")) {
00074                 attributes = child.getAttributes();
00075                 attr = attributes.getNamedItem("name");
00076                 if (attr == null)
00077                     throw new BadTemplateException("attr tag contains no name attribute.");
00078                 children = child.getChildNodes();
00079                 // Recurse on the contents of the attr tag.
00080                 Vector childrenVector = getVector(children);
00081                 recurse(childrenVector, env);
00082                 child.normalize();
00083                 
00084                 // Extract the text and put it in the attribute                 
00085                 children = child.getChildNodes();
00086                 if (children.getLength() == 0)
00087                     newElement.setAttribute(attr.getNodeValue(), "");
00088                 else if ((children.item(0).getNodeType() == Node.TEXT_NODE) && (children.getLength() == 1))  
00089                     newElement.setAttribute(attr.getNodeValue(), children.item(0).getNodeValue());
00090                 else
00091                     throw new BadTemplateException("Malformed attr tag.");
00092                 i++;
00093             }
00094             else {
00095                 tagNode.removeChild(child);
00096                 newElement.appendChild(child);
00097                 _firstHandler.handleTag(child, env);
00098                 // Don't increment i since removeChild removed index i
00099             }
00100         }
00101         Node parentNode = tagNode.getParentNode();
00102         parentNode.replaceChild(newElement, tagNode);
00103     }
00104     
00105 
00106 }

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