001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs.cvslib;
019:
020: import org.apache.tools.ant.taskdefs.AbstractCvsTask;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.util.StringTokenizer;
024:
025: /**
026: * this task allows to find out the client and the server version of a
027: * CVS installation
028: *
029: * example usage :
030: * <cvsversion
031: * cvsRoot=":pserver:anoncvs@cvs.apache.org:/home/cvspublic"
032: * passfile="c:/programme/cygwin/home/antoine/.cvspass"
033: * clientversionproperty="apacheclient"
034: * serverversionproperty="apacheserver" />
035: *
036: * the task can be used also in the API by calling its execute method,
037: * then calling getServerVersion and/or getClientVersion
038: *
039: * @ant.task category="scm"
040: * @since ant 1.6.1
041: */
042: public class CvsVersion extends AbstractCvsTask {
043: static final long VERSION_1_11_2 = 11102;
044: static final long MULTIPLY = 100;
045: private String clientVersion;
046: private String serverVersion;
047: private String clientVersionProperty;
048: private String serverVersionProperty;
049:
050: /**
051: * Get the CVS client version
052: * @return CVS client version
053: */
054: public String getClientVersion() {
055: return clientVersion;
056: }
057:
058: /**
059: * Get the CVS server version
060: * @return CVS server version
061: */
062: public String getServerVersion() {
063: return serverVersion;
064: }
065:
066: /**
067: * Set a property where to store the CVS client version
068: * @param clientVersionProperty property for CVS client version
069: */
070: public void setClientVersionProperty(String clientVersionProperty) {
071: this .clientVersionProperty = clientVersionProperty;
072: }
073:
074: /**
075: * Set a property where to store the CVS server version
076: * @param serverVersionProperty property for CVS server version
077: */
078: public void setServerVersionProperty(String serverVersionProperty) {
079: this .serverVersionProperty = serverVersionProperty;
080: }
081:
082: /**
083: * Find out if the server version supports log with S option
084: * @return boolean indicating if the server version supports log with S option
085: */
086: public boolean supportsCvsLogWithSOption() {
087: if (serverVersion == null) {
088: return false;
089: }
090: StringTokenizer tokenizer = new StringTokenizer(serverVersion,
091: ".");
092: long counter = MULTIPLY * MULTIPLY;
093: long version = 0;
094: while (tokenizer.hasMoreTokens()) {
095: String s = tokenizer.nextToken();
096: int i = 0;
097: for (i = 0; i < s.length(); i++) {
098: if (!Character.isDigit(s.charAt(i))) {
099: break;
100: }
101: }
102: String s2 = s.substring(0, i);
103: version = version + counter * Long.parseLong(s2);
104: if (counter == 1) {
105: break;
106: }
107: counter = counter / MULTIPLY;
108: }
109: return (version >= VERSION_1_11_2);
110: }
111:
112: /**
113: * the execute method running CvsVersion
114: */
115: public void execute() {
116: ByteArrayOutputStream bos = new ByteArrayOutputStream();
117: this .setOutputStream(bos);
118: ByteArrayOutputStream berr = new ByteArrayOutputStream();
119: this .setErrorStream(berr);
120: setCommand("version");
121: super .execute();
122: String output = bos.toString();
123: StringTokenizer st = new StringTokenizer(output);
124: boolean client = false;
125: boolean server = false;
126: boolean cvs = false;
127: while (st.hasMoreTokens()) {
128: String currentToken = st.nextToken();
129: if (currentToken.equals("Client:")) {
130: client = true;
131: } else if (currentToken.equals("Server:")) {
132: server = true;
133: } else if (currentToken.equals("(CVS)")) {
134: cvs = true;
135: }
136: if (client && cvs) {
137: if (st.hasMoreTokens()) {
138: clientVersion = st.nextToken();
139: }
140: client = false;
141: cvs = false;
142: } else if (server && cvs) {
143: if (st.hasMoreTokens()) {
144: serverVersion = st.nextToken();
145: }
146: server = false;
147: cvs = false;
148: }
149:
150: }
151: if (clientVersionProperty != null) {
152: getProject().setNewProperty(clientVersionProperty,
153: clientVersion);
154: }
155: if (serverVersionProperty != null) {
156: getProject().setNewProperty(serverVersionProperty,
157: serverVersion);
158: }
159: }
160: }
|