001    package railo.intergral.fusiondebug.server.type.coll;
002    
003    import java.util.ArrayList;
004    import java.util.Arrays;
005    import java.util.List;
006    
007    import railo.intergral.fusiondebug.server.type.FDValueSupport;
008    import railo.intergral.fusiondebug.server.type.FDVariable;
009    import railo.intergral.fusiondebug.server.util.FDCaster;
010    import railo.runtime.Component;
011    import railo.runtime.ComponentPro;
012    import railo.runtime.type.Array;
013    import railo.runtime.type.Collection;
014    import railo.runtime.type.Collection.Key;
015    import railo.runtime.type.Struct;
016    import railo.runtime.type.comparator.TextComparator;
017    
018    import com.intergral.fusiondebug.server.FDLanguageException;
019    import com.intergral.fusiondebug.server.FDMutabilityException;
020    import com.intergral.fusiondebug.server.IFDStackFrame;
021    
022    public class FDCollection extends FDValueSupport {
023    
024            private static final int INTERVALL = 10;
025            private ArrayList children;
026            private Collection coll;
027            private String name;
028            private Key[] keys;
029    
030            /**
031             * Constructor of the class
032             * @param frame 
033             * @param name 
034             * @param name
035             * @param coll
036             */
037            
038    
039            public FDCollection(IFDStackFrame frame, String name, Collection coll) {
040                    this(frame,name,coll,keys(coll));
041            }
042    
043            public FDCollection(IFDStackFrame frame, String name, Collection coll, Key[] keys) {
044                    
045                    this.name=name;
046                    this.coll=coll;
047                    this.keys=keys;
048                    //Key[] keys = coll.keys();
049                    children=new ArrayList();
050                    
051                    int intervall=INTERVALL;
052                    while(intervall*intervall<keys.length)
053                            intervall*=intervall;
054                    
055                    if(keys.length>intervall){
056                            FDCollection node;
057                            
058                            
059                            int len=keys.length;
060                            
061                            int max;
062                            for(int i=0;i<len;i+=intervall)      {
063                                    max=(i+(intervall))<len?(intervall):len-i;
064                                    Key[] skeys=new Key[max];
065                                    for(int y=0;y<max;y++)       {
066                                            skeys[y]=keys[i+y];
067                                    }                               
068                                    node = new FDCollection(frame,"Rows",coll,skeys);
069                                    children.add(new FDVariable(frame,node.getName(),node));
070                            }
071                    }
072                    else {
073                            FDCollectionNode node;
074                            for(int i=0;i<keys.length;i++){
075                                    node = new FDCollectionNode(frame,coll,keys[i]);
076                                    children.add(new FDVariable(frame,node.getName(),node));
077                            }
078                    }
079            }
080            
081            private static Key[] keys(Collection coll) {
082                    Key[] keys=coll.keys();
083                    if(coll instanceof Array) return keys;
084                    TextComparator comp=new TextComparator(true,true);
085                    Arrays.sort(keys,comp);
086                    return keys;
087            }
088    
089            /**
090             * @see com.intergral.fusiondebug.server.IFDVariable#getChildren()
091             */
092            public List getChildren() {
093                    return children;
094            }
095    
096     
097            public IFDStackFrame getStackFrame() {
098                    // TODO Auto-generated method stub
099                    return null;
100            }
101    
102            /**
103             * @see com.intergral.fusiondebug.server.IFDValue#isMutable()
104             */
105            public boolean isMutable() {
106                    return false;
107            }
108    
109            public void set(String arg0) throws FDMutabilityException,FDLanguageException {
110                    throw new FDMutabilityException();
111            }
112    
113            /**
114             * @see com.intergral.fusiondebug.server.IFDValue#hasChildren()
115             */
116            public boolean hasChildren() {
117                    return true;
118            }
119            
120    
121            /* *
122             * @see java.lang.Object#toString()
123             * /
124            public String toString() {
125                    return "<CFML "+StringUtil.ucFirst(Caster.toTypeName(coll))+">";
126            }*/
127            
128    
129            /**
130             * @see java.lang.Object#toString()
131             */
132            public String toString() {
133                    if(coll instanceof Array)
134                            return "["+fromto()+"]";
135                    if(coll instanceof Component){
136                            ComponentPro c=(ComponentPro) coll;
137                            return "Component "+c.getName()+"("+c.getPageSource().getDisplayPath()+")";
138                    }
139                    if(coll instanceof Struct)
140                            return "{"+fromto()+"}";
141                    return FDCaster.serialize(coll);
142            }
143            
144    
145            private String fromto() {
146                    StringBuffer sb=new StringBuffer();
147                    for(int i=0;i<keys.length;i++){
148                            if(i!=0)sb.append(",");
149                            sb.append(keys[i].toString());
150                    }
151                    return keys[0]+" ... "+keys[keys.length-1];
152            }
153    
154    
155            /**
156             * @see railo.intergral.fusiondebug.server.type.FDValueSupport#getName()
157             */
158            public String getName() {
159                    return name;
160            }
161    }