KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > cvslib > CvsVersion


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.cvslib;
19
20 import org.apache.tools.ant.taskdefs.AbstractCvsTask;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 /**
26  * this task allows to find out the client and the server version of a
27  * CVS installation
28  *
29  * example usage :
30  * <cvsversion
31  * cvsRoot=":pserver:anoncvs@cvs.apache.org:/home/cvspublic"
32  * passfile="c:/programme/cygwin/home/antoine/.cvspass"
33  * clientversionproperty="apacheclient"
34  * serverversionproperty="apacheserver" />
35  *
36  * the task can be used also in the API by calling its execute method,
37  * then calling getServerVersion and/or getClientVersion
38  *
39  * @ant.task category="scm"
40  * @since ant 1.6.1
41  */

42 public class CvsVersion extends AbstractCvsTask {
43     static final long VERSION_1_11_2 = 11102;
44     static final long MULTIPLY = 100;
45     private String JavaDoc clientVersion;
46     private String JavaDoc serverVersion;
47     private String JavaDoc clientVersionProperty;
48     private String JavaDoc serverVersionProperty;
49
50     /**
51      * Get the CVS client version
52      * @return CVS client version
53      */

54     public String JavaDoc getClientVersion() {
55         return clientVersion;
56     }
57     /**
58      * Get the CVS server version
59      * @return CVS server version
60      */

61     public String JavaDoc getServerVersion() {
62         return serverVersion;
63     }
64     /**
65      * Set a property where to store the CVS client version
66      * @param clientVersionProperty property for CVS client version
67      */

68     public void setClientVersionProperty(String JavaDoc clientVersionProperty) {
69         this.clientVersionProperty = clientVersionProperty;
70     }
71
72     /**
73      * Set a property where to store the CVS server version
74      * @param serverVersionProperty property for CVS server version
75      */

76     public void setServerVersionProperty(String JavaDoc serverVersionProperty) {
77         this.serverVersionProperty = serverVersionProperty;
78     }
79     /**
80      * Find out if the server version supports log with S option
81      * @return boolean indicating if the server version supports log with S option
82      */

83     public boolean supportsCvsLogWithSOption() {
84         if (serverVersion == null) {
85             return false;
86         }
87         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(serverVersion, ".");
88         long counter = MULTIPLY * MULTIPLY;
89         long version = 0;
90         while (tokenizer.hasMoreTokens()) {
91             String JavaDoc s = tokenizer.nextToken();
92             int i = 0;
93             for (i = 0; i < s.length(); i++) {
94                 if (!Character.isDigit(s.charAt(i))) {
95                     break;
96                 }
97             }
98             String JavaDoc s2 = s.substring(0, i);
99             version = version + counter * Long.parseLong(s2);
100             if (counter == 1) {
101                 break;
102             }
103             counter = counter / MULTIPLY;
104         }
105         return (version >= VERSION_1_11_2);
106     }
107     /**
108      * the execute method running CvsVersion
109      */

110     public void execute() {
111         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
112         this.setOutputStream(bos);
113         ByteArrayOutputStream JavaDoc berr = new ByteArrayOutputStream JavaDoc();
114         this.setErrorStream(berr);
115         setCommand("version");
116         super.execute();
117         String JavaDoc output = bos.toString();
118         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(output);
119         boolean client = false;
120         boolean server = false;
121         boolean cvs = false;
122         while (st.hasMoreTokens()) {
123             String JavaDoc currentToken = st.nextToken();
124             if (currentToken.equals("Client:")) {
125                 client = true;
126             } else if (currentToken.equals("Server:")) {
127                 server = true;
128             } else if (currentToken.equals("(CVS)")) {
129                 cvs = true;
130             }
131             if (client && cvs) {
132                 if (st.hasMoreTokens()) {
133                     clientVersion = st.nextToken();
134                 }
135                 client = false;
136                 cvs = false;
137             } else if (server && cvs) {
138                 if (st.hasMoreTokens()) {
139                     serverVersion = st.nextToken();
140                 }
141                 server = false;
142                 cvs = false;
143             }
144
145         }
146         if (clientVersionProperty != null) {
147             getProject().setNewProperty(clientVersionProperty, clientVersion);
148         }
149         if (serverVersionProperty != null) {
150             getProject().setNewProperty(serverVersionProperty, serverVersion);
151         }
152     }
153 }
154
Popular Tags