View Javadoc
1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import org.apache.commons.lang3.StringUtils;
19  
20  public class VersionUtils {
21  
22  	public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
23  	private static final String[] DELIMITERS = new String[] { ".", "-" };
24  	private static final String SEPARATOR_CHARS = Str.toString(DELIMITERS);
25  
26  	/**
27  	 * Return true if <code>version</code> ends with <code>-SNAPSHOT</code> or <code>.SNAPSHOT</code> (case insensitive).
28  	 */
29  	public static final boolean isSnapshot(String version) {
30  		for (String delimiter : DELIMITERS) {
31  			String suffix = delimiter + MAVEN_SNAPSHOT_TOKEN;
32  			if (StringUtils.endsWithIgnoreCase(version, suffix)) {
33  				return true;
34  			}
35  		}
36  		return false;
37  	}
38  
39  	/**
40  	 * Return <code>version</code> with <code>.SNAPSHOT</code> or <code>-SNAPSHOT</code> removed from the end (if present)
41  	 * 
42  	 * <pre>
43  	 * 1.0.0-SNAPSHOT returns 1.0.0
44  	 * 1.0.0.SNAPSHOT returns 1.0.0
45  	 * 1.0.0          returns 1.0.0
46  	 * 1.0.0SNAPSHOT  returns 1.0.0SNAPSHOT
47  	 * </pre>
48  	 */
49  	public static final String trimSnapshot(String version) {
50  		if (isSnapshot(version)) {
51  			int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
52  			return StringUtils.left(version, version.length() - length);
53  		} else {
54  			return version;
55  		}
56  	}
57  
58  	/**
59  	 * Parse a <code>Version</code> object from the <code>version</code> string. The logic here is crudely simple. First <code>SNAPSHOT</code> is trimmed off the end of the string
60  	 * (if it exists). Whatever remains is then split into tokens using <code>.</code> and <code>-</code> as delimiters. The first 3 tokens encountered are <code>major</code>,
61  	 * <code>minor</code>, and <code>incremental</code>, respectively. Anything after that is the <code>qualifier</code>
62  	 */
63  	public static final Version getVersion(String version) {
64  		boolean snapshot = isSnapshot(version);
65  		String trimmed = trimSnapshot(version);
66  		Version v = new Version();
67  		v.setTrimmed(trimmed);
68  		v.setSnapshot(snapshot);
69  		String[] tokens = StringUtils.split(trimmed, SEPARATOR_CHARS);
70  		if (tokens.length > 0) {
71  			v.setMajor(tokens[0]);
72  		}
73  		if (tokens.length > 1) {
74  			v.setMinor(tokens[1]);
75  		}
76  		if (tokens.length > 2) {
77  			v.setIncremental(tokens[2]);
78  		}
79  		String qualifier = getQualifier(trimmed, tokens);
80  		v.setQualifier(qualifier);
81  		return v;
82  	}
83  
84  	/**
85  	 * 2.4.0-beta1-SNAPSHOT -> 240BETA1
86  	 */
87  	public static String asSanitizedString(Version version) {
88  		StringBuilder sb = new StringBuilder();
89  		sb.append(StringUtils.trimToEmpty(version.getMajor()));
90  		sb.append(StringUtils.trimToEmpty(version.getMinor()));
91  		sb.append(StringUtils.trimToEmpty(version.getIncremental()));
92  		sb.append(StringUtils.trimToEmpty(version.getQualifier()));
93  		return sanitize(sb.toString());
94  	}
95  
96  	/**
97  	 * Convert dots and dashes to underscores and convert to uppercase
98  	 */
99  	protected static String sanitize(String s) {
100 		s = StringUtils.replace(s, ".", "_");
101 		s = StringUtils.replace(s, "-", "_");
102 		return StringUtils.upperCase(s);
103 	}
104 
105 	/**
106 	 * Convert dots and dashes to underscores and convert to uppercase
107 	 */
108 	public static final String getSanitizedQualifier(String qualifier) {
109 		if (qualifier == null) {
110 			return null;
111 		} else {
112 			return sanitize(qualifier);
113 		}
114 	}
115 
116 	protected static final String getQualifier(String trimmed, String[] tokens) {
117 		if (tokens.length < 4) {
118 			return null;
119 		}
120 		int pos = tokens[0].length() + 1 + tokens[1].length() + 1 + tokens[2].length() + 1;
121 		return trimmed.substring(pos);
122 	}
123 
124 }