1 64 65 package com.jcorporate.expresso.kernel.util; 66 67 import com.jcorporate.expresso.kernel.Containable; 68 import com.jcorporate.expresso.kernel.DataContext; 69 import com.jcorporate.expresso.kernel.ExpressoComponent; 70 import com.jcorporate.expresso.kernel.RootContainerInterface; 71 import com.jcorporate.expresso.kernel.digester.ComponentConfig; 72 import com.jcorporate.expresso.kernel.metadata.ComponentMetadata; 73 74 import java.util.ArrayList ; 75 import java.util.ListIterator ; 76 import java.util.Map ; 77 78 79 92 93 public class LocatorUtils { 94 ExpressoComponent root = null; 95 96 102 public LocatorUtils(ExpressoComponent component) { 103 if (component == null) { 104 throw new IllegalArgumentException ("Must have a non-null argument for parameter 'component'"); 105 } 106 boolean done = false; 107 Containable parent = null; 108 while (!done) { 109 parent = component.getParent(); 110 if (parent == null) { 111 done = true; 112 root = component; 113 } else { 114 component = parent; 115 } 116 } 117 } 118 119 120 134 public ExpressoComponent locateComponent(String pathName) { 135 if (pathName == null || pathName.length() == 0) { 136 return root; 137 } 138 139 String stringLeft = pathName; 140 String singleComponent = null; 141 int splitPoint = 0; 142 ExpressoComponent currentComponent = root; 143 do { 144 splitPoint = stringLeft.indexOf("."); 145 if (splitPoint == 0) { 146 throw new IllegalArgumentException ("Malformed Path Request: " + pathName); 147 } 148 if (splitPoint == -1) { 149 singleComponent = stringLeft; 150 stringLeft = ""; 151 } else { 152 singleComponent = stringLeft.substring(0, splitPoint); 153 stringLeft = stringLeft.substring(splitPoint + 1); 154 } 155 156 if (!(currentComponent instanceof Containable)) { 157 throw new IllegalArgumentException ("Can't navigate below component: " 158 + currentComponent.getMetaData().getName() + 159 "it is not a component container"); 160 } 161 162 Containable currentContainer = (Containable) currentComponent; 163 Map children = currentContainer.getContainerImplementation().getChildComponents(); 164 if (!children.containsKey(singleComponent)) { 165 throw new IllegalArgumentException ("Unable to locate subcomponent " + 166 singleComponent + " as a subcomponent of " + currentComponent.getMetaData().getName()); 167 } 168 169 currentComponent = (ExpressoComponent) children.get(singleComponent); 170 171 } while (splitPoint > -1); 172 173 return currentComponent; 174 } 175 176 177 191 public ComponentConfig locateConfiguration(String pathName) { 192 if (pathName == null || pathName.length() == 0) { 193 return ((RootContainerInterface) root).getExpressoServicesConfig().getRootConfig(); 194 } 195 196 String stringLeft = pathName; 197 String singleComponent = null; 198 int splitPoint = 0; 199 ComponentConfig currentComponent = ((RootContainerInterface) root) 200 .getExpressoServicesConfig().getRootConfig(); 201 do { 202 splitPoint = stringLeft.indexOf("."); 203 if (splitPoint == 0) { 204 throw new IllegalArgumentException ("Malformed Path Request: " + pathName); 205 } 206 if (splitPoint == -1) { 207 singleComponent = stringLeft; 208 stringLeft = ""; 209 } else { 210 singleComponent = stringLeft.substring(0, splitPoint); 211 stringLeft = stringLeft.substring(splitPoint + 1); 212 } 213 214 ComponentConfig testconfig = currentComponent.getChildComponent(singleComponent); 215 if (testconfig == null) { 216 throw new IllegalArgumentException ("Unable to locate subcomponent configuration " + 217 singleComponent + " as a subcomponent of " + currentComponent.getName()); 218 219 } 220 currentComponent = testconfig; 221 222 } while (splitPoint > -1); 223 224 return currentComponent; 225 } 226 227 241 public ComponentMetadata locateMetadata(String pathName) { 242 ExpressoComponent component = this.locateComponent(pathName); 243 return component.getMetaData(); 244 } 245 246 247 254 public String getPath(ExpressoComponent component) { 255 Containable c = component.getParent(); 256 if (c == null) { 257 return ""; 258 } 259 260 ExpressoComponent currentComponent = component; 261 ArrayList al = new ArrayList (); 262 while (c != null) { 264 al.add(currentComponent.getMetaData().getName()); 265 currentComponent = c; 266 c = currentComponent.getParent(); 267 } 268 269 FastStringBuffer path = FastStringBuffer.getInstance(); 271 boolean needPeriod = false; 272 for (ListIterator i = al.listIterator(al.size()); i.hasPrevious();) { 273 String value = (String ) i.previous(); 274 if (needPeriod) { 275 path.append("."); 276 } else { 277 needPeriod = true; 278 } 279 280 path.append(value); 281 } 282 283 String returnValue = path.toString(); 284 path.release(); 285 return returnValue; 286 } 287 288 296 public String getDataContextName(ExpressoComponent component) { 297 return getDataContext(component).getMetaData().getName(); 298 299 } 300 301 302 309 public DataContext getDataContext(ExpressoComponent component) { 310 if (component == null) { 311 throw new IllegalArgumentException ("Must have a non-null argument for parameter 'component'"); 312 } 313 boolean done = false; 314 Containable parent = null; 315 while (!done) { 316 parent = component.getParent(); 317 if (parent instanceof DataContext) { 318 return (DataContext) parent; 319 } 320 321 if (parent == null) { 322 done = true; 323 } 324 component = parent; 325 } 326 327 return null; 328 } 329 } 330 331 | Popular Tags |