KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 /**
45  * This class provides a default label incrementation.
46  * This class expects the label format to be "x<sep>y",
47  * where x is any String and y is an integer and <sep> a separator.
48  * The default separator is "." and can be modified using {@link #setSeparator}.
49  *
50  * @author <a HREF="mailto:alden@thoughtworks.com">alden almagro</a>
51  * @author <a HREF="mailto:pj@thoughtworks.com">Paul Julius</a>
52  */

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 JavaDoc separator = ".";
61     
62     private String JavaDoc defaultPrefix = "build";
63     private int defaultSuffix = 1;
64     /* temporary variable, only used at instance setup time */
65     private String JavaDoc defaultLabel = null;
66
67     /**
68      * Increments the label when a successful build occurs.
69      * Assumes that the label will be in
70      * the format of "x.y", where x can be anything, and y is an integer.
71      * The y value will be incremented by one, the rest will remain the same.
72      *
73      * @param oldLabel Label from previous successful build.
74      * @return Label to use for most recent successful build.
75      */

76     public String JavaDoc incrementLabel(String JavaDoc oldLabel, Element buildLog) {
77         String JavaDoc prefix =
78             oldLabel.substring(0, oldLabel.lastIndexOf(separator) + 1);
79         String JavaDoc suffix =
80             oldLabel.substring(
81                 oldLabel.lastIndexOf(separator) + 1,
82                 oldLabel.length());
83         int i = Integer.parseInt(suffix);
84         String JavaDoc 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     /**
94      * Set the pre/post behavior of the label incrementer.
95      */

96     public void setPreBuildIncrementer(boolean preInc) {
97         preIncrement = preInc;
98     }
99
100     /**
101      * Verify that the label specified is a valid label. In this case a valid
102      * label contains at least one separator character, and an integer after the last
103      * occurrence of the separator character.
104      */

105     public boolean isValidLabel(String JavaDoc label) {
106
107         if (label.indexOf(separator) < 0) {
108             return false;
109         }
110
111         try {
112             String JavaDoc suffix =
113                 label.substring(
114                     label.lastIndexOf(separator) + 1,
115                     label.length());
116             Integer.parseInt(suffix);
117             return true;
118         } catch (NumberFormatException JavaDoc e) {
119             return false;
120         }
121     }
122
123     public void setSeparator(String JavaDoc newSeparator) {
124         separator = newSeparator;
125     }
126
127     /**
128      * The instance must be fully initialized before calling this method.
129      * @throws IllegalStateException if the instance is not properly initialized
130      * e.g. if the {@link #setSeparator set separator} doesn't match the
131      * {@link #setDefaultLabel set default label}
132      */

133     public String JavaDoc getDefaultLabel() {
134         if (defaultLabel != null) {
135             final int separatorIndex = defaultLabel.lastIndexOf(separator);
136             if (separatorIndex == -1) {
137                  throw new IllegalStateException JavaDoc("separator \"" + separator
138                        + "\" not found in default Label " + defaultLabel);
139             }
140             defaultPrefix = defaultLabel.substring(0, separatorIndex);
141             String JavaDoc 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 JavaDoc label) {
149         defaultLabel = label;
150     }
151 }
152
Popular Tags