1 package org.apache.archiva.common.utils;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
28 * Version utility methods.
32 public class VersionUtil
35 * These are the version patterns found in the filenames of the various artifact's versions IDs.
36 * These patterns are all tackling lowercase version IDs.
38 private static final String versionPatterns[] =
39 new String[]{ "([0-9][_.0-9a-z]*)", "(snapshot)", "(g?[_.0-9ab]*(pre|rc|g|m)[_.0-9]*)", "(dev[_.0-9]*)",
40 "(alpha[_.0-9]*)", "(beta[_.0-9]*)", "(rc[_.0-9]*)",
41 // "(test[_.0-9]*)", -- omitted for MRM-681, can be reinstated as part of MRM-712
42 "(debug[_.0-9]*)", "(unofficial[_.0-9]*)", "(current)", "(latest)", "(fcs)", "(release[_.0-9]*)",
43 "(nightly)", "(final)", "(incubating)", "(incubator)", "([ab][_.0-9]+)" };
45 public static final String SNAPSHOT = "SNAPSHOT";
47 public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" );
49 public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" );
51 public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT );
53 private static final Pattern VERSION_MEGA_PATTERN =
54 Pattern.compile( StringUtils.join( versionPatterns, '|' ), Pattern.CASE_INSENSITIVE );
58 * Tests if the unknown string contains elements that identify it as a version string (or not).
62 * The algorithm tests each part of the string that is delimited by a '-' (dash) character.
63 * If 75% or more of the sections are identified as 'version' strings, the result is
64 * determined to be of a high probability to be version identifier string.
67 * @param unknown the unknown string to test.
68 * @return true if the unknown string is likely a version string.
70 public static boolean isVersion( String unknown )
72 String versionParts[] = StringUtils.split( unknown, '-' );
76 int countValidParts = 0;
78 for ( int i = 0; i < versionParts.length; i++ )
80 String part = versionParts[i];
81 mat = VERSION_MEGA_PATTERN.matcher( part );
85 if ( i == 0 ) // loosen rule to return true if first token matches
93 /* Calculate version probability as true if 3/4's of the input string has pieces of
94 * of known version identifier strings.
96 int threshold = (int) Math.floor( Math.max( (double) 1.0, (double) ( versionParts.length * 0.75 ) ) );
98 return ( countValidParts >= threshold );
103 * Tests if the identifier is a known simple version keyword.
107 * This method is different from {@link #isVersion(String)} in that it tests the whole input string in
108 * one go as a simple identifier. (eg "alpha", "1.0", "beta", "debug", "latest", "rc#", etc...)
111 * @param identifier the identifier to test.
112 * @return true if the unknown string is likely a version string.
114 public static boolean isSimpleVersionKeyword( String identifier )
116 Matcher mat = VERSION_MEGA_PATTERN.matcher( identifier );
118 return mat.matches();
121 public static boolean isSnapshot( String version )
123 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
130 return isGenericSnapshot( version );
134 public static String getBaseVersion( String version )
136 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
139 return m.group( 1 ) + "-" + SNAPSHOT;
149 * Get the release version of the snapshot version.
153 * If snapshot version is 1.0-SNAPSHOT, then release version would be 1.0
154 * And if snapshot version is 1.0-20070113.163208-1.jar, then release version would still be 1.0
157 * @param snapshotVersion
160 public static String getReleaseVersion( String snapshotVersion )
162 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( snapshotVersion );
164 if ( isGenericSnapshot( snapshotVersion ) )
166 m = GENERIC_SNAPSHOT_PATTERN.matcher( snapshotVersion );
175 return snapshotVersion;
179 public static boolean isUniqueSnapshot( String version )
181 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
190 public static boolean isGenericSnapshot( String version )
192 return version.endsWith( SNAPSHOT );