001    /**
002     * Implements the Cold Fusion Function expandpath
003     */
004    package railo.runtime.functions.system;
005    
006    import java.io.IOException;
007    
008    import railo.commons.io.res.Resource;
009    import railo.commons.io.res.util.ResourceUtil;
010    import railo.commons.lang.StringUtil;
011    import railo.runtime.PageContext;
012    import railo.runtime.PageContextImpl;
013    import railo.runtime.config.ConfigWeb;
014    import railo.runtime.config.ConfigWebImpl;
015    import railo.runtime.config.ConfigWebUtil;
016    import railo.runtime.exp.ExpressionException;
017    import railo.runtime.ext.function.Function;
018    import railo.runtime.type.util.ArrayUtil;
019    
020    public final class ExpandPath implements Function {
021    
022            private static final long serialVersionUID = 6192659914120397912L;
023    
024            public static String call(PageContext pc , String realPath) throws ExpressionException {
025                    
026                    ConfigWeb config=pc.getConfig();
027                    realPath=realPath.replace('\\','/');
028            Resource res;
029            
030            if(StringUtil.startsWith(realPath,'/')) {
031                    PageContextImpl pci=(PageContextImpl) pc;
032                    ConfigWebImpl cwi=(ConfigWebImpl) config;
033                    Resource[] reses = cwi.getPhysicalResources(pc,pc.getApplicationContext().getMappings(),realPath,false,pci.useSpecialMappings(),true);
034                    if(!ArrayUtil.isEmpty(reses)) {
035                            // first check for existing
036                            for(int i=0;i<reses.length;i++){
037                                    if(reses[i].exists()) {
038                                            return toReturnValue(realPath,reses[i]);
039                                    }
040                            }
041                            return toReturnValue(realPath,reses[0]);
042                    }
043            }
044            realPath=ConfigWebUtil.replacePlaceholder(realPath, config);
045            res=pc.getConfig().getResource(realPath);
046            if(res.isAbsolute()) return toReturnValue(realPath,res);
047            
048            res=ResourceUtil.getResource(pc,pc.getBasePageSource());
049            res = res.getParentResource().getRealResource(realPath);
050            return toReturnValue(realPath,res);
051            
052            }
053    
054        private static String toReturnValue(String realPath,Resource res) {
055            String path;
056            char pathChar='/';
057            try {
058                path=res.getCanonicalPath();
059                pathChar=ResourceUtil.FILE_SEPERATOR;
060            } catch (IOException e) {
061                path= res.getAbsolutePath();
062            }
063            boolean pathEndsWithSep=StringUtil.endsWith(path,pathChar);
064            boolean realEndsWithSep=StringUtil.endsWith(realPath,'/');
065            
066            if(realEndsWithSep) {
067                if(!pathEndsWithSep)path=path+pathChar;
068            }
069            else if(pathEndsWithSep) {
070                path=path.substring(0,path.length()-1);
071            }
072            
073            return path;
074        }
075    }