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 gettemplatepath
021 */
022package lucee.runtime.functions.system;
023
024import lucee.commons.lang.StringUtil;
025import lucee.runtime.Mapping;
026import lucee.runtime.MappingImpl;
027import lucee.runtime.PageContext;
028import lucee.runtime.PageSourceImpl;
029import lucee.runtime.PageSourcePool;
030import lucee.runtime.config.ConfigWebUtil;
031import lucee.runtime.exp.PageException;
032import lucee.runtime.ext.function.Function;
033import lucee.runtime.type.Array;
034import lucee.runtime.type.ArrayImpl;
035
036public final class PagePoolList implements Function {
037
038        private static final long serialVersionUID = 7743072823224800862L;
039
040        public static Array call(PageContext pc) throws PageException {
041                ArrayImpl arr = new ArrayImpl();
042                fill(arr,ConfigWebUtil.getAllMappings(pc));
043                return arr;
044        }
045
046        private static void fill(Array arr, Mapping[] mappings) throws PageException {
047                if(mappings==null) return;
048                MappingImpl mapping;
049                for(int i=0;i<mappings.length;i++)   {
050                        mapping=(MappingImpl) mappings[i];
051                        toArray(arr,mapping.getPageSourcePool());
052                }               
053        }
054
055        private static Array toArray(Array arr, PageSourcePool psp) throws PageException {
056                Object[] keys = psp.keys();
057                
058                PageSourceImpl ps;
059                for(int y=0;y<keys.length;y++)       {
060                        ps = (PageSourceImpl) psp.getPageSource(keys[y], false);
061                        if(ps.isLoad())
062                                arr.append(ps.getDisplayPath());
063                }
064                return arr;
065        }
066
067        public static String removeStartingSlash(String virtual) {
068                virtual=virtual.trim();
069                if(StringUtil.startsWith(virtual, '/'))virtual=virtual.substring(1);
070                if(StringUtil.isEmpty(virtual)) return "root";
071                return virtual;
072        }
073}