KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > NodeProviderHelper


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11
12
13 import org.mmbase.bridge.jsp.taglib.util.Attribute;
14 import javax.servlet.jsp.*;
15 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
16 import java.util.Stack JavaDoc;
17
18 import org.mmbase.bridge.*;
19 import org.mmbase.bridge.util.*;
20
21 import org.mmbase.util.logging.Logger;
22 import org.mmbase.util.logging.Logging;
23
24
25 /**
26  *
27  * @author Michiel Meeuwissen
28  * @version $Id: NodeProviderHelper.java,v 1.23 2006/07/25 14:37:48 michiel Exp $
29  * @since MMBase-1.7
30  */

31
32 public class NodeProviderHelper implements NodeProvider {
33
34     private static final Logger log = Logging.getLoggerInstance(NodeProviderHelper.class);
35
36     public static final String JavaDoc STACK_ATTRIBUTE = "org.mmbase.bridge.jsp.taglib._nodeStack";
37     public static final String JavaDoc _NODE = "_node";
38
39     private NodeChanger node;
40     private Query query = null;
41     private String JavaDoc jspvar = null;
42     private ContextReferrerTag thisTag;
43     private Attribute commit = Attribute.NULL;
44
45     /**
46      * 'underscore' stack, containing the values for '_node'.
47      * @since MMBase_1.8
48      */

49     private Stack JavaDoc _Stack;
50     // whether this tag pushed something on the stack already.
51
private int pushed = 0;
52
53     public NodeProviderHelper(ContextReferrerTag thisTag) {
54         this.thisTag = thisTag;
55     }
56
57     // general attributes for NodeProviders
58
// id from TagSupport
59

60     public void setJspvar(String JavaDoc jv) {
61         jspvar = jv;
62         if ("".equals(jspvar)) jspvar = null;
63     }
64
65     /**
66      * @since MMBase-1.8
67      */

68     public void setCommitonclose(String JavaDoc c) throws JspTagException {
69         commit = thisTag.getAttribute(c);
70     }
71
72
73     /**
74     * For use by children, they can find the current 'node' belonging
75     * to this tag.
76     */

77
78     public Node getNodeVar() {
79         return node;
80     }
81
82     /**
83      * Children can also directly access the node member, but the
84      * prefered method is to treat this variable as much as possible
85      * as private, and use this.
86      */

87
88     public void setNodeVar(Node node) {
89         if (node == null) {
90             this.node = null;
91         } else if (node instanceof NodeChanger) {
92             this.node = (NodeChanger) node;
93         } else {
94             this.node = new NodeChanger(node);
95         }
96     }
97
98
99     public void setGeneratingQuery(Query q) {
100         query = q;
101     }
102
103     public Query getGeneratingQuery() throws JspTagException {
104         if (query == null) {
105             query = Queries.createNodeQuery(getNodeVar());
106         }
107         return query;
108     }
109
110     public String JavaDoc getId() {
111         try {
112             return (String JavaDoc) thisTag.id.getValue(thisTag);
113         } catch (JspTagException j) {
114             throw new RuntimeException JavaDoc(j);
115         }
116     }
117
118
119     boolean checked = false; // need to check jspvar/pagecontext-var conflict only first time.
120
/**
121      * Fill the jsp and context vars
122      *
123      */

124
125     public void fillVars() throws JspTagException {
126         org.mmbase.bridge.jsp.taglib.util.ContextContainer cc = thisTag.getContextProvider().getContextContainer();
127         if (thisTag.id != Attribute.NULL) {
128             cc.registerNode(getId(), node);
129         }
130         if (jspvar != null && node != null) {
131             cc.setJspVar(thisTag.getPageContext(), jspvar, WriterHelper.TYPE_NODE, node);
132         }
133         PageContext pageContext = thisTag.getPageContext();
134
135         _Stack = (Stack JavaDoc) pageContext.getAttribute(STACK_ATTRIBUTE, PageContext.REQUEST_SCOPE);
136         if (_Stack == null) {
137             _Stack = new Stack JavaDoc();
138             pageContext.setAttribute(STACK_ATTRIBUTE, _Stack, PageContext.REQUEST_SCOPE);
139         }
140         _Stack.push(node);
141         pushed++;
142         pageContext.setAttribute(_NODE, org.mmbase.util.Casting.wrap(node, (org.mmbase.util.transformers.CharTransformer) pageContext.findAttribute(ContentTag.ESCAPER_KEY)), PageContext.REQUEST_SCOPE);
143     }
144
145     private String JavaDoc getSimpleReturnValueName(String JavaDoc fieldName){
146         return getSimpleReturnValueName(jspvar, fieldName);
147     }
148     /**
149      * Generates the variable-name for a field.
150      *
151      * @param prefix A prefix to use. Can be null.
152      * @param fieldName The name of the field.
153      */

154     static String JavaDoc getSimpleReturnValueName(String JavaDoc prefix, String JavaDoc fieldName){
155         String JavaDoc field = fieldName.replace('.', '_');
156         if (prefix != null && ! "".equals(prefix)) {
157             field = prefix + "_" + field;
158         }
159         return field;
160     }
161
162     /**
163      * @since MMBase-1.8.2
164      */

165     private void pop_Stack() {
166         if (_Stack != null) {
167             Object JavaDoc pop = _Stack.pop();
168             pushed--;
169             PageContext pageContext = thisTag.getPageContext();
170             if (_Stack.empty()) {
171                 pageContext.removeAttribute(_NODE, PageContext.REQUEST_SCOPE);
172             } else {
173                 pageContext.setAttribute(_NODE, org.mmbase.util.Casting.wrap(_Stack.peek(), (org.mmbase.util.transformers.CharTransformer) pageContext.findAttribute(ContentTag.ESCAPER_KEY)), PageContext.REQUEST_SCOPE);
174             }
175             _Stack = null;
176         }
177     }
178     /**
179     * Does everything needed on the afterbody tag of every
180     * NodeProvider. Normally this function would be overrided with
181     * one that has to call super.doAfterBody(). But not all servlet
182     * engines to call this function if there is no body. So, in that
183     * case it should be called from doEndTag, if the tag can do
184     * something without a body.
185     **/

186     public int doAfterBody() throws JspTagException {
187         if (node != null) {
188             if (node.isNew() || node.isChangedByThis()) {
189                 // node can need committing
190
if (commit.getBoolean(thisTag, true)) {
191                     if (log.isDebugEnabled()) {
192                         log.debug("Committing node " + node.getNumber() + " for user " + node.getCloud().getUser().getIdentifier() + " changed fields: " + node.getChanged() + " " + node.isNew() + " " + node.isChanged() + " because ", new Exception JavaDoc());
193                     }
194                     node.commit();
195                 } else if (log.isDebugEnabled()) {
196                     log.debug("Not committing " + node.getNumber() + " for user " + node.getCloud().getUser().getIdentifier() + " changed fields: " + node.getChanged() + " " + node.isNew() + " " + node.isChanged());
197                 }
198             } else {
199                 if (log.isDebugEnabled()) {
200                     log.debug("Node " + node.getNumber() + " was not changed " + thisTag.getClass() + " ");
201                 }
202             }
203         }
204         pop_Stack();
205         return BodyTagSupport.SKIP_BODY;
206     }
207
208     public int doEndTag() throws JspTagException {
209         // to enable gc:
210
if (_Stack != null) {
211             while (pushed > 0) {
212                 pop_Stack();
213             }
214         }
215         pushed = 0;
216         checked = false;
217         return BodyTagSupport.EVAL_PAGE;
218     }
219
220     public void doFinally () {
221         node = null;
222         _Stack = null;
223         query = null;
224     }
225 }
226
Popular Tags