KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.IOException JavaDoc;
13
14 import javax.servlet.jsp.JspTagException JavaDoc;
15 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
16
17 import org.mmbase.bridge.*;
18 import org.mmbase.bridge.util.Queries;
19 import org.mmbase.bridge.jsp.taglib.util.Attribute;
20 import org.mmbase.bridge.jsp.taglib.util.Notfound;
21
22 import org.mmbase.util.logging.Logger;
23 import org.mmbase.util.logging.Logging;
24
25 /**
26  * NodeTag provides the fields of a node
27  *
28  * @author Rob Vermeulen
29  * @author Michiel Meeuwissen
30  * @version $Id: NodeTag.java,v 1.64 2006/07/17 15:38:47 johannes Exp $
31  */

32
33 public class NodeTag extends AbstractNodeProviderTag implements BodyTag JavaDoc {
34
35     private static final Logger log = Logging.getLoggerInstance(NodeTag.class);
36
37     private Attribute number = Attribute.NULL;
38     private Attribute element = Attribute.NULL;
39
40     private Attribute notfound = Attribute.NULL;
41
42     /**
43      * Release all allocated resources.
44      */

45     public void doFinally() {
46         log.debug("releasing");
47         super.doFinally();
48         number = Attribute.NULL;
49         element = Attribute.NULL;
50         notfound = Attribute.NULL;
51     }
52
53
54     public void setNumber(String JavaDoc number) throws JspTagException JavaDoc {
55         if (log.isDebugEnabled()) {
56             log.debug("setting number to " + number);
57         }
58         this.number = getAttribute(number);
59     }
60
61
62     public void setNotfound(String JavaDoc i) throws JspTagException JavaDoc {
63         notfound = getAttribute(i);
64     }
65     /**
66      * This function cannot be added because of Orion.
67      * It will call this function even if you use an attribute without <%= %>, stupidly.
68
69      public void setNumber(int number) throws JspTagException {
70      this.number = new Integer(number).toString();
71      }
72
73     */

74
75
76     /**
77      * The element attribute is used to access elements of
78      * clusternodes.
79      */

80     public void setElement(String JavaDoc e) throws JspTagException JavaDoc {
81         element = getAttribute(e);
82     }
83
84
85     public int doStartTag() throws JspTagException JavaDoc{
86         Node node = null;
87         if (referid != Attribute.NULL) {
88             String JavaDoc referString = (String JavaDoc) referid.getValue(this);
89             // try to find if already in context.
90
if (log.isDebugEnabled()) {
91                 log.debug("Looking up Node with " + referString + " in context");
92             }
93             switch(Notfound.get(notfound, this)) {
94                 case Notfound.MESSAGE:
95                     node = getNodeOrNull(referString);
96                     if (node == null) {
97                         try {
98                             getPageContext().getOut().write("Could not find node element '" + element.getString(this) + "'");
99                         } catch (java.io.IOException JavaDoc ioe) {
100                             log.warn(ioe);
101                         }
102                         return SKIP_BODY;
103                     }
104                     break;
105                 case Notfound.SKIP: {
106                     node = getNodeOrNull(referString);
107                     if (node == null) return SKIP_BODY;
108                     break;
109                 }
110                 case Notfound.PROVIDENULL: {
111                     node = getNodeOrNull(referString);
112                     break;
113                 }
114                 default: node = getNode(referString);
115             }
116             if (node != null) {
117                 if (node.getCloud().hasNodeManager(node.getNodeManager().getName())) { // rather clumsy way to check virtuality
118
nodeHelper.setGeneratingQuery(Queries.createNodeQuery(node));
119                 }
120             }
121
122             if(referString.equals(getId())) {
123                 getContextProvider().getContextContainer().unRegister(referString);
124                 // register it again, but as node
125
// (often referid would have been a string).
126
}
127         }
128
129         if (node == null) { // found no node by referid
130
String JavaDoc n = number.getString(this);
131             if (log.isDebugEnabled()) {
132                 log.debug("node is null, number attribute: '" + n + "'");
133             }
134             if (number != Attribute.NULL) { // if (! n.equals("")) { // if empty string should mean 'not present'. Not sure what is most conventient
135
// explicity indicated which node (by number or alias)
136
Cloud c = getCloudVar();
137                 if (! c.hasNode(n) || ! c.mayRead(n)) {
138                     switch(Notfound.get(notfound, this)) {
139                     case Notfound.MESSAGE:
140                         try {
141                             getPageContext().getOut().write("Node '" + n + "' does not exist or may not be read");
142                         } catch (java.io.IOException JavaDoc ioe) {
143                             log.warn(ioe);
144                         }
145                     case Notfound.SKIP:
146                         return SKIP_BODY;
147                     case Notfound.PROVIDENULL:
148                         node = null;
149                         break;
150                     default:
151                         node = c.getNode(n); // throws Exception
152
}
153                 } else {
154                     node = c.getNode(n); // does not throw Exception
155
}
156                 if (node != null) {
157                     if (node.getCloud().hasNodeManager(node.getNodeManager().getName())) { // rather clumsy way to check virtuality
158
nodeHelper.setGeneratingQuery(Queries.createNodeQuery(node));
159                     }
160                 }
161             } else {
162                 // get the node from a parent element.
163
NodeProvider nodeProvider = findNodeProvider(false);
164                 if (nodeProvider == null) {
165                     node = (Node) pageContext.findAttribute(NodeProviderHelper._NODE);
166                     if (node == null) {
167                         throw new JspTagException JavaDoc("Could not find parent of type " + NodeProvider.class + ", and no 'number' or 'referid' attribute specified.");
168                     }
169                 } else {
170                     node = nodeProvider.getNodeVar();
171                 }
172
173
174                 if (element != Attribute.NULL) {
175                     node = node.getNodeValue(element.getString(this));
176                     if (node == null) {
177                         switch(Notfound.get(notfound, this)) {
178                         case Notfound.MESSAGE:
179                             try {
180                                 getPageContext().getOut().write("Could not find node element '" + element.getString(this) + "'");
181                             } catch (java.io.IOException JavaDoc ioe) {
182                                 log.warn(ioe);
183                             }
184                         case Notfound.SKIP:
185                             return SKIP_BODY;
186                         case Notfound.PROVIDENULL:
187                             node = null;
188                             break;
189                         default:
190                             throw new JspTagException JavaDoc("Could not find node element '" + element.getString(this) + "'");
191                         }
192                     }
193                     if (nodeProvider.getNodeVar() != null) {
194                         if (nodeProvider.getNodeVar().getCloud().hasNodeManager(nodeProvider.getNodeVar().getNodeManager().getName())) {
195                             nodeHelper.setGeneratingQuery(nodeProvider.getGeneratingQuery());
196                         }
197                     }
198                 } else {
199                     if (node.getCloud().hasNodeManager(node.getNodeManager().getName())) { // rather clumsy way to check virtuality
200
nodeHelper.setGeneratingQuery(Queries.createNodeQuery(node));
201                     }
202                 }
203             }
204         }
205
206         setNodeVar(node);
207
208         // if direct parent is a Formatter Tag, then communicate
209
FormatterTag f = (FormatterTag) findParentTag(FormatterTag.class, null, false);
210         if (f!= null && f.wantXML() && node != null) {
211             f.getGenerator().add(node);
212             f.setCloud(node.getCloud());
213         }
214
215         fillVars();
216         //log.debug("found node " + node.getValue("gui()"));
217
return EVAL_BODY_BUFFERED;
218     }
219
220
221     /**
222      * this method writes the content of the body back to the jsp page
223      **/

224     public int doAfterBody() throws JspTagException JavaDoc { // write the body if there was one
225
if (bodyContent != null) {
226             try {
227                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
228             } catch (IOException JavaDoc ioe){
229                 throw new TaglibException(ioe);
230             }
231         }
232         return SKIP_BODY;
233     }
234
235
236     public int doEndTag() throws JspTagException JavaDoc {
237         super.doAfterBody(); // if modified
238
return super.doEndTag();
239     }
240
241 }
242
Popular Tags