KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > pageflow > TreeFileTag


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.pageflow;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import javax.servlet.jsp.JspTagException JavaDoc;
14 import javax.servlet.jsp.JspException JavaDoc;
15
16 import org.mmbase.util.Casting;
17
18 import org.mmbase.util.logging.Logger;
19 import org.mmbase.util.logging.Logging;
20
21 /**
22  * Like IncludeTag, but an entire tree of files is being probed to find the one
23  * that has the most specified value.
24  *
25  * This is a taglib-implementation of the 'TREEFILE' command.
26  * A full description of this command can be found in the mmbase-taglib.xml file.
27  *
28  * @author Johannes Verelst
29  * @version $Id: TreeFileTag.java,v 1.18.2.1 2006/11/30 11:22:09 michiel Exp $
30  */

31
32 public class TreeFileTag extends UrlTag {
33
34     private static final Logger log = Logging.getLoggerInstance(TreeFileTag.class);
35     protected Attribute objectList = Attribute.NULL;
36     protected TreeHelper th = new TreeHelper();
37
38     protected Attribute notFound = Attribute.NULL;
39
40     public void setNotfound(String JavaDoc n) throws JspTagException JavaDoc {
41         notFound = getAttribute(n);
42     }
43
44
45     public int doStartTag() throws JspTagException JavaDoc {
46         if (page == Attribute.NULL) {
47             throw new JspTagException JavaDoc("Attribute 'page' was not specified");
48         }
49         if (objectList == Attribute.NULL) {
50             throw new JspTagException JavaDoc("Attribute 'objectlist' was not specified");
51         }
52         th.setCloud(getCloudVar());
53         super.doStartTag();
54         helper.setValue(new Comparable JavaDoc() {
55                             final TreeFileTag t = TreeFileTag.this;
56                             public String JavaDoc toString() {
57                                 try {
58                                     String JavaDoc string = t.getUrl();
59                                     // this means that it is written to page by ${_} and that consequently there _must_ be a body.
60
// this is needed when body is not buffered.
61
haveBody();
62                                     return string;
63                                 } catch (Throwable JavaDoc e){
64                                     return e.toString();
65                                 }
66                             }
67                             public int compareTo(Object JavaDoc o) {
68                                 return toString().compareTo(Casting.toString(o));
69                             }
70                         });
71         return EVAL_BODY; // lets try _not_ buffering the body.
72
// this may give unexpected results if ${_} is not used (or another tag calling 'haveBody')
73
}
74
75     protected String JavaDoc getPage() throws JspTagException JavaDoc {
76         String JavaDoc orgPage = super.getPage();
77         String JavaDoc treePage = th.findTreeFile(orgPage, objectList.getString(this), pageContext.getSession());
78         if (log.isDebugEnabled()) {
79             log.debug("Retrieving page '" + treePage + "'");
80         }
81
82         if (treePage == null || "".equals(treePage)) {
83             throw new JspTagException JavaDoc("Could not find page " + orgPage);
84         }
85
86         return treePage;
87     }
88
89
90     public int doAfterBody() throws JspException JavaDoc {
91         return helper.doAfterBody();
92     }
93
94     public int doEndTag() throws JspTagException JavaDoc {
95         // Let UrlTag do the rest
96
int retval = super.doEndTag();
97         return retval;
98     }
99
100     public void doFinally() {
101         th.doFinally();
102         super.doFinally();
103     }
104
105     /**
106      * @param includePage the page to include, can contain arguments and path (path/file.jsp?argument=value)
107       */

108
109     public void setObjectlist(String JavaDoc includePage) throws JspTagException JavaDoc {
110         objectList = getAttribute(includePage);
111     }
112
113     // override to cancel
114
protected boolean doMakeRelative() {
115         log.debug("doMakeRelative() overridden!");
116         return false;
117     }
118
119     protected String JavaDoc getUrl(boolean writeamp, boolean encode) throws JspTagException JavaDoc {
120         String JavaDoc url = "";
121         try {
122             url = super.getUrl(writeamp, encode);
123         } catch (JspTagException JavaDoc e) {
124             if (!notFound.getString(this).equals("skip")) {
125                 throw(e);
126             }
127         }
128         return url;
129     }
130
131 }
132
Popular Tags