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.TryCatchFinally;
023import lucee.transformer.bytecode.statement.tag.Tag;
024import lucee.transformer.cfml.evaluator.EvaluatorException;
025import lucee.transformer.cfml.evaluator.EvaluatorSupport;
026import lucee.transformer.library.tag.TagLib;
027import lucee.transformer.library.tag.TagLibTag;
028
029
030
031/**
032 * Prueft den Kontext des Tag break.
033 * Das Tag <code>break</code> darf nur innerhalb des Tag <code>loop, while, foreach</code> liegen.
034 */
035public final class Retry extends EvaluatorSupport {
036
037
038        /**
039         * @see lucee.transformer.cfml.evaluator.EvaluatorSupport#evaluate(org.w3c.dom.Element, lucee.transformer.library.tag.TagLibTag)
040         */
041        public void evaluate(Tag tag,TagLibTag libTag) throws EvaluatorException { 
042                String ns=libTag.getTagLib().getNameSpaceAndSeparator();
043                String name=ns+"catch";
044                
045                if(getAncestorCatch(libTag.getTagLib(),tag)==null)
046                        throw new EvaluatorException("Wrong Context, tag "+libTag.getFullName()+" must be inside a "+name+" tag");
047        }
048        
049        public static Statement getAncestorCatch(TagLib tagLib, Statement stat) {
050                String name=tagLib.getNameSpaceAndSeparator()+"catch";
051                Tag tag;
052                Statement parent=stat;
053                while(true)     {
054                        parent=parent.getParent();
055                        if(parent==null)return null;
056                        if(parent instanceof Tag)       {
057                                tag=(Tag) parent;
058                                if(tag.getFullname().equalsIgnoreCase(name))
059                                        return tag;
060                        }
061                        else if(parent instanceof TryCatchFinally)
062                                return parent;
063                }
064        }
065
066}