KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > taglib > VarTagSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
29  * Add support for setting and getting variables
30  *
31  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
32  * @version CVS $Id: VarTagSupport.java 158423 2005-03-21 09:15:22Z cziegeler $
33  */

34 public abstract class VarTagSupport extends TagSupport implements Serviceable {
35     protected String JavaDoc var;
36     protected ServiceManager manager;
37     private JXPathCocoonContexts jxpathContexts;
38     private Request request;
39
40     /**
41      * Get the attribute to store the result in.
42      */

43     public final String JavaDoc getVar() {
44         return this.var;
45     }
46
47     /**
48      * Set the attribute to store the result in.
49      */

50     public final void setVar(String JavaDoc 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 JavaDoc getVariable(String JavaDoc name) {
61         JXPathContext context = getVariableContext();
62         if (name.charAt(0) == '$') {
63             return context.getValue(name);
64         }
65         return context.getVariables().getVariable(name);
66         //getRequest().getAttribute(name);
67
}
68
69     /**
70      * Register the name and object specified.
71      *
72      * @param name the name of the attribute to set
73      * @param value the object to associate with the name
74      */

75     protected final void setVariable(String JavaDoc name, Object JavaDoc value) {
76         JXPathContext context = getVariableContext();
77         if (name.charAt(0) == '$')
78             context.setValue(name, value);
79         else
80             context.getVariables().declareVariable(name, value);
81         //getRequest().setAttribute(name, value);
82
}
83
84     protected final void removeVariable(String JavaDoc name) {
85         JXPathContext context = getVariableContext();
86         if (name.charAt(0) == '$')
87             context.setValue(name, null);
88         else
89             context.getVariables().declareVariable(name, null);
90         //getRequest().removeAttribute(name);
91
}
92
93     private final JXPathContext getVariableContext() {
94         if (jxpathContexts == null) {
95             try {
96                 jxpathContexts = (JXPathCocoonContexts) manager.lookup(JXPathCocoonContexts.ROLE);
97             } catch (ServiceException e) {
98                 //XXX
99
}
100         }
101         return jxpathContexts.getVariableContext();
102         //return JXPathCocoonContexts.getVariableContext(objectModel);
103
}
104
105     /*
106      * @see Serviceable#service(ServiceManager)
107      */

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