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[] = new String[] {
41 "(g?[_.0-9ab]*(pre|rc|g|m)[_.0-9]*)",
46 // "(test[_.0-9]*)", -- omitted for MRM-681, can be reinstated as part of MRM-712
48 "(unofficial[_.0-9]*)",
59 public static final String SNAPSHOT = "SNAPSHOT";
61 public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" );
63 public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" );
65 public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT );
67 private static final Pattern VERSION_MEGA_PATTERN = Pattern.compile( StringUtils.join( versionPatterns, '|' ), Pattern.CASE_INSENSITIVE );
71 * Tests if the unknown string contains elements that identify it as a version string (or not).
75 * The algorithm tests each part of the string that is delimited by a '-' (dash) character.
76 * If 75% or more of the sections are identified as 'version' strings, the result is
77 * determined to be of a high probability to be version identifier string.
80 * @param unknown the unknown string to test.
81 * @return true if the unknown string is likely a version string.
83 public static boolean isVersion( String unknown )
85 String versionParts[] = StringUtils.split( unknown, '-' );
89 int countValidParts = 0;
91 for ( int i = 0; i < versionParts.length; i++ )
93 String part = versionParts[i];
94 mat = VERSION_MEGA_PATTERN.matcher( part );
98 if ( i == 0 ) // loosen rule to return true if first token matches
106 /* Calculate version probability as true if 3/4's of the input string has pieces of
107 * of known version identifier strings.
109 int threshold = (int) Math.floor( Math.max( (double) 1.0, (double) ( versionParts.length * 0.75 ) ) );
111 return ( countValidParts >= threshold );
116 * Tests if the identifier is a known simple version keyword.
120 * This method is different from {@link #isVersion(String)} in that it tests the whole input string in
121 * one go as a simple identifier. (eg "alpha", "1.0", "beta", "debug", "latest", "rc#", etc...)
124 * @param identifier the identifier to test.
125 * @return true if the unknown string is likely a version string.
127 public static boolean isSimpleVersionKeyword( String identifier )
129 Matcher mat = VERSION_MEGA_PATTERN.matcher( identifier );
131 return mat.matches();
134 public static boolean isSnapshot( String version )
136 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
143 return isGenericSnapshot(version);
147 public static String getBaseVersion( String version )
149 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
152 return m.group( 1 ) + "-" + SNAPSHOT;
162 * Get the release version of the snapshot version.
166 * If snapshot version is 1.0-SNAPSHOT, then release version would be 1.0
167 * And if snapshot version is 1.0-20070113.163208-1.jar, then release version would still be 1.0
170 * @param snapshotVersion
173 public static String getReleaseVersion( String snapshotVersion )
175 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( snapshotVersion );
177 if( isGenericSnapshot( snapshotVersion ) )
179 m = GENERIC_SNAPSHOT_PATTERN.matcher( snapshotVersion );
188 return snapshotVersion;
192 public static boolean isUniqueSnapshot( String version )
194 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
203 public static boolean isGenericSnapshot( String version )
205 return version.endsWith( SNAPSHOT );