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 java.util.Iterator;
022import java.util.List;
023
024import lucee.transformer.bytecode.Body;
025import lucee.transformer.bytecode.Statement;
026import lucee.transformer.bytecode.statement.tag.Tag;
027import lucee.transformer.bytecode.util.ASMUtil;
028import lucee.transformer.cfml.evaluator.EvaluatorException;
029import lucee.transformer.cfml.evaluator.EvaluatorSupport;
030
031import org.w3c.dom.Element;
032
033/**
034 * Prueft den Kontext des Tag <code>try</code>.
035 * Innerhalb des Tag try muss sich am Schluss 1 bis n Tags vom Typ catch befinden.
036 */
037public final class Try extends EvaluatorSupport {
038        
039        /**
040         * @see lucee.transformer.cfml.evaluator.EvaluatorSupport#evaluate(Element)
041         */
042        public void evaluate(Tag tag) throws EvaluatorException {
043                Body body=tag.getBody();
044        int catchCount=0;
045        int noCatchCount=0;
046        int finallyCount=0;
047        
048        // count catch tag and other in body
049        if(body!=null) {
050                List stats = body.getStatements();
051                Iterator it = stats.iterator();
052                Statement stat;
053                Tag t;
054            String name;
055            while(it.hasNext()) {
056                stat=(Statement) it.next();
057                if(stat instanceof Tag) {
058                        t=(Tag) stat;
059                        name=t.getTagLibTag().getName();
060                        if(name.equals("finally")) {
061                                finallyCount++;
062                                noCatchCount++;
063                        }
064                        else if(name.equals("catch"))catchCount++;
065                    else noCatchCount++;
066                }
067                else noCatchCount++;
068            }
069        }
070        // check if has Content
071        if(catchCount==0 && finallyCount==0)
072            throw new EvaluatorException("Wrong Context, tag cftry must have at least one tag cfcatch inside or a cffinally tag.");
073        if(finallyCount>1)
074            throw new EvaluatorException("Wrong Context, tag cftry can have only one tag cffinally inside.");
075        // check if no has Content
076        if(noCatchCount==0) {
077                ASMUtil.remove(tag);
078        }
079                
080        }
081}