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.intergral.fusiondebug.server.util;
020
021import java.util.Iterator;
022
023import com.intergral.fusiondebug.server.IFDValue;
024import com.intergral.fusiondebug.server.IFDVariable;
025
026public class FDDump {
027
028        //private static PrintStream out=System.out;
029
030        public static void dump(IFDVariable var) {
031                System.out.print(toString(var));
032        }
033
034        public static String toString(Object value) {
035                StringBuffer sb=new StringBuffer();
036                dump(sb,value, 0);
037                return sb.toString();
038        }
039        
040        public static String toString(IFDVariable var) {
041                StringBuffer sb=new StringBuffer();
042                dump(sb,var, 0);
043                return sb.toString();
044        }
045        
046
047        private static void dump(StringBuffer sb,Object value,int level) {
048                if(value instanceof IFDValue) dump(sb, (IFDValue)value, level);
049                else dump(sb, (IFDVariable)value, level);
050        }
051        
052        private static void dump(StringBuffer sb,IFDValue value,int level) {
053                for(int i=0;i<level;i++){
054                        sb.append(" - ");
055                }
056                
057                sb.append(value.toString());
058                sb.append("\n");
059                if(value.hasChildren()){ 
060                        Iterator it = value.getChildren().iterator();
061                        while(it.hasNext()){
062                                Object o=it.next();
063                                dump(sb,(IFDVariable) o,level+1);
064                        }
065                }
066        }
067        
068        private static void dump(StringBuffer sb,IFDVariable var,int level) {
069                for(int i=0;i<level;i++){
070                        sb.append(" - ");
071                }
072                sb.append(var.getName());
073                sb.append(":");
074                IFDValue value = var.getValue();
075                
076                sb.append(value.toString());
077                sb.append("\n");
078                //print.err(value.getClass().getName());
079                if(value.hasChildren()){ 
080                        Iterator it = value.getChildren().iterator();
081                        while(it.hasNext()){
082                                Object o=it.next();
083                                //print.err(o.getClass().getName());
084                                dump(sb,(IFDVariable) o,level+1);
085                                //dump(sb,(IFDVariable) it.next(),level+1);
086                        }
087                }
088        }
089        
090        /*public static void main(String[] args) throws PageException {
091                Array arr = new ArrayImpl();
092                arr.setEL(1, "aaa");
093                arr.setEL(2, Boolean.TRUE);
094                arr.setEL(5, Constants.INTEGER_3);
095                
096                Array sub1 = new ArrayImpl();
097                sub1.setEL(1, "ddd");
098                arr.setEL(6, sub1);
099                
100                Struct sct=new StructImpl();
101                sct.set("susi1", "eee");
102                sct.set("susi2", "fff");
103                arr.setEL(7, sct);
104                
105                Key aaa = KeyImpl.intern("aaa");
106                Key bbb = KeyImpl.intern("bbb");
107                Query qry=new QueryImpl(new Collection.Key[]{aaa,bbb},2,"quererli");
108                qry.setAt(aaa, 1, "a1");
109                qry.setAt(bbb, 1, "b1");
110                qry.setAt(aaa, 2, "a2");
111                qry.setAt(bbb, 2, sct);
112                arr.setEL(8, qry);
113                
114                //arr.setEL(9, new StringBuffer());
115
116                dump(new FDVariable(null,"susi",FDCaster.toFDValue(null,"susi",qry)));
117                //dump(new FDVariable(null,"susi",FDCaster.toFDValue(null,"susi",arr)));
118                
119                //dump(FDCaster.toFDVariable("susi",arr));
120        }*/
121}