1 37 38 package net.sourceforge.cruisecontrol.labelincrementers; 39 40 import net.sourceforge.cruisecontrol.LabelIncrementer; 41 import org.apache.log4j.Logger; 42 import org.jdom.Element; 43 44 53 public class DefaultLabelIncrementer implements LabelIncrementer { 54 55 private static final Logger LOG = 56 Logger.getLogger(DefaultLabelIncrementer.class); 57 58 private boolean preIncrement = false; 59 60 private String separator = "."; 61 62 private String defaultPrefix = "build"; 63 private int defaultSuffix = 1; 64 65 private String defaultLabel = null; 66 67 76 public String incrementLabel(String oldLabel, Element buildLog) { 77 String prefix = 78 oldLabel.substring(0, oldLabel.lastIndexOf(separator) + 1); 79 String suffix = 80 oldLabel.substring( 81 oldLabel.lastIndexOf(separator) + 1, 82 oldLabel.length()); 83 int i = Integer.parseInt(suffix); 84 String newLabel = prefix + ++i; 85 LOG.debug("Incrementing label: " + oldLabel + " -> " + newLabel); 86 return newLabel; 87 } 88 89 public boolean isPreBuildIncrementer() { 90 return preIncrement; 91 } 92 93 96 public void setPreBuildIncrementer(boolean preInc) { 97 preIncrement = preInc; 98 } 99 100 105 public boolean isValidLabel(String label) { 106 107 if (label.indexOf(separator) < 0) { 108 return false; 109 } 110 111 try { 112 String suffix = 113 label.substring( 114 label.lastIndexOf(separator) + 1, 115 label.length()); 116 Integer.parseInt(suffix); 117 return true; 118 } catch (NumberFormatException e) { 119 return false; 120 } 121 } 122 123 public void setSeparator(String newSeparator) { 124 separator = newSeparator; 125 } 126 127 133 public String getDefaultLabel() { 134 if (defaultLabel != null) { 135 final int separatorIndex = defaultLabel.lastIndexOf(separator); 136 if (separatorIndex == -1) { 137 throw new IllegalStateException ("separator \"" + separator 138 + "\" not found in default Label " + defaultLabel); 139 } 140 defaultPrefix = defaultLabel.substring(0, separatorIndex); 141 String suffix = defaultLabel.substring(separatorIndex + 1); 142 defaultSuffix = Integer.parseInt(suffix); 143 defaultLabel = null; 144 } 145 return defaultPrefix + separator + defaultSuffix; 146 } 147 148 public void setDefaultLabel(String label) { 149 defaultLabel = label; 150 } 151 } 152 | Popular Tags |