1 package org.apache.maven.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.
30 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
33 public class VersionUtil
36 * These are the version patterns found in the filenames of the various artifact's versions IDs.
37 * These patterns are all tackling lowercase version IDs.
39 private static final String versionPatterns[] = new String[] {
42 "(g?[_.0-9ab]*(pre|rc|g|m)[_.0-9]*)",
49 "(unofficial[_.0-9]*)",
60 private static final String VersionMegaPattern = StringUtils.join( versionPatterns, '|' );
62 public static final String SNAPSHOT = "SNAPSHOT";
64 public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" );
66 public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" );
68 public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT );
72 * Tests if the unknown string contains elements that identify it as a version string (or not).
76 * The algorithm tests each part of the string that is delimited by a '-' (dash) character.
77 * If 75% or more of the sections are identified as 'version' strings, the result is
78 * determined to be of a high probability to be version identifier string.
81 * @param unknown the unknown string to test.
82 * @return true if the unknown string is likely a version string.
84 public static boolean isVersion( String unknown )
86 String versionParts[] = StringUtils.split( unknown, '-' );
88 Pattern pat = Pattern.compile( VersionMegaPattern, Pattern.CASE_INSENSITIVE );
91 int countValidParts = 0;
93 for ( int i = 0; i < versionParts.length; i++ )
95 String part = versionParts[i];
96 mat = pat.matcher( part );
104 /* Calculate version probability as true if 3/4's of the input string has pieces of
105 * of known version identifier strings.
107 int threshold = (int) Math.floor( Math.max( (double) 1.0, (double) ( versionParts.length * 0.75 ) ) );
109 return ( countValidParts >= threshold );
114 * Tests if the identifier is a known simple version keyword.
118 * This method is different from {@link #isVersion(String)} in that it tests the whole input string in
119 * one go as a simple identifier. (eg "alpha", "1.0", "beta", "debug", "latest", "rc#", etc...)
122 * @param identifier the identifier to test.
123 * @return true if the unknown string is likely a version string.
125 public static boolean isSimpleVersionKeyword( String identifier )
127 Pattern pat = Pattern.compile( VersionMegaPattern, Pattern.CASE_INSENSITIVE );
128 Matcher mat = pat.matcher( identifier );
130 return mat.matches();
133 public static boolean isSnapshot( String version )
135 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
142 return version.endsWith( SNAPSHOT );
146 public static String getBaseVersion( String version )
148 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
151 return m.group( 1 ) + "-" + SNAPSHOT;
161 * Get the release version of the snapshot version.
165 * If snapshot version is 1.0-SNAPSHOT, then release version would be 1.0
166 * And if snapshot version is 1.0-20070113.163208-1.jar, then release version would still be 1.0
169 * @param snapshotVersion
172 public static String getReleaseVersion( String snapshotVersion )
174 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( snapshotVersion );
176 if( isGenericSnapshot( snapshotVersion ) )
178 m = GENERIC_SNAPSHOT_PATTERN.matcher( snapshotVersion );
187 return snapshotVersion;
191 public static boolean isUniqueSnapshot( String version )
193 Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );
202 public static boolean isGenericSnapshot( String version )
204 return version.endsWith( SNAPSHOT );