1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
41
42
43
44
45
46
47
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
60
61
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
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
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
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 }