1 37 package net.sourceforge.cruisecontrol.labelincrementers; 38 39 import net.sourceforge.cruisecontrol.LabelIncrementer; 40 import org.apache.log4j.Logger; 41 import org.jdom.Element; 42 43 57 public class FormattedLabelIncrementer implements LabelIncrementer { 58 59 private static final Logger LOG = 60 Logger.getLogger(DefaultLabelIncrementer.class); 61 62 private boolean preIncrement = false; 63 private boolean includePrefix = true; 64 private String defaultPrefix = "CC"; 65 private int defaultBuildNum = 1; 66 private String defaultSuffix = "INT"; 67 private String separator = "_"; 68 69 public FormattedLabelIncrementer() { 70 setSeparator(separator); 71 } 72 73 77 public void setSeparator(String newSeparator) { 78 separator = newSeparator; 79 } 80 81 85 public void setPreBuildIncrementer(boolean preInc) { 86 preIncrement = preInc; 87 } 88 89 93 public void setPrefix(boolean prefix) { 94 includePrefix = prefix; 95 } 96 97 101 public String getDefaultLabel() { 102 if (includePrefix) { 103 return defaultPrefix + separator 104 + defaultBuildNum + separator + defaultSuffix; 105 } else { 106 return defaultBuildNum + separator + defaultSuffix; 107 } 108 } 109 110 114 public void setDefaultLabel(String label) { 115 LOG.debug("Setting default label: " + label); 116 if (includePrefix) { 117 int separatorIndex = label.lastIndexOf(separator); 118 defaultSuffix = label.substring(separatorIndex + 1, label.length()).toUpperCase(); 119 defaultPrefix = label.substring(0, separatorIndex); 120 separatorIndex = defaultPrefix.lastIndexOf(separator); 121 defaultBuildNum = Integer.parseInt( 122 defaultPrefix.substring(separatorIndex + 1, 123 defaultPrefix.length())); 124 defaultPrefix = defaultPrefix.substring(0, separatorIndex).toUpperCase(); 125 } else { 126 defaultSuffix = label.substring(label.indexOf(separator) + 1, label.length()).toUpperCase(); 127 defaultBuildNum = Integer.parseInt( 128 label.substring(0, label.indexOf(separator))); 129 } 130 } 131 132 136 public boolean isPreBuildIncrementer() { 137 return preIncrement; 138 } 139 140 150 public String incrementLabel(String oldLabel, Element buildLog) { 151 String newLabel; 152 153 if (includePrefix) { 154 String prefix1 = oldLabel.substring(0, oldLabel.lastIndexOf(separator)); 155 String prefix2 = prefix1.substring(0, prefix1.lastIndexOf(separator)); 156 String suffix = oldLabel.substring( 157 oldLabel.lastIndexOf(separator) + 1, 158 oldLabel.length()); 159 String buildnum = prefix1.substring( 160 prefix1.lastIndexOf(separator) + 1, 161 prefix1.length()); 162 int i = Integer.parseInt(buildnum); 163 newLabel = prefix2.toUpperCase() + separator 164 + ++i + separator + suffix.toUpperCase(); 165 } else { 166 String suffix = oldLabel.substring( 167 oldLabel.lastIndexOf(separator) + 1, 168 oldLabel.length()); 169 String buildnum = oldLabel.substring( 170 0, oldLabel.indexOf(separator)); 171 int i = Integer.parseInt(buildnum); 172 newLabel = ++i + separator + suffix.toUpperCase(); 173 } 174 LOG.debug("Incrementing label: " + oldLabel + " -> " + newLabel); 175 return newLabel; 176 } 177 178 186 public boolean isValidLabel(String label) { 187 188 if (label.indexOf(separator) < 0) { 190 return false; 191 } 192 193 try { 194 String suffix; 195 String buildnum; 196 197 if (includePrefix) { 199 String prefix1 = label.substring(0, label.lastIndexOf(separator)); 200 suffix = label.substring(label.lastIndexOf(separator) + 1, 201 label.length()); 202 buildnum = prefix1.substring(prefix1.lastIndexOf(separator) + 1, 203 prefix1.length()); 204 } else { 205 suffix = label.substring(label.lastIndexOf(separator) + 1, 206 label.length()); 207 buildnum = label.substring(0, label.indexOf(separator)); 208 } 209 210 if (suffix.equals("BLD") || suffix.equals("INT") || suffix.equals("REL")) { 212 Integer.parseInt(buildnum); 213 return true; 214 } else { return false; } 215 } catch (NumberFormatException e) { 216 return false; 217 } catch (StringIndexOutOfBoundsException e) { 218 return false; 219 } 220 } 221 222 } 223 | Popular Tags |