1 package org.apache.velocity.runtime; 2 3 18 19 import java.util.Hashtable ; 20 21 import java.io.StringReader ; 22 import java.io.BufferedReader ; 23 24 import org.apache.velocity.runtime.directive.VelocimacroProxy; 25 import org.apache.velocity.runtime.parser.node.SimpleNode; 26 import org.apache.velocity.util.StringUtils; 27 28 import org.apache.velocity.context.InternalContextAdapter; 29 30 46 public class VelocimacroManager 47 { 48 private RuntimeServices rsvc = null; 49 private static String GLOBAL_NAMESPACE = ""; 50 51 private boolean registerFromLib = false; 52 53 54 private Hashtable namespaceHash = new Hashtable (); 55 56 57 private Hashtable libraryMap = new Hashtable (); 58 59 63 private boolean namespacesOn = true; 64 private boolean inlineLocalMode = false; 65 66 69 VelocimacroManager(RuntimeServices rs) 70 { 71 this.rsvc = rs; 72 73 76 77 addNamespace( GLOBAL_NAMESPACE ); 78 } 79 80 84 public boolean addVM(String vmName, String macroBody, String argArray[], 85 String namespace) 86 { 87 MacroEntry me = new MacroEntry(this, vmName, macroBody, argArray, 88 namespace); 89 90 me.setFromLibrary(registerFromLib); 91 92 98 99 boolean isLib = true; 100 101 if (registerFromLib) 102 { 103 libraryMap.put(namespace, namespace); 104 } 105 else 106 { 107 114 115 isLib = libraryMap.containsKey(namespace); 116 } 117 118 if ( !isLib && usingNamespaces(namespace) ) 119 { 120 124 125 Hashtable local = getNamespace(namespace, true); 126 local.put((String ) vmName, me); 127 128 return true; 129 } 130 else 131 { 132 136 137 MacroEntry exist = (MacroEntry) getNamespace(GLOBAL_NAMESPACE).get(vmName); 138 139 if (exist != null) 140 { 141 me.setFromLibrary(exist.getFromLibrary()); 142 } 143 144 147 148 getNamespace(GLOBAL_NAMESPACE).put(vmName, me); 149 150 return true; 151 } 152 } 153 154 158 public VelocimacroProxy get(String vmName, String namespace) 159 { 160 161 if (usingNamespaces(namespace)) 162 { 163 Hashtable local = getNamespace(namespace, false); 164 165 168 169 if (local != null) 170 { 171 MacroEntry me = (MacroEntry) local.get(vmName); 172 173 if (me != null) 174 { 175 return me.createVelocimacro(namespace); 176 } 177 } 178 } 179 180 184 185 MacroEntry me = (MacroEntry) getNamespace(GLOBAL_NAMESPACE).get( vmName ); 186 187 if (me != null) 188 { 189 return me.createVelocimacro(namespace); 190 } 191 192 return null; 193 } 194 195 203 public boolean dumpNamespace(String namespace) 204 { 205 synchronized(this) 206 { 207 if (usingNamespaces(namespace)) 208 { 209 Hashtable h = (Hashtable ) namespaceHash.remove(namespace); 210 211 if (h == null) 212 return false; 213 214 h.clear(); 215 216 return true; 217 } 218 219 return false; 220 } 221 } 222 223 228 public void setNamespaceUsage(boolean b) 229 { 230 namespacesOn = b; 231 } 232 233 public void setRegisterFromLib(boolean b) 234 { 235 registerFromLib = b; 236 } 237 238 public void setTemplateLocalInlineVM(boolean b) 239 { 240 inlineLocalMode = b; 241 } 242 243 250 private Hashtable getNamespace(String namespace) 251 { 252 return getNamespace(namespace, false); 253 } 254 255 263 private Hashtable getNamespace(String namespace, boolean addIfNew) 264 { 265 Hashtable h = (Hashtable ) namespaceHash.get(namespace); 266 267 if (h == null && addIfNew) 268 { 269 h = addNamespace(namespace); 270 } 271 272 return h; 273 } 274 275 281 private Hashtable addNamespace(String namespace) 282 { 283 Hashtable h = new Hashtable (); 284 Object oh; 285 286 if ((oh = namespaceHash.put(namespace, h)) != null) 287 { 288 295 namespaceHash.put(namespace, oh); 296 300 return null; 301 } 302 303 return h; 304 } 305 306 312 private boolean usingNamespaces(String namespace) 313 { 314 317 318 if (!namespacesOn) 319 { 320 return false; 321 } 322 323 326 327 if (inlineLocalMode) 328 { 329 return true; 330 } 331 332 return false; 333 } 334 335 public String getLibraryName(String vmName, String namespace) 336 { 337 if (usingNamespaces(namespace)) 338 { 339 Hashtable local = getNamespace(namespace, false); 340 341 346 347 if ( local != null) 348 { 349 MacroEntry me = (MacroEntry) local.get(vmName); 350 351 if (me != null) 352 { 353 return null; 354 } 355 } 356 } 357 358 362 363 MacroEntry me = (MacroEntry) getNamespace(GLOBAL_NAMESPACE).get(vmName); 364 365 if (me != null) 366 { 367 return me.getSourceTemplate(); 368 } 369 370 return null; 371 } 372 373 374 377 protected class MacroEntry 378 { 379 String macroname; 380 String [] argarray; 381 String macrobody; 382 String sourcetemplate; 383 SimpleNode nodeTree = null; 384 VelocimacroManager manager = null; 385 boolean fromLibrary = false; 386 387 MacroEntry(VelocimacroManager vmm, String vmName, String macroBody, 388 String argArray[], String sourceTemplate) 389 { 390 this.macroname = vmName; 391 this.argarray = argArray; 392 this.macrobody = macroBody; 393 this.sourcetemplate = sourceTemplate; 394 this.manager = vmm; 395 } 396 397 public void setFromLibrary(boolean b) 398 { 399 fromLibrary = b; 400 } 401 402 public boolean getFromLibrary() 403 { 404 return fromLibrary; 405 } 406 407 public SimpleNode getNodeTree() 408 { 409 return nodeTree; 410 } 411 412 public String getSourceTemplate() 413 { 414 return sourcetemplate; 415 } 416 417 VelocimacroProxy createVelocimacro(String namespace) 418 { 419 VelocimacroProxy vp = new VelocimacroProxy(); 420 vp.setName(this.macroname); 421 vp.setArgArray(this.argarray); 422 vp.setMacrobody(this.macrobody); 423 vp.setNodeTree(this.nodeTree); 424 vp.setNamespace(namespace); 425 return vp; 426 } 427 428 void setup( InternalContextAdapter ica) 429 { 430 433 434 if( nodeTree == null) 435 parseTree(ica); 436 } 437 438 void parseTree(InternalContextAdapter ica) 439 { 440 try 441 { 442 BufferedReader br = new BufferedReader (new StringReader (macrobody)); 443 444 nodeTree = rsvc.parse(br, "VM:" + macroname, true); 445 nodeTree.init(ica,null); 446 } 447 catch ( Exception e ) 448 { 449 rsvc.error("VelocimacroManager.parseTree() : exception " + 450 macroname + " : " + StringUtils.stackTrace(e)); 451 } 452 } 453 } 454 } 455 | Popular Tags |