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