1 package org.apache.archiva.common.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27
28
29
30 public class VersionUtil
31 {
32
33
34
35
36 private static final String versionPatterns[] =
37 new String[]{ "([0-9][_.0-9a-z]*)", "(snapshot)", "(g?[_.0-9ab]*(pre|rc|g|m)[_.0-9]*)", "(dev[_.0-9]*)",
38 "(alpha[_.0-9]*)", "(beta[_.0-9]*)", "(rc[_.0-9]*)",
39
40 "(debug[_.0-9]*)", "(unofficial[_.0-9]*)", "(current)", "(latest)", "(fcs)", "(release[_.0-9]*)",
41 "(nightly)", "(final)", "(incubating)", "(incubator)", "([ab][_.0-9]+)" };
42
43 public static final String SNAPSHOT = "SNAPSHOT";
44
45 public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" );
46
47 public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" );
48
49 public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT );
50
51 private static final Pattern VERSION_MEGA_PATTERN =
52 Pattern.compile( StringUtils.join( versionPatterns, '|' ), Pattern.CASE_INSENSITIVE );
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static boolean isVersion( String unknown )
69 {
70 String versionParts[] = StringUtils.split( unknown, '-' );
71
72 Matcher mat;
73
74 int countValidParts = 0;
75
76 for ( int i = 0; i < versionParts.length; i++ )
77 {
78 String part = versionParts[i];
79 mat = VERSION_MEGA_PATTERN.matcher( part );
80
81 if ( mat.matches() )
82 {
83 if ( i == 0 )
84 {
85 return true;
86 }
87 countValidParts++;
88 }
89 }
90
91
92
93
94 int threshold = (int) Math.floor( Math.max( (double) 1.0, (double) ( versionParts.length * 0.75 ) ) );
95
96 return ( countValidParts >= threshold );
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 public static boolean isSimpleVersionKeyword( String identifier )
113 {
114 Matcher mat = VERSION_MEGA_PATTERN.matcher( identifier );
115
116 return mat.matches();
117 }
118
119 public static boolean isSnapshot( String version )
120 {
121 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
122 if ( m.matches() )
123 {
124 return true;
125 }
126 else
127 {
128 return isGenericSnapshot( version );
129 }
130 }
131
132 public static String getBaseVersion( String version )
133 {
134 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
135 if ( m.matches() )
136 {
137 return m.group( 1 ) + "-" + SNAPSHOT;
138 }
139 else
140 {
141 return version;
142 }
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static String getReleaseVersion( String snapshotVersion )
159 {
160 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( snapshotVersion );
161
162 if ( isGenericSnapshot( snapshotVersion ) )
163 {
164 m = GENERIC_SNAPSHOT_PATTERN.matcher( snapshotVersion );
165 }
166
167 if ( m.matches() )
168 {
169 return m.group( 1 );
170 }
171 else
172 {
173 return snapshotVersion;
174 }
175 }
176
177 public static boolean isUniqueSnapshot( String version )
178 {
179 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
180 return m.matches();
181 }
182
183 public static boolean isGenericSnapshot( String version )
184 {
185 return version.endsWith( SNAPSHOT );
186 }
187
188 public static String getVersionFromGenericSnapshot( String version )
189 {
190 Matcher m = GENERIC_SNAPSHOT_PATTERN.matcher( version );
191 if ( m.matches() )
192 {
193 return m.group( 1 );
194 }
195 return version;
196 }
197 }