1 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 ; 44 import javax.servlet.jsp.JspTagException ; 45 import javax.servlet.jsp.tagext.BodyContent ; 46 import java.io.File ; 47 import java.io.IOException ; 48 import java.text.DateFormat ; 49 import java.text.SimpleDateFormat ; 50 import java.util.Arrays ; 51 52 public class NavigationTag extends CruiseControlBodyTagSupport { 53 public static final String LINK_TEXT_ATTR = "linktext"; 54 public static final String URL_ATTR = "url"; 55 public static final String LOG_FILE_ATTR = "logfile"; 56 public static final String BUILD_INFO_ATTR = "buildinfo"; 57 58 private BuildInfo[] buildInfo; private int count; 61 private int startingBuildNumber = 0; 62 private int finalBuildNumber = Integer.MAX_VALUE; 63 private int endPoint; 64 private DateFormat dateFormat = null; 65 66 protected String getLinkText(BuildInfo info) { 67 String 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 { 76 BuildInfo [] logFileNames = findLogFiles(); 77 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 { 90 File logDir = findLogDir(); 91 return BuildInfo.loadFromDir(logDir).asArray(); 92 } 93 94 public void doInitBody() throws JspException { 95 setupLinkVariables(); 96 } 97 98 void setupLinkVariables() throws JspTagException { 99 final BuildInfo info = buildInfo[count]; 100 String 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 { 109 if (count < endPoint) { 110 setupLinkVariables(); 111 return EVAL_BODY_TAG; 112 } else { 113 try { 114 BodyContent out = getBodyContent(); 115 out.writeOut(out.getEnclosingWriter()); 116 } catch (IOException 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 149 public void setDateFormat(String dateFormatString) { 150 dateFormat = new SimpleDateFormat (dateFormatString); 151 } 152 153 private DateFormat getDateFormat() { 154 if (dateFormat == null) { 155 dateFormat = DateHelper.createDateFormat(getLocale()); 156 } 157 return dateFormat; 158 } 159 160 } 161 | Popular Tags |