1 23 24 package org.apache.slide.search.basic; 25 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.StringTokenizer ; 31 32 import org.apache.slide.common.Scope; 33 import org.apache.slide.search.InvalidScopeException; 34 35 41 public class QueryTree { 42 43 private TokenizedScope topNode; 44 45 private List allQueryTreeNodes = new ArrayList (); 46 47 48 54 61 QueryTree (Enumeration stores, Scope scope, Scope [] excluded) 62 throws InvalidScopeException 63 { 64 topNode = new TokenizedScope (scope); 66 67 while (stores.hasMoreElements()) { 68 Scope configuredStore = (Scope)stores.nextElement(); 69 TokenizedScope tConfStore = new TokenizedScope (configuredStore); 70 if (tConfStore.isChildOf (topNode) && tConfStore.isNotExcluded (excluded) ) { 71 allQueryTreeNodes.add (configuredStore); 72 } 73 } 74 allQueryTreeNodes.add (scope); 75 76 } 77 78 86 public boolean hasChildren (Scope scopeToBeChecked) { 87 boolean result = false; 88 TokenizedScope tScopeToBeChecked = new TokenizedScope (scopeToBeChecked); 89 Iterator it = allQueryTreeNodes.iterator(); 90 while (it.hasNext()) { 91 TokenizedScope ts = new TokenizedScope ((Scope)it.next()); 92 if (ts.isChildOf (tScopeToBeChecked)) 93 return true; 94 } 95 96 return false; 97 } 98 99 108 public int relativeDepth (Scope scope) { 109 TokenizedScope tScope = new TokenizedScope (scope); 110 boolean contains = allQueryTreeNodes.contains (tScope); 111 112 return tScope.depth - topNode.depth; 113 } 114 115 116 122 public Iterator iterator () { 123 return allQueryTreeNodes.iterator(); 124 } 125 126 127 133 class TokenizedScope { 134 private int depth; 135 Scope scope; 136 137 TokenizedScope (Scope scope) { 138 this.scope = scope; 139 StringTokenizer st = new StringTokenizer (scope.toString(), "/"); 140 int noOfTokens = st.countTokens(); 141 depth = noOfTokens; 142 } 143 144 152 public boolean equals (Object o) { 153 return scope.equals(o); 154 } 155 156 162 public String toString () { 163 return scope.toString(); 164 } 165 166 174 public boolean isChildOf (TokenizedScope tScope) { 175 if (depth > tScope.depth && toString().startsWith(tScope.toString())) { 176 return true; 177 } 178 else { 179 return false; 180 } 181 } 182 183 191 public boolean isNotExcluded (Scope []excluded) { 192 for (int i = 0; i < excluded.length; i++) { 193 if (this.toString().startsWith(excluded [i].toString())) { 194 return false; 195 } 196 } 197 return true; 198 } 199 } 200 } 201 202 203 | Popular Tags |