KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > tree > TreeContainerTag


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.tree;
11
12 import javax.servlet.jsp.JspTagException JavaDoc;
13
14 import org.mmbase.bridge.*;
15 import org.mmbase.storage.search.*;
16 import org.mmbase.bridge.jsp.taglib.containers.*;
17 import org.mmbase.bridge.jsp.taglib.util.*;
18 import org.mmbase.bridge.jsp.taglib.*;
19 import org.mmbase.bridge.util.*;
20 import org.mmbase.util.logging.*;
21
22 /**
23  * Container cognate for TreeTag
24  *
25  * @author Michiel Meeuwissen
26  * @since MMBase-1.7.1
27  * @version $Id: TreeContainerTag.java,v 1.9 2006/07/25 19:56:23 michiel Exp $
28  */

29 public class TreeContainerTag extends RelatedNodesContainerTag implements NodeQueryContainer, ContainerReferrer { // extending from relatednodescontainer only for the attributes
30

31     private static final Logger log = Logging.getLoggerInstance(TreeContainerTag.class);
32
33     protected Attribute maxDepth = Attribute.NULL;
34     protected Attribute container = Attribute.NULL;
35     private String JavaDoc jspVar;
36
37     public void setMaxdepth(String JavaDoc md) throws JspTagException JavaDoc {
38         maxDepth = getAttribute(md);
39     }
40     public void setContainer(String JavaDoc c) throws JspTagException JavaDoc {
41         container = getAttribute(c);
42     }
43
44     public void setJspvar(String JavaDoc jv) {
45         jspVar = jv;
46     }
47
48     protected GrowingTreeList tree;
49
50     public GrowingTreeList getTree() {
51         return tree;
52     }
53
54
55     public Query getQuery() {
56         return getNodeQuery();
57     }
58     public NodeQuery getNodeQuery() {
59         return tree.getTemplate();
60     }
61
62     /**
63      * Retrieves the starting query from environment.
64      * Static because also used by TreeTag itself.
65      */

66     static NodeQuery getStartQuery(ContextReferrerTag thisTag, Attribute containerAttribute, Attribute nodeAttribute) throws JspTagException JavaDoc {
67         NodeQuery query = null;
68         String JavaDoc container = containerAttribute.getString(thisTag);
69         String JavaDoc node = nodeAttribute.getString(thisTag);
70         if ("".equals(container) && "".equals(node)) {
71             log.debug("no node attribute, no container attribute, trying container first");
72             NodeQueryContainer c = (NodeQueryContainer) thisTag.findParentTag(NodeQueryContainer.class, null, false);
73             if (c != null) {
74                 query = c.getNodeQuery();
75             }
76         } else if (! "".equals(container)) {
77             log.debug("container attribute, trying container");
78             NodeQueryContainer c = (NodeQueryContainer) thisTag.findParentTag(NodeQueryContainer.class, container, true);
79             if (c != null) {
80                 query = c.getNodeQuery();
81             }
82         }
83         if (query == null) { // try to work as node-referrer
84
log.debug("working as node-referrer");
85             NodeProvider np = (NodeProvider) thisTag.findParentTag(NodeProvider.class, "".equals(node) ? null : node, ! "".equals(node));
86             if (np == null) {
87                 throw new TaglibException("No NodeQueryContainer nor a NodeProvider found in tree-tag");
88             } else {
89                 query = Queries.createNodeQuery(np.getNodeVar());
90             }
91         }
92         return query;
93
94     }
95
96
97     void addBranch(NodeManager nodeManager, String JavaDoc r, String JavaDoc sd) {
98         tree.grow(nodeManager, r, sd);
99     }
100
101     public int doStartTag() throws JspTagException JavaDoc {
102         // first of all, we need a 'start' query, take it from a surrounding 'nodequery container'
103

104         query = getStartQuery(this, container, parentNodeId);
105
106
107         if (nodeManager != Attribute.NULL) {
108             tree = new GrowingTreeList(query,
109                                        maxDepth.getInt(this, 5),
110                                        query.getCloud().getNodeManager(nodeManager.getString(this)),
111                                        role.getString(this),
112                                        searchDirs.getString(this));
113
114             if (path != Attribute.NULL) throw new JspTagException JavaDoc("Should specify either 'type' or 'path' attributes on treecontainer");
115         } else {
116             tree = new GrowingTreeList(query, maxDepth.getInt(this, 5));
117             if (path != Attribute.NULL) {
118                 Queries.addPath(tree.getTemplate(), (String JavaDoc) path.getValue(this), (String JavaDoc) searchDirs.getValue(this));
119
120                 // I'm not entirely sure why the following is necessary at all:
121
Step step = (Step) tree.getTemplate().getSteps().get(2);
122                 if (query.getSteps().contains(step)) {
123                     query.setNodeStep(step);
124                 }
125             }
126         }
127         if (jspVar != null) {
128             pageContext.setAttribute(jspVar, tree);
129         }
130         return EVAL_BODY;
131     }
132
133     public int doAfterBody() throws JspTagException JavaDoc {
134         if (EVAL_BODY == EVAL_BODY_BUFFERED) {
135             try {
136                 if (bodyContent != null) {
137                     bodyContent.writeOut(bodyContent.getEnclosingWriter());
138                 }
139             } catch (java.io.IOException JavaDoc ioe){
140                 throw new JspTagException JavaDoc(ioe.toString());
141             }
142         }
143         return SKIP_BODY;
144     }
145     public int doEndTag() throws JspTagException JavaDoc {
146         tree = null;
147         return super.doEndTag();
148     }
149
150 }
151
Popular Tags