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.net.ftp;
020
021import java.io.IOException;
022
023import lucee.runtime.PageContext;
024import lucee.runtime.dump.DumpData;
025import lucee.runtime.dump.DumpProperties;
026import lucee.runtime.dump.DumpTable;
027import lucee.runtime.dump.Dumpable;
028import lucee.runtime.dump.SimpleDumpData;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.functions.arrays.ArrayMerge;
031import lucee.runtime.type.Array;
032import lucee.runtime.type.util.ListUtil;
033
034/**
035 * represent a ftp path
036 */
037public final class FTPPath implements Dumpable{
038    
039    private String path;
040    private String name;
041    //private Array arrPath;
042
043    /**
044     * @param current
045     * @param relpath
046     * @throws PageException
047     * @throws IOException 
048     */
049    public FTPPath(AFTPClient client, String relpath) throws PageException, IOException {
050        relpath=relpath.replace('\\','/');
051        Array relpathArr=ListUtil.listToArrayTrim(relpath,'/');
052
053        // relpath is absolute
054        if(relpath.startsWith("/")) {
055            init(relpathArr);
056            return;
057        }
058        String current;
059        if(client==null)current="";
060        else current=client.printWorkingDirectory().replace('\\','/');
061        Array parentArr=ListUtil.listToArrayTrim(current,'/');
062        
063        // Single Dot .
064        if(relpathArr.size()>0&&relpathArr.get(1,"").equals(".")) {
065            relpathArr.removeEL(1);
066        }
067        
068        // Double Dot ..
069        while(relpathArr.size()>0&&relpathArr.get(1,"").equals("..")) {
070            relpathArr.removeEL(1);
071            if(parentArr.size()>0) {
072                parentArr.removeEL(parentArr.size());
073            }
074            else {
075                parentArr.prepend("..");
076            }
077                }
078        ArrayMerge.append(parentArr,relpathArr);
079        init(parentArr);
080    }
081    
082    private void init(Array arr) throws PageException {
083        if(arr.size()>0) {
084                this.name=(String)arr.get(arr.size(),"");
085                arr.removeEL(arr.size());
086                this.path='/'+ListUtil.arrayToList(arr,"/")+'/';
087        }
088        else {
089            this.path="/";
090            this.name="";
091        }
092        //this.arrPath=arr;
093    }
094    
095    /**
096     * @return Returns the name.
097     */
098    public String getName() {
099        return name;
100    }
101    /**
102     * @return Returns the path.
103     */
104    public String getPath() {
105        return path;
106    }
107    
108    @Override
109    public String toString() {
110        return path+name;//+" - "+"path("+getPath()+");"+"name("+getName()+");"+"parent("+getParentPath()+");";
111    }
112    
113    @Override
114        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
115                DumpTable table = new DumpTable("string","#ff6600","#ffcc99","#000000");
116                table.appendRow(1,new SimpleDumpData("FTPPath"),new SimpleDumpData(toString()));
117                return table;
118    }
119}