KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > labelincrementers > FormattedLabelIncrementer


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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.labelincrementers;
38
39 import net.sourceforge.cruisecontrol.LabelIncrementer;
40 import org.apache.log4j.Logger;
41 import org.jdom.Element;
42
43 /**
44  * This class provides a label incrementation for creating consistent, formatted upper
45  * case labels. This class expects the label format to be either "x_y_z" or "y_z"
46  * where x is any String, y is an integer and z is one of REL, INT or BLD.
47  *
48  * * Usage:
49  *
50  * <formattedlabelincrementer prefix="false" defaultlabel="1.INT"/%gt;
51  *
52  * @author <a HREF="mailto:kevin.lee@buildmeister.com">Kevin Lee</a>
53  * @author Jeff Brekke (Jeff.Brekke@qg.com)
54  * @author alden almagro (alden@thoughtworks.com)
55  * @author Paul Julius (pdjulius@thoughtworks.com)
56  */

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 JavaDoc defaultPrefix = "CC";
65     private int defaultBuildNum = 1;
66     private String JavaDoc defaultSuffix = "INT";
67     private String JavaDoc separator = "_";
68
69     public FormattedLabelIncrementer() {
70         setSeparator(separator);
71     }
72
73     /**
74      * set the separtor to be use between parts of the build label, default is "_"
75      * @param newSeparator the character string to use as a separator
76      */

77     public void setSeparator(String JavaDoc newSeparator) {
78         separator = newSeparator;
79     }
80     
81     /**
82      * Set the pre/post behavior of the label incrementer
83      * @param preInc whether to increment the build before the build, default is false
84      */

85     public void setPreBuildIncrementer(boolean preInc) {
86         preIncrement = preInc;
87     }
88
89     /**
90      * Set whether a prefix is required or no
91      * @param prefix whether to include a prefix with the label, default is true
92      */

93     public void setPrefix(boolean prefix) {
94         includePrefix = prefix;
95     }
96   
97     /**
98      * Get the default label
99      * @return string containing the defailt label
100      */

101     public String JavaDoc getDefaultLabel() {
102         if (includePrefix) {
103             return defaultPrefix + separator
104                 + defaultBuildNum + separator + defaultSuffix;
105         } else {
106             return defaultBuildNum + separator + defaultSuffix;
107         }
108     }
109
110     /**
111      * Set the default label
112      * @param label string to set the default label to
113      */

114     public void setDefaultLabel(String JavaDoc 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     /**
133      * Checks whether the label should be incremented pre/post build
134      * @return true if the label will be incremented before the build, else false
135      */

136     public boolean isPreBuildIncrementer() {
137         return preIncrement;
138     }
139       
140     /**
141      * Increments the label when a successful build occurs.
142      * Assumes that the label will be in the format of "x_y_z" or "y_z",
143      * where x can be anything, y is an integer and z is one of REL, INT or BLD
144      * The y value will be incremented by one, the rest will remain the same.
145      * The label is converted to uppercase by default.
146      *
147      * @param oldLabel Label from previous successful build.
148      * @return Label to use for most recent successful build.
149      */

150     public String JavaDoc incrementLabel(String JavaDoc oldLabel, Element buildLog) {
151         String JavaDoc newLabel;
152         
153         if (includePrefix) {
154             String JavaDoc prefix1 = oldLabel.substring(0, oldLabel.lastIndexOf(separator));
155             String JavaDoc prefix2 = prefix1.substring(0, prefix1.lastIndexOf(separator));
156             String JavaDoc suffix = oldLabel.substring(
157                 oldLabel.lastIndexOf(separator) + 1,
158                 oldLabel.length());
159             String JavaDoc 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 JavaDoc suffix = oldLabel.substring(
167                     oldLabel.lastIndexOf(separator) + 1,
168                     oldLabel.length());
169             String JavaDoc 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     /**
179      * Verify that the label specified is a valid label. In this case a valid
180      * label contains at least one '_' character, and an integer after the last
181      * but one occurrence of the '_' character, followed by REL, INT or BLD
182      *
183      * @param label the label to check for validity
184      * @return true if label is valid, else false
185      */

186     public boolean isValidLabel(String JavaDoc label) {
187
188         // the label does not include a separator
189
if (label.indexOf(separator) < 0) {
190             return false;
191         }
192
193         try {
194             String JavaDoc suffix;
195             String JavaDoc buildnum;
196             
197             // check for label format
198
if (includePrefix) {
199                 String JavaDoc 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             // check for consistent suffix
211
if (suffix.equals("BLD") || suffix.equals("INT") || suffix.equals("REL")) {
212                 Integer.parseInt(buildnum);
213                 return true;
214             } else { return false; }
215         } catch (NumberFormatException JavaDoc e) {
216             return false;
217         } catch (StringIndexOutOfBoundsException JavaDoc e) {
218             return false;
219         }
220     }
221        
222 }
223
Popular Tags