001    /**
002     * Implements the Cold Fusion Function getbasetagdata
003     */
004    package railo.runtime.functions.other;
005    
006    import javax.servlet.jsp.tagext.Tag;
007    
008    import railo.commons.lang.StringUtil;
009    import railo.runtime.PageContext;
010    import railo.runtime.exp.ExpressionException;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.ext.function.Function;
013    import railo.runtime.tag.CFTag;
014    import railo.runtime.tag.CFTagCore;
015    import railo.runtime.type.Struct;
016    
017    public final class GetBaseTagData implements Function {
018            
019        public static Struct call(PageContext pc , String tagName) throws PageException {
020                    return call(pc,tagName,-1);
021            }
022            
023        public static Struct call(PageContext pc , String tagName, double minLevel) throws PageException {
024            //print.out("tagname:"+tagName);
025            CFTag tag=getParentCFTag(pc.getCurrentTag(), tagName, (int)minLevel);
026            if(tag==null) throw new ExpressionException("can't find base tag with name ["+tagName+"]");
027            return tag.getVariablesScope();
028            }
029        
030        public synchronized static CFTag getParentCFTag(Tag srcTag,String trgTagName, int minLevel) {
031            String pureName=trgTagName;
032            int level=0;
033            CFTag cfTag;
034            while(srcTag!=null) {
035                    // cftag
036                    if(srcTag instanceof CFTag && minLevel<=++level) {
037                    cfTag=(CFTag)srcTag;
038                    if(cfTag instanceof CFTagCore){
039                            CFTagCore tc=(CFTagCore) cfTag;
040                            if(tc!=null && (tc.getName()+"").equalsIgnoreCase(pureName))
041                                    return cfTag;
042                            if(StringUtil.startsWithIgnoreCase(pureName,"cf")) {
043                            pureName=pureName.substring(2);
044                        }
045                            if(tc!=null && (tc.getName()+"").equalsIgnoreCase(pureName))
046                                    return cfTag;
047                    }
048                    else if( cfTag.getAppendix().equalsIgnoreCase(pureName)) {
049                        return cfTag;
050                    }
051                    else if(StringUtil.startsWithIgnoreCase(pureName,"cf_")) {
052                        pureName=pureName.substring(3);
053                        if(cfTag.getAppendix().equalsIgnoreCase(pureName)) 
054                            return cfTag;
055                    }
056                }
057                srcTag=srcTag.getParent();
058                
059            }
060            return null;
061        }
062    }