KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > IncrementSpecificationVersions


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.io.OutputStreamWriter JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.regex.Matcher JavaDoc;
37 import java.util.regex.Pattern JavaDoc;
38 import org.apache.tools.ant.BuildException;
39 import org.apache.tools.ant.Project;
40 import org.apache.tools.ant.Task;
41
42 /**
43  * Increments specification versions of all specified modules,
44  * in the trunk or in a branch, in a regulated manner.
45  * @author Jesse Glick
46  */

47 public final class IncrementSpecificationVersions extends Task {
48     
49     private File JavaDoc nbroot;
50     private List JavaDoc<String JavaDoc> modules;
51     private int stickyLevel = -1;
52     
53     public IncrementSpecificationVersions() {}
54     
55     public void setNbroot(File JavaDoc f) {
56         nbroot = f;
57     }
58     
59     public void setModules(String JavaDoc m) {
60         modules = new ArrayList JavaDoc<String JavaDoc>();
61         for (Object JavaDoc o : Collections.list(new StringTokenizer JavaDoc(m, ", "))) {
62             modules.add((String JavaDoc) o);
63         }
64     }
65     
66     public void setBranch(boolean b) {
67         setStickyLevel(b ? 2 : 1);
68     }
69
70     /** Number of digits from the begining that are supposed to
71      * stay the same
72      */

73     public void setStickyLevel(int stickyLevel) {
74         if (this.stickyLevel != -1) {
75             throw new BuildException("Only one stickyLevel or branch attribute can be used!");
76         }
77
78         this.stickyLevel = stickyLevel;
79     }
80
81     public void execute() throws BuildException {
82         if (nbroot == null || modules == null) {
83             throw new BuildException("Missing params 'nbroot' or 'modules'", getLocation());
84         }
85         MODULE: for (String JavaDoc module : modules) {
86             File JavaDoc dir = new File JavaDoc(nbroot, module.replace('/', File.separatorChar));
87             if (!dir.isDirectory()) {
88                 log("No such directory " + dir + "; skipping", Project.MSG_WARN);
89                 continue;
90             }
91             try {
92                 File JavaDoc pp = new File JavaDoc(dir, "nbproject" + File.separatorChar + "project.properties");
93                 if (pp.isFile()) {
94                     String JavaDoc[] lines = gulp(pp, "ISO-8859-1");
95                     for (int i = 0; i < lines.length; i++) {
96                         Matcher JavaDoc m1 = Pattern.compile("(spec\\.version\\.base=)(.+)").matcher(lines[i]);
97                         if (m1.matches()) {
98                             String JavaDoc old = m1.group(2);
99                             String JavaDoc nue = increment(old, stickyLevel, false);
100                             if (nue != null) {
101                                 lines[i] = m1.group(1) + nue;
102                                 spit(pp, "ISO-8859-1", lines);
103                                 log("Incrementing " + old + " -> " + nue + " in " + pp);
104                             } else {
105                                 log(pp + ":" + (i + 1) + ": Unsupported old version number " + old + " (must be x.y.0 in trunk or x.y.z in branch); skipping", Project.MSG_WARN);
106                             }
107                             continue MODULE;
108                         }
109                     }
110                 } else {
111                     if (!new File JavaDoc(dir, "nbproject" + File.separatorChar + "project.xml").isFile()) {
112                         log("No such file " + pp + "; unprojectized module?", Project.MSG_WARN);
113                     }
114                 }
115                 File JavaDoc mf = new File JavaDoc(dir, "manifest.mf");
116                 if (mf.isFile()) {
117                     String JavaDoc[] lines = gulp(mf, "UTF-8");
118                     for (int i = 0; i < lines.length; i++) {
119                         Matcher JavaDoc m1 = Pattern.compile("(OpenIDE-Module-Specification-Version: )(.+)").matcher(lines[i]);
120                         if (m1.matches()) {
121                             String JavaDoc old = m1.group(2);
122                                 String JavaDoc nue = increment(old, stickyLevel, true);
123                             if (nue != null) {
124                                 lines[i] = m1.group(1) + nue;
125                                 spit(mf, "UTF-8", lines);
126                                 log("Incrementing " + old + " -> " + nue + " in " + mf);
127                             } else {
128                                 log(mf + ":" + (i + 1) + ": Unsupported old version number " + old + " (must be x.y in trunk or x.y.z in branch); skipping", Project.MSG_WARN);
129                             }
130                             continue MODULE;
131                         }
132                     }
133                 } else {
134                     log("No such file " + mf + "; not a real module?", Project.MSG_WARN);
135                 }
136                 log("Could not find any specification version in " + dir + "; skipping", Project.MSG_WARN);
137             } catch (IOException JavaDoc e) {
138                 throw new BuildException("While processing " + dir + ": " + e, e, getLocation());
139             }
140         }
141     }
142
143     /** Does the increment of the specification version to new version.
144      * @return the new version or null if the increment fails
145      */

146     static String JavaDoc increment(String JavaDoc old, int stickyLevel, boolean manifest) throws NumberFormatException JavaDoc {
147         String JavaDoc nue = null;
148
149         switch (stickyLevel) {
150             case 1: // trunk
151
if (manifest) {
152                     Matcher JavaDoc m2 = Pattern.compile("([0-9]+\\.)([0-9]+)").matcher(old);
153                     if (m2.matches()) {
154                         nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1);
155                     }
156                 } else {
157                     Matcher JavaDoc m2 = Pattern.compile("([0-9]+\\.)([0-9]+)(\\.0)").matcher(old);
158                     if (m2.matches()) {
159                         nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1) + m2.group(3);
160                     }
161                 }
162                 break;
163             case 2: // branch
164
if (manifest) {
165                     Matcher JavaDoc m2 = Pattern.compile("([0-9]+\\.[0-9]+\\.)([0-9]+)").matcher(old);
166                     if (m2.matches()) {
167                         nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1);
168                     } else if (old.matches("[0-9]+\\.[0-9]+")) {
169                         nue = old + ".1";
170                     }
171                 } else {
172                     Matcher JavaDoc m2 = Pattern.compile("([0-9]+\\.[0-9]+\\.)([0-9]+)").matcher(old);
173                     if (m2.matches()) {
174                         nue = m2.group(1) + (Integer.parseInt(m2.group(2)) + 1);
175                     }
176                 }
177                 break;
178             default:
179                 if (stickyLevel < 1) {
180                     throw new BuildException("Invalid sticky level: " + stickyLevel);
181                 }
182                 int[] segments = new int[stickyLevel + 1];
183                 StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(old, ".");
184                 for (int i = 0; i < segments.length && tok.hasMoreElements(); i++) {
185                     segments[i] = Integer.parseInt(tok.nextToken());
186                 }
187                 segments[stickyLevel]++;
188                 nue = "";
189                 String JavaDoc pref = "";
190                 for (int i = 0; i < segments.length; i++) {
191                     nue += pref;
192                     nue += segments[i];
193                     pref = ".";
194                 }
195                 break;
196         }
197
198         return nue;
199     }
200
201     private static String JavaDoc[] gulp(File JavaDoc file, String JavaDoc enc) throws IOException JavaDoc {
202         InputStream JavaDoc is = new FileInputStream JavaDoc(file);
203         try {
204             BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is, enc));
205             List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
206             String JavaDoc line;
207             while ((line = r.readLine()) != null) {
208                 l.add(line);
209             }
210             return l.toArray(new String JavaDoc[l.size()]);
211         } finally {
212             is.close();
213         }
214     }
215     
216     private static void spit(File JavaDoc file, String JavaDoc enc, String JavaDoc[] lines) throws IOException JavaDoc {
217         OutputStream JavaDoc os = new FileOutputStream JavaDoc(file);
218         try {
219             PrintWriter JavaDoc w = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(os, enc));
220             for (String JavaDoc line : lines) {
221                 w.println(line);
222             }
223             w.flush();
224         } finally {
225             os.close();
226         }
227     }
228
229 }
230
Popular Tags