C:\Java WorkShop\Lexx\src\lexx\trie\RootNode.java

1    package lexx.trie; 
2     
3    import java.util.ArrayList; 
4    import java.util.Iterator; 
5    import java.util.List; 
6    import java.util.Vector; 
7     
8    import lexx.api.ClassName; 
9    import lexx.api.Directory; 
10   import java.io.File; 
11    
12   /** 
13    * <p>Create a Trie data structure</p> 
14    * <p>The purpose of this class is to produce a tree like structure for accessing text quickly</p> 
15    * <p>Copyright (c) 2002-2003</p> 
16    * @since 18/02/2003 
17    * @author Mohammed Imran 
18    * @version 1.0 
19    */ 
20    
21   public final class RootNode extends TrieNode implements java.io.Serializable, 
22       Cloneable 
23   { 
24     private static boolean allowDuplicates = false; 
25     private final static org.apache.log4j.Logger log = org.apache.log4j.Logger. 
26         getLogger(RootNode.class); 
27    
28     public static boolean canHaveDuplicates() 
29     { 
30       return allowDuplicates; 
31     } 
32    
33     public static void setDuplicates(boolean canHave) 
34     { 
35       allowDuplicates = canHave; 
36     } 
37    
38     private String text = ""; 
39    
40     protected RootNode() 
41     { 
42       childNodes = new CharMap(); 
43     } 
44    
45     public RootNode(String toStringText) 
46     { 
47       text = toStringText; 
48       childNodes = new CharMap(); 
49     } 
50    
51     public final Object cloneToDisk(String dir, int maxLevel) 
52     { 
53       RootNode node = new RootNode(); 
54    
55       if(obj instanceof ClassName) 
56       { 
57         node.obj = ( (ClassName) obj).clone(); 
58       } 
59       else 
60       { 
61         if(obj instanceof TrieNode) 
62         { 
63           node.obj = ( (TrieNode) obj).clone(); 
64         } 
65         else 
66         { 
67           if(obj instanceof List || obj instanceof Vector || 
68              obj instanceof ArrayList) 
69           { 
70             Vector v = new Vector(); 
71             Directory.clone( (List) obj, v); 
72             node.obj = v; 
73           } 
74           else 
75           { 
76             node.obj = obj; 
77           } 
78         } 
79       } 
80       node.level = level; 
81       node.letter = letter; 
82    
83       if(level <= maxLevel) 
84       { 
85         CharMapDisk d = new CharMapDisk(dir); 
86    
87         Iterator it = this.childNodes.iterator(); 
88    
89         while(it.hasNext()) 
90         { 
91           TrieNode tmp = (TrieNode) it.next(); 
92    
93           if(level >= maxLevel) 
94           { 
95             d.put(tmp.letter, tmp.clone()); 
96           } 
97           else 
98           { 
99             d.put(tmp.letter, 
100                  tmp.cloneToDisk(d.getCurrentDir().toString() + File.separatorChar + tmp.letter, 
101                                  maxLevel)); 
102          } 
103        } 
104      } 
105   
106      if(level > maxLevel) 
107      { 
108        node.childNodes = (CharMapBase) childNodes.clone(); 
109   
110      } 
111      return node; 
112    } 
113   
114    public final void add(String key, Object obj, boolean isMutable) 
115    { 
116      if(obj.getClass() == Vector.class) 
117      { 
118        throw new RuntimeException("Cannot add a vector to a TRIE"); 
119      } 
120   
121      Object o = childNodes.get(key.charAt(0)); 
122      if(o == null) 
123      { 
124        childNodes.put(key.charAt(0), new TrieNode(key, obj, 0, isMutable)); 
125      } 
126      else 
127      { 
128        ( (TrieNode) o).add(key, obj, 0, isMutable); //match tree 
129      } 
130    } 
131   
132    public final Object clone() 
133    { 
134      RootNode node = new RootNode(); 
135      if(obj instanceof ClassName) 
136      { 
137        node.obj = ( (ClassName) obj).clone(); 
138      } 
139      else 
140      { 
141        if(obj instanceof TrieNode) 
142        { 
143          node.obj = ( (TrieNode) obj).clone(); 
144        } 
145        else 
146        { 
147          if(obj instanceof List || obj instanceof Vector || 
148             obj instanceof ArrayList) 
149          { 
150            Vector v = new Vector(); 
151            Directory.clone( (List) obj, v); 
152            node.obj = v; 
153          } 
154          else 
155          { 
156            node.obj = obj; 
157          } 
158        } 
159      } 
160      node.level = level; 
161      node.letter = letter; 
162      node.childNodes = (CharMap) childNodes.clone(); 
163   
164      return node; 
165    } 
166   
167    public final Trie getNode(String key) 
168    { 
169      if(key == null || "".equals(key)) 
170      { 
171        return null; 
172      } 
173   
174      Object child = childNodes.get(key.charAt(0)); 
175      if(child == null) 
176      { 
177        return null; 
178      } 
179   
180      Trie nextTree = (Trie) child; 
181   
182      return nextTree.getNode(key); 
183    } 
184   
185    public String toString() 
186    { 
187      return text; 
188    } 
189  }