001    /**
002     * Implements the CFML Function expandpath
003     */
004    package railo.runtime.functions.system;
005    
006    
007    import railo.commons.io.SystemUtil;
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.PageSource;
013    import railo.runtime.ext.function.Function;
014    
015    public final class ContractPath implements Function {
016            public static String call(PageContext pc , String absPath) {
017                    return call(pc, absPath,false);
018            }
019            
020            public static String call(PageContext pc , String absPath, boolean placeHolder) {
021                    Resource res = ResourceUtil.toResourceNotExisting(pc, absPath);
022                    if(!res.exists()) return absPath;
023                    
024                    if(placeHolder){
025                            String cp = SystemUtil.addPlaceHolder(res, null);
026                            if(!StringUtil.isEmpty(cp))return cp;
027                    }
028                    
029                    //Config config=pc.getConfig();
030                    PageSource ps = pc.toPageSource(res,null);
031                    if(ps==null) return absPath;
032                    
033                    String realPath = ps.getRealpath();
034                    realPath=realPath.replace('\\', '/');
035                    if(StringUtil.endsWith(realPath,'/'))realPath=realPath.substring(0,realPath.length()-1);
036                    
037                    String mapping=ps.getMapping().getVirtual();
038                    mapping=mapping.replace('\\', '/');
039                    if(StringUtil.endsWith(mapping,'/'))mapping=mapping.substring(0,mapping.length()-1);
040                    
041                    return mapping+realPath;
042            }
043    }