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:
019: package org.apache.tools.ant.taskdefs.condition;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.util.DeweyDecimal;
024:
025: /**
026: * An Ant version condition.
027: * @since Ant 1.7
028: */
029: public class AntVersion implements Condition {
030:
031: private String atLeast = null;
032: private String exactly = null;
033:
034: /**
035: * Evalute the condition.
036: * @return true if the condition is true.
037: * @throws BuildException if an error occurs.
038: */
039: public boolean eval() throws BuildException {
040: validate();
041: DeweyDecimal actual = getVersion();
042: if (null != atLeast) {
043: return actual
044: .isGreaterThanOrEqual(new DeweyDecimal(atLeast));
045: }
046: if (null != exactly) {
047: return actual.isEqual(new DeweyDecimal(exactly));
048: }
049: //default
050: return false;
051: }
052:
053: private void validate() throws BuildException {
054: if (atLeast != null && exactly != null) {
055: throw new BuildException(
056: "Only one of atleast or exactly may be set.");
057: }
058: if (null == atLeast && null == exactly) {
059: throw new BuildException(
060: "One of atleast or exactly must be set.");
061: }
062: try {
063: if (atLeast != null) {
064: new DeweyDecimal(atLeast);
065: } else {
066: new DeweyDecimal(exactly);
067: }
068: } catch (NumberFormatException e) {
069: throw new BuildException(
070: "The argument is not a Dewey Decimal eg 1.1.0");
071: }
072: }
073:
074: private DeweyDecimal getVersion() {
075: Project p = new Project();
076: p.init();
077: char[] versionString = p.getProperty("ant.version")
078: .toCharArray();
079: StringBuffer sb = new StringBuffer();
080: boolean foundFirstDigit = false;
081: for (int i = 0; i < versionString.length; i++) {
082: if (Character.isDigit(versionString[i])) {
083: sb.append(versionString[i]);
084: foundFirstDigit = true;
085: }
086: if (versionString[i] == '.' && foundFirstDigit) {
087: sb.append(versionString[i]);
088: }
089: if (Character.isLetter(versionString[i]) && foundFirstDigit) {
090: break;
091: }
092: }
093: return new DeweyDecimal(sb.toString());
094: }
095:
096: /**
097: * Get the atleast attribute.
098: * @return the atleast attribute.
099: */
100: public String getAtLeast() {
101: return atLeast;
102: }
103:
104: /**
105: * Set the atleast attribute.
106: * This is of the form major.minor.point.
107: * For example 1.7.0.
108: * @param atLeast the version to check against.
109: */
110: public void setAtLeast(String atLeast) {
111: this .atLeast = atLeast;
112: }
113:
114: /**
115: * Get the exactly attribute.
116: * @return the exactly attribute.
117: */
118: public String getExactly() {
119: return exactly;
120: }
121:
122: /**
123: * Set the exactly attribute.
124: * This is of the form major.minor.point.
125: * For example 1.7.0.
126: * @param exactly the version to check against.
127: */
128: public void setExactly(String exactly) {
129: this.exactly = exactly;
130: }
131: }
|