001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.taglib;
018:
019: import org.apache.avalon.framework.service.ServiceException;
020: import org.apache.avalon.framework.service.ServiceManager;
021: import org.apache.avalon.framework.service.Serviceable;
022: import org.apache.cocoon.environment.ObjectModelHelper;
023: import org.apache.cocoon.environment.Request;
024: import org.apache.cocoon.jxpath.JXPathCocoonContexts;
025:
026: import org.apache.commons.jxpath.JXPathContext;
027:
028: /**
029: * Add support for setting and getting variables
030: *
031: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
032: * @version CVS $Id: VarTagSupport.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public abstract class VarTagSupport extends TagSupport implements
035: Serviceable {
036: protected String var;
037: protected ServiceManager manager;
038: private JXPathCocoonContexts jxpathContexts;
039: private Request request;
040:
041: /**
042: * Get the attribute to store the result in.
043: */
044: public final String getVar() {
045: return this .var;
046: }
047:
048: /**
049: * Set the attribute to store the result in.
050: */
051: public final void setVar(String var) {
052: this .var = var;
053: }
054:
055: protected final Request getRequest() {
056: if (request == null)
057: request = ObjectModelHelper.getRequest(objectModel);
058: return request;
059: }
060:
061: protected final Object getVariable(String name) {
062: JXPathContext context = getVariableContext();
063: if (name.charAt(0) == '$') {
064: return context.getValue(name);
065: }
066: return context.getVariables().getVariable(name);
067: //getRequest().getAttribute(name);
068: }
069:
070: /**
071: * Register the name and object specified.
072: *
073: * @param name the name of the attribute to set
074: * @param value the object to associate with the name
075: */
076: protected final void setVariable(String name, Object value) {
077: JXPathContext context = getVariableContext();
078: if (name.charAt(0) == '$')
079: context.setValue(name, value);
080: else
081: context.getVariables().declareVariable(name, value);
082: //getRequest().setAttribute(name, value);
083: }
084:
085: protected final void removeVariable(String name) {
086: JXPathContext context = getVariableContext();
087: if (name.charAt(0) == '$')
088: context.setValue(name, null);
089: else
090: context.getVariables().declareVariable(name, null);
091: //getRequest().removeAttribute(name);
092: }
093:
094: private final JXPathContext getVariableContext() {
095: if (jxpathContexts == null) {
096: try {
097: jxpathContexts = (JXPathCocoonContexts) manager
098: .lookup(JXPathCocoonContexts.ROLE);
099: } catch (ServiceException e) {
100: //XXX
101: }
102: }
103: return jxpathContexts.getVariableContext();
104: //return JXPathCocoonContexts.getVariableContext(objectModel);
105: }
106:
107: /*
108: * @see Serviceable#service(ServiceManager)
109: */
110: public void service(ServiceManager manager) throws ServiceException {
111: this .manager = manager;
112: }
113:
114: public void recycle() {
115: this.var = null;
116: if (this.manager != null) {
117: this.manager.release(this.jxpathContexts);
118: }
119: this.jxpathContexts = null;
120: this.request = null;
121: super.recycle();
122: }
123: }
|