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.bytecode.statement.tag;
020
021import lucee.runtime.exp.TemplateException;
022import lucee.transformer.bytecode.BytecodeContext;
023import lucee.transformer.bytecode.BytecodeException;
024import lucee.transformer.bytecode.Position;
025import lucee.transformer.bytecode.Statement;
026import lucee.transformer.bytecode.statement.FlowControlFinal;
027import lucee.transformer.bytecode.visitor.ParseBodyVisitor;
028
029public final class TagOutput extends TagGroup {
030
031        public static final int TYPE_QUERY = 0;
032        public static final int TYPE_GROUP = 1;
033        public static final int TYPE_INNER_GROUP = 2;
034        public static final int TYPE_INNER_QUERY = 3;
035        public static final int TYPE_NORMAL= 4;
036        
037        
038        private int type;
039        
040
041        public TagOutput(Position start,Position end) {
042                super(start,end);
043        }
044
045
046        public static TagOutput getParentTagOutputQuery(Statement stat) throws BytecodeException {
047                Statement parent=stat.getParent();
048                if(parent==null) throw new BytecodeException("there is no parent output with query",null);
049                else if(parent instanceof TagOutput) {
050                        if(((TagOutput)parent).hasQuery())
051                                return ((TagOutput)parent);
052                }
053                return getParentTagOutputQuery(parent);
054        }
055
056        public void setType(int type) {
057                this.type=type;
058        }
059
060
061        /**
062         *
063         * @see lucee.transformer.bytecode.statement.tag.TagBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
064         */
065        public void _writeOut(BytecodeContext bc) throws BytecodeException {
066                boolean old;
067                switch(type) {
068                case TYPE_GROUP:
069                        old = bc.changeDoSubFunctions(false);
070                        TagGroupUtil.writeOutTypeGroup(this,bc);
071                        bc.changeDoSubFunctions(old);
072                break;
073                case TYPE_INNER_GROUP:
074                        old = bc.changeDoSubFunctions(false);
075                        TagGroupUtil.writeOutTypeInnerGroup(this,bc);
076                        bc.changeDoSubFunctions(old);
077                break;
078                case TYPE_INNER_QUERY:
079                        old = bc.changeDoSubFunctions(false);
080                        TagGroupUtil.writeOutTypeInnerQuery(this,bc);
081                        bc.changeDoSubFunctions(old);
082                break;
083                case TYPE_NORMAL:
084                        writeOutTypeNormal(bc);
085                break;
086                case TYPE_QUERY:
087                        old = bc.changeDoSubFunctions(false);
088                        TagGroupUtil.writeOutTypeQuery(this,bc);
089                        bc.changeDoSubFunctions(old);
090                break;
091                
092                default:
093                        throw new BytecodeException("invalid type",getStart());
094                }
095        }
096
097
098        
099        
100
101
102        
103
104
105        /**
106         * write out normal query
107         * @param adapter
108         * @throws TemplateException
109         */
110        private void writeOutTypeNormal(BytecodeContext bc) throws BytecodeException {
111                ParseBodyVisitor pbv=new ParseBodyVisitor();
112                pbv.visitBegin(bc);
113                        getBody().writeOut(bc);
114                pbv.visitEnd(bc);
115        }
116
117
118        @Override
119        public short getType() {
120                return TAG_OUTPUT;
121        }
122
123
124        @Override
125        public FlowControlFinal getFlowControlFinal() {
126                return null;
127        }
128
129}