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.runtime.tag;
020
021import java.io.File;
022
023import lucee.commons.lang.StringUtil;
024import lucee.runtime.PageContextImpl;
025import lucee.runtime.PageSource;
026import lucee.runtime.config.ConfigWeb;
027import lucee.runtime.customtag.CustomTagUtil;
028import lucee.runtime.customtag.InitFile;
029import lucee.runtime.exp.ExpressionException;
030import lucee.runtime.exp.PageException;
031import lucee.runtime.type.util.ListUtil;
032
033/**
034 * To create cfimport custom tags
035 */
036public final class CFImportTag extends CFTag {
037
038    
039        @Override
040        public void initFile() throws PageException {
041                ConfigWeb config = pageContext.getConfig();
042        
043                String[] filenames=CustomTagUtil.getFileNames(config, getAppendix());// = appendix+'.'+config.getCFMLExtension();
044        
045                
046                String strRelPathes=attributesScope.remove("__custom_tag_path").toString();
047                String[] relPathes=ListUtil.listToStringArray(strRelPathes, File.pathSeparatorChar);
048            for(int i=0;i<relPathes.length;i++){
049                if(!StringUtil.endsWith(relPathes[i],'/'))relPathes[i]=relPathes[i]+"/";
050            }
051            
052            // MUSTMUST use cache like regular ct
053                // page source
054            PageSource ps;
055            for(int rp=0;rp<relPathes.length;rp++){
056                    for(int fn=0;fn<filenames.length;fn++){
057                    ps=((PageContextImpl)pageContext).getRelativePageSourceExisting(relPathes[rp]+filenames[fn]);
058                    if(ps!=null){
059                        source=new InitFile(ps,filenames[fn],filenames[fn].endsWith('.'+config.getCFCExtension()));
060                        return;
061                    }
062                        } 
063            }
064            
065        // EXCEPTION
066            // message
067            
068        StringBuffer msg=new StringBuffer("could not find template [");
069        msg.append(CustomTagUtil.getDisplayName(config, getAppendix()));
070        msg.append("] in the following directories [");
071        msg.append(strRelPathes.replace(File.pathSeparatorChar, ','));
072        msg.append(']');
073        
074            
075                throw new ExpressionException(msg.toString(),CustomTagUtil.getDetail(config));
076            
077        }
078
079}