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 **/
019/**
020 * Implements the CFML Function getbasetagdata
021 */
022package lucee.runtime.functions.other;
023
024import javax.servlet.jsp.tagext.Tag;
025
026import lucee.commons.lang.StringUtil;
027import lucee.runtime.PageContext;
028import lucee.runtime.exp.ExpressionException;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.ext.function.Function;
031import lucee.runtime.tag.CFTag;
032import lucee.runtime.tag.CFTagCore;
033import lucee.runtime.type.Struct;
034
035public final class GetBaseTagData implements Function {
036        
037        private static final long serialVersionUID = -7016207088098049143L;
038
039        public static Struct call(PageContext pc , String tagName) throws PageException {
040                return call(pc,tagName,-1);
041        }
042        
043    public static Struct call(PageContext pc , String tagName, double minLevel) throws PageException {
044        CFTag tag=getParentCFTag(pc.getCurrentTag(), tagName, (int)minLevel);
045        if(tag==null) throw new ExpressionException("can't find base tag with name ["+tagName+"]");
046        return tag.getVariablesScope();
047        }
048    
049    public synchronized static CFTag getParentCFTag(Tag tag,String trgTagName, int minLevel) {
050        String pureName=trgTagName;
051        int level=0;
052        CFTag cfTag;
053        while(tag!=null) {
054                if(tag instanceof CFTag && minLevel<=(level++)) {
055                cfTag=(CFTag)tag;
056                if(cfTag instanceof CFTagCore){
057                
058                        CFTagCore tc=(CFTagCore) cfTag;
059                        
060                        if((tc.getName()+"").equalsIgnoreCase(pureName))
061                                return cfTag;
062                        if(StringUtil.startsWithIgnoreCase(pureName,"cf")) {
063                        pureName=pureName.substring(2);
064                    }
065                        if((tc.getName()+"").equalsIgnoreCase(pureName))
066                                return cfTag;
067                }
068                else if( cfTag.getAppendix().equalsIgnoreCase(pureName)) {
069                    return cfTag;
070                }
071                else if(StringUtil.startsWithIgnoreCase(pureName,"cf_")) {
072                    pureName=pureName.substring(3);
073                    if(cfTag.getAppendix().equalsIgnoreCase(pureName)) 
074                        return cfTag;
075                }
076            }
077                tag=tag.getParent();
078        }
079        return null;
080    }
081}