001    package railo.runtime.net.ftp;
002    
003    import railo.runtime.PageContext;
004    import railo.runtime.dump.DumpData;
005    import railo.runtime.dump.DumpProperties;
006    import railo.runtime.dump.DumpTable;
007    import railo.runtime.dump.Dumpable;
008    import railo.runtime.dump.SimpleDumpData;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.functions.arrays.ArrayMerge;
011    import railo.runtime.type.Array;
012    import railo.runtime.type.util.ListUtil;
013    
014    /**
015     * represent a ftp path
016     */
017    public final class FTPPath implements Dumpable{
018        
019        private String path;
020        private String name;
021        //private Array arrPath;
022    
023        /**
024         * @param current
025         * @param realpath
026         * @throws PageException
027         */
028        public FTPPath(String current, String realpath) throws PageException {
029            realpath=realpath.replace('\\','/');
030            //if(realpath.startsWith("./")) realpath=realpath.substring(2);
031            //if(realpath.startsWith(".")) realpath=realpath.substring(1);
032            Array realpathArr=ListUtil.listToArrayTrim(realpath,'/');
033    
034            // realpath is absolute
035            if(realpath.startsWith("/")) {
036                init(realpathArr);
037                return;
038            }
039            if(current==null)current="";
040            else current=current.replace('\\','/');
041            Array parentArr=ListUtil.listToArrayTrim(current,'/');
042            
043            // Single Dot .
044            if(realpathArr.size()>0&&realpathArr.get(1,"").equals(".")) {
045                realpathArr.removeEL(1);
046            }
047            
048            // Double Dot ..
049            while(realpathArr.size()>0&&realpathArr.get(1,"").equals("..")) {
050                realpathArr.removeEL(1);
051                if(parentArr.size()>0) {
052                    parentArr.removeEL(parentArr.size());
053                }
054                else {
055                    parentArr.prepend("..");
056                }
057                    }
058            ArrayMerge.append(parentArr,realpathArr);
059            init(parentArr);
060        }
061        
062        private void init(Array arr) throws PageException {
063            if(arr.size()>0) {
064                    this.name=(String)arr.get(arr.size(),"");
065                    arr.removeEL(arr.size());
066                    this.path='/'+ListUtil.arrayToList(arr,"/")+'/';
067            }
068            else {
069                this.path="/";
070                this.name="";
071            }
072            //this.arrPath=arr;
073        }
074        
075        /**
076         * @return Returns the name.
077         */
078        public String getName() {
079            return name;
080        }
081        /**
082         * @return Returns the path.
083         */
084        public String getPath() {
085            return path;
086        }
087        
088        @Override
089        public String toString() {
090            return path+name;//+" - "+"path("+getPath()+");"+"name("+getName()+");"+"parent("+getParentPath()+");";
091        }
092        
093        @Override
094            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
095                    DumpTable table = new DumpTable("string","#ff6600","#ffcc99","#000000");
096                    table.appendRow(1,new SimpleDumpData("FTPPath"),new SimpleDumpData(toString()));
097                    return table;
098        }
099    }