001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.transformer.cfml.evaluator.impl;
020
021import lucee.transformer.bytecode.Statement;
022import lucee.transformer.bytecode.statement.tag.Tag;
023import lucee.transformer.bytecode.statement.tag.TagOutput;
024import lucee.transformer.cfml.evaluator.EvaluatorException;
025import lucee.transformer.cfml.evaluator.EvaluatorSupport;
026import lucee.transformer.library.tag.TagLibTag;
027
028
029/**
030 * Prueft den Kontext des Tag output.
031 * Das Tag output darf nicht innerhalb eines output Tag verschachtelt sein, 
032 * ausser das aeussere Tag besitzt ein group Attribute. Das innere Tag darf jedoch kein group Attribute besitzen.
033
034 */
035public final class Output extends EvaluatorSupport {
036
037        /**
038         * @see lucee.transformer.cfml.evaluator.EvaluatorSupport#evaluate(org.w3c.dom.Element, lucee.transformer.library.tag.TagLibTag)
039         */
040        public void evaluate(Tag tag,TagLibTag libTag) throws EvaluatorException { 
041                
042                TagOutput output=(TagOutput) tag;
043                
044        // check if inside a query tag
045                TagOutput parent = output;
046        boolean hasParentWithGroup=false;
047        boolean hasParentWithQuery=false;
048                boolean hasQuery=tag.containsAttribute("query");
049                
050                while((parent=getParentTagOutput(parent))!=null) {
051            if(!hasParentWithQuery)hasParentWithQuery=parent.hasQuery();
052            if(!hasParentWithGroup)hasParentWithGroup=parent.hasGroup();
053            if(hasParentWithQuery && hasParentWithGroup)break;
054                }
055        
056        if(hasQuery && hasParentWithQuery) 
057                        throw new EvaluatorException("Nesting of tags cfoutput with attribute query is not allowed");
058
059        if(hasQuery) 
060                output.setType(TagOutput.TYPE_QUERY);
061        
062        else if(tag.containsAttribute("group") && hasParentWithQuery)
063                output.setType(TagOutput.TYPE_GROUP);
064        
065        else if(hasParentWithQuery) {
066                if(hasParentWithGroup) output.setType(TagOutput.TYPE_INNER_GROUP);
067                else output.setType(TagOutput.TYPE_INNER_QUERY);
068        }
069        else
070                 output.setType(TagOutput.TYPE_NORMAL);
071        
072        
073        
074        // attribute maxrows and endrow not allowd at the same time
075        if(tag.containsAttribute("maxrows") && tag.containsAttribute("endrow"))
076                throw new EvaluatorException("Wrong Context, you cannot use attribute maxrows and endrow at the same time.");
077        
078        
079        }
080        
081        public static TagOutput getParentTagOutput(TagOutput stat) {
082                Statement parent = stat;
083                
084                
085                while(true)     {
086                        parent=parent.getParent();
087                        if(parent==null)return null;
088                        if(parent instanceof TagOutput) return (TagOutput) parent;
089                }
090        }
091}
092
093
094
095