1 16 package org.apache.cocoon.taglib; 17 18 19 import org.apache.avalon.framework.service.ServiceException; 20 import org.apache.avalon.framework.service.ServiceManager; 21 import org.apache.avalon.framework.service.Serviceable; 22 import org.apache.cocoon.environment.ObjectModelHelper; 23 import org.apache.cocoon.environment.Request; 24 import org.apache.cocoon.jxpath.JXPathCocoonContexts; 25 26 import org.apache.commons.jxpath.JXPathContext; 27 28 34 public abstract class VarTagSupport extends TagSupport implements Serviceable { 35 protected String var; 36 protected ServiceManager manager; 37 private JXPathCocoonContexts jxpathContexts; 38 private Request request; 39 40 43 public final String getVar() { 44 return this.var; 45 } 46 47 50 public final void setVar(String var) { 51 this.var = var; 52 } 53 54 protected final Request getRequest() { 55 if (request == null) 56 request = ObjectModelHelper.getRequest(objectModel); 57 return request; 58 } 59 60 protected final Object getVariable(String name) { 61 JXPathContext context = getVariableContext(); 62 if (name.charAt(0) == '$') { 63 return context.getValue(name); 64 } 65 return context.getVariables().getVariable(name); 66 } 68 69 75 protected final void setVariable(String name, Object value) { 76 JXPathContext context = getVariableContext(); 77 if (name.charAt(0) == '$') 78 context.setValue(name, value); 79 else 80 context.getVariables().declareVariable(name, value); 81 } 83 84 protected final void removeVariable(String name) { 85 JXPathContext context = getVariableContext(); 86 if (name.charAt(0) == '$') 87 context.setValue(name, null); 88 else 89 context.getVariables().declareVariable(name, null); 90 } 92 93 private final JXPathContext getVariableContext() { 94 if (jxpathContexts == null) { 95 try { 96 jxpathContexts = (JXPathCocoonContexts) manager.lookup(JXPathCocoonContexts.ROLE); 97 } catch (ServiceException e) { 98 } 100 } 101 return jxpathContexts.getVariableContext(); 102 } 104 105 108 public void service(ServiceManager manager) throws ServiceException { 109 this.manager = manager; 110 } 111 112 public void recycle() { 113 this.var = null; 114 if ( this.manager != null ) { 115 this.manager.release(this.jxpathContexts); 116 } 117 this.jxpathContexts = null; 118 this.request = null; 119 super.recycle(); 120 } 121 } 122 | Popular Tags |