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 getbasetaglist
021 */
022package lucee.runtime.functions.other;
023
024import java.util.Iterator;
025import java.util.Map;
026
027import javax.servlet.jsp.tagext.Tag;
028
029import lucee.runtime.PageContext;
030import lucee.runtime.config.ConfigImpl;
031import lucee.runtime.ext.function.Function;
032import lucee.runtime.ext.tag.AppendixTag;
033import lucee.runtime.tag.CFImportTag;
034import lucee.runtime.tag.CFTag;
035import lucee.runtime.tag.CFTagCore;
036import lucee.runtime.tag.Module;
037import lucee.runtime.type.util.ListUtil;
038import lucee.transformer.library.tag.TagLib;
039import lucee.transformer.library.tag.TagLibTag;
040
041public final class GetBaseTagList implements Function {
042    public static String call(PageContext pc) {
043        return call(pc,",");
044    }
045    public static String call(PageContext pc, String delimiter) {
046        Tag tag=pc.getCurrentTag();
047        StringBuilder sb=new StringBuilder();
048        while(tag!=null) {
049                if(sb.length()>0)sb.append(delimiter);
050            sb.append(getName(pc,tag));
051            tag=tag.getParent();
052        }
053        return sb.toString();
054    }
055    private static String getName(PageContext pc, Tag tag) {
056        Class clazz = tag.getClass();
057        if(clazz==CFImportTag.class)clazz=CFTag.class;
058        String className=clazz.getName();
059        TagLib[] tlds = ((ConfigImpl)pc.getConfig()).getTLDs();
060        TagLibTag tlt;
061        
062        
063        
064        for(int i=0;i<tlds.length;i++) {
065            //String ns = tlds[i].getNameSpaceAndSeparator();
066            
067            
068            Map tags = tlds[i].getTags();
069            Iterator it = tags.keySet().iterator();
070            
071            while(it.hasNext()){
072                tlt=(TagLibTag) tags.get(it.next());
073                if(tlt.getTagClassName().equals(className)) {
074                    // custm tag
075                        if(tag instanceof AppendixTag) {
076                        AppendixTag atag=(AppendixTag)tag;
077                        if(atag.getAppendix()!=null && !(tag instanceof Module)) {
078                            return tlt.getFullName().toUpperCase()+atag.getAppendix().toUpperCase();
079                        }
080                    }
081                        // built in cfc based custom tag
082                        if(tag instanceof CFTagCore) {
083                                if(((CFTagCore)tag).getName().equals(tlt.getAttribute("__name").getDefaultValue()))
084                                        return tlt.getFullName().toUpperCase();
085                                continue;
086                        }
087                        
088                        return tlt.getFullName().toUpperCase();
089                }
090            }
091        }
092        return ListUtil.last(className,".",true).toUpperCase();
093        
094    }
095}