001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.console.util;
018    
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    import java.util.Hashtable;
022    import java.util.Collections;
023    
024    public class StringTree implements Comparable{
025        public String name = null;
026        public ArrayList childs = new ArrayList();    
027        public StringTree(String nm, ArrayList elements){
028            name = nm;
029            childs = elements;
030        }
031        public StringTree(String nm){
032            name = nm;        
033        }
034        public String getName(){
035            return name;
036        }
037        public void setName(String nm){
038            name = nm;;
039        }
040        public ArrayList getChilds(){
041            return childs;
042        }
043        public void setChilds(ArrayList elements){
044            childs = elements;
045        }
046        public void addChild(Object obj){
047            childs.add(obj);
048        }
049        public StringTree findNode(String id){
050            if(id == null)return null;
051            if(name != null && name.equals(id))
052                return this;
053            Iterator iter = childs.iterator();
054            while(iter.hasNext()){
055                Object obj = iter.next();
056                if(obj instanceof StringTree){
057                    StringTree tree = ((StringTree)obj).findNode(id);
058                    if(tree != null)return tree;
059                }                
060            }
061            return null;
062        }
063        public boolean equals(Object node){
064            if(node instanceof StringTree && ((StringTree)node).name.equals(this.name))
065                return true;
066            return false;
067        }
068               
069        public String toJSONObject(String prependId){
070            return toJSONObject(prependId, null);
071        }
072        public String toJSONObject(String prependId, Hashtable htLinks){
073            return toJSONObject(prependId, htLinks, false);
074        }
075               
076        public String toJSONObject(String prependId, Hashtable htLinks, boolean level1){
077            StringBuffer stb = new StringBuffer();
078            if(htLinks != null){
079                if(!name.startsWith("class ") && !name.startsWith("interface ") && !name.equals("Classes") && !name.equals("Interfaces") && htLinks.containsKey(name) && !level1){
080                    stb.append("{title:'link::");
081                    stb.append(htLinks.get(name));
082                    stb.append("',widgetId:'");
083                    stb.append(prependId);
084                    stb.append("'}");
085                    return stb.toString();
086                }
087                else {
088                    htLinks.put(name, prependId);
089                }       
090            }        
091            stb.append("{title:'");
092            if(name != null)
093                stb.append(name);
094            stb.append("',widgetId:'");
095            stb.append(prependId);
096            if(childs == null || childs.size() == 0){
097                stb.append("',children:[]}");
098            }
099            else
100            {
101                stb.append("',children:[");
102                Collections.sort(childs);
103                for(int i=0;i<childs.size();i++){
104                    Object obj = childs.get(i);
105                    if(i !=0 )stb.append(",");
106                    if(obj instanceof StringTree)
107                        stb.append(((StringTree)obj).toJSONObject(prependId+"."+i, htLinks)); 
108                    else
109                    {
110                        stb.append("{title:'");
111                        stb.append((String)obj);
112                        stb.append("',widgetId:'");
113                        stb.append(prependId+"."+i);
114                        stb.append("'}");
115                    }
116                }
117                stb.append("]}");
118            }
119            return stb.toString();
120        }
121        public int compareTo(Object obj){
122            if(name == null)
123                return -1;
124            return name.compareTo(((StringTree)obj).getName());
125        }
126    }