KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sourceforge.cruisecontrol.BuildInfo;
40 import net.sourceforge.cruisecontrol.util.CCTagException;
41 import net.sourceforge.cruisecontrol.util.DateHelper;
42
43 import javax.servlet.jsp.JspException JavaDoc;
44 import javax.servlet.jsp.JspTagException JavaDoc;
45 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
46 import java.io.File JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.text.DateFormat JavaDoc;
49 import java.text.SimpleDateFormat JavaDoc;
50 import java.util.Arrays JavaDoc;
51
52 public class NavigationTag extends CruiseControlBodyTagSupport {
53     public static final String JavaDoc LINK_TEXT_ATTR = "linktext";
54     public static final String JavaDoc URL_ATTR = "url";
55     public static final String JavaDoc LOG_FILE_ATTR = "logfile";
56     public static final String JavaDoc BUILD_INFO_ATTR = "buildinfo";
57
58     private BuildInfo[] buildInfo; // the log files in the log directory.
59
private int count; // How many times around the loop have we gone.
60

61     private int startingBuildNumber = 0;
62     private int finalBuildNumber = Integer.MAX_VALUE;
63     private int endPoint;
64     private DateFormat JavaDoc dateFormat = null;
65
66     protected String JavaDoc getLinkText(BuildInfo info) {
67         String JavaDoc label = "";
68         if (info.getLabel() != null) {
69             label = " (" + info.getLabel() + ")";
70         }
71
72         return getDateFormat().format(info.getBuildDate()) + label;
73     }
74
75     public int doStartTag() throws JspException JavaDoc {
76         BuildInfo [] logFileNames = findLogFiles();
77         //sort links...
78
Arrays.sort(logFileNames, new ReversedComparator());
79         buildInfo = logFileNames;
80         count = Math.max(0, startingBuildNumber);
81         endPoint = Math.min(finalBuildNumber, buildInfo.length - 1) + 1;
82         if (count < endPoint) {
83             return EVAL_BODY_TAG;
84         } else {
85             return SKIP_BODY;
86         }
87     }
88
89     private BuildInfo[] findLogFiles() throws JspException JavaDoc {
90         File JavaDoc logDir = findLogDir();
91         return BuildInfo.loadFromDir(logDir).asArray();
92     }
93
94     public void doInitBody() throws JspException JavaDoc {
95         setupLinkVariables();
96     }
97
98     void setupLinkVariables() throws JspTagException JavaDoc {
99         final BuildInfo info = buildInfo[count];
100         String JavaDoc logName = info.getLogName();
101         getPageContext().setAttribute(URL_ATTR, createUrl(LOG_PARAMETER, logName));
102         getPageContext().setAttribute(LINK_TEXT_ATTR, getLinkText(info));
103         getPageContext().setAttribute(LOG_FILE_ATTR, logName);
104         getPageContext().setAttribute(BUILD_INFO_ATTR, info);
105         count++;
106     }
107
108     public int doAfterBody() throws JspException JavaDoc {
109         if (count < endPoint) {
110             setupLinkVariables();
111             return EVAL_BODY_TAG;
112         } else {
113             try {
114                 BodyContent JavaDoc out = getBodyContent();
115                 out.writeOut(out.getEnclosingWriter());
116             } catch (IOException JavaDoc e) {
117                 err(e);
118                 throw new CCTagException("IO Error: " + e.getMessage(), e);
119             }
120             return SKIP_BODY;
121         }
122     }
123
124     public int getStartingBuildNumber() {
125         return startingBuildNumber;
126     }
127
128     public void setStartingBuildNumber(int startingBuildNumber) {
129         this.startingBuildNumber = startingBuildNumber;
130     }
131
132     public int getFinalBuildNumber() {
133         return finalBuildNumber;
134     }
135
136     public void setFinalBuildNumber(int finalBuildNumber) {
137         this.finalBuildNumber = finalBuildNumber;
138     }
139
140     /**
141      * Set the DateFormat to use.
142      * The default is based on the client's locale with a 24 hour time. For a
143      * client with a US locale that would be MM/dd/yyyy HH:mm:ss.
144      *
145      * @param dateFormatString The date format to use. Any format appropriate for the
146      * {@link SimpleDateFormat} is okay to use.
147      * @see SimpleDateFormat
148      */

149     public void setDateFormat(String JavaDoc dateFormatString) {
150         dateFormat = new SimpleDateFormat JavaDoc(dateFormatString);
151     }
152
153     private DateFormat JavaDoc getDateFormat() {
154         if (dateFormat == null) {
155             dateFormat = DateHelper.createDateFormat(getLocale());
156         }
157         return dateFormat;
158     }
159
160 }
161
Popular Tags