KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > taglib > ProjectNavigationTag


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.taglib;
38
39 import java.io.IOException JavaDoc;
40 import java.util.Arrays JavaDoc;
41
42 import javax.servlet.jsp.JspException JavaDoc;
43 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
44
45 /**
46  *
47  */

48 public class ProjectNavigationTag extends CruiseControlBodyTagSupport {
49     static final String JavaDoc STATUS_PAGE_TEXT = "-- STATUS PAGE --";
50     static final String JavaDoc SELECTED_ATTR_VALUE = "selected=\"selected\"";
51     public static final String JavaDoc SELECTED_ATTR = "selected";
52     public static final String JavaDoc LINK_TEXT_ATTR = "linktext";
53     public static final String JavaDoc URL_ATTR = "projecturl";
54
55     private int count; // the current item counter of the project list
56
private int endPoint; // How many times around the loop have we gone
57
private String JavaDoc[] projects; // array of project names
58

59     public int doStartTag() throws JspException JavaDoc {
60         projects = findProjects();
61         Arrays.sort(projects);
62         count = -1;
63         endPoint = projects.length;
64         if (count < endPoint) {
65             return EVAL_BODY_TAG;
66         } else {
67             return SKIP_BODY;
68         }
69     }
70
71
72     public void doInitBody() throws JspException JavaDoc {
73        setupLinkVariables();
74     }
75
76     void setupLinkVariables() {
77         if (count == -1) {
78             getPageContext().setAttribute(LINK_TEXT_ATTR, STATUS_PAGE_TEXT);
79             getPageContext().setAttribute(URL_ATTR, getRequest().getContextPath() + "/");
80             getPageContext().setAttribute(SELECTED_ATTR, "");
81         } else {
82             final String JavaDoc projectName = projects[count];
83             getPageContext().setAttribute(LINK_TEXT_ATTR, projectName);
84             getPageContext().setAttribute(URL_ATTR, getURL(projectName));
85             getPageContext().setAttribute(SELECTED_ATTR, getSelected(projectName));
86            }
87         count++;
88     }
89
90     private String JavaDoc getSelected(final String JavaDoc projectName) {
91         if (getProject().length() > 0 && getProject().substring(1).compareTo(projectName) == 0) {
92             return SELECTED_ATTR_VALUE;
93         }
94         return "";
95     }
96     
97     private String JavaDoc getURL(String JavaDoc projectName) {
98        return getServletPath() + "/" + projectName;
99     }
100
101
102     public int doAfterBody() throws JspException JavaDoc {
103         if (count < endPoint) {
104             setupLinkVariables();
105             return EVAL_BODY_TAG;
106         } else {
107             try {
108                 BodyContent JavaDoc out = getBodyContent();
109                 out.writeOut(out.getEnclosingWriter());
110             } catch (IOException JavaDoc e) {
111                 err(e);
112             }
113             return SKIP_BODY;
114         }
115     }
116 }
117
Popular Tags