Beefing up VersionUtil to help in webapp.

git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches/archiva-jpox-database-refactor@530544 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joakim Erdfelt 2007-04-19 20:56:45 +00:00
parent a2d0393d94
commit 93ab972d74
1 changed files with 93 additions and 1 deletions

View File

@ -19,21 +19,113 @@ package org.apache.maven.archiva.common.utils;
* under the License.
*/
import org.apache.commons.lang.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* VersionConstants
* Version utility methods.
*
* @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
* @version $Id$
*/
public class VersionUtil
{
/**
* These are the version patterns found in the filenames of the various artifact's versions IDs.
* These patterns are all tackling lowercase version IDs.
*/
private static final String versionPatterns[] = new String[] {
"([0-9][_.0-9a-z]*)",
"(snapshot)",
"(g?[_.0-9ab]*(pre|rc|g|m)[_.0-9]*)",
"(dev[_.0-9]*)",
"(alpha[_.0-9]*)",
"(beta[_.0-9]*)",
"(rc[_.0-9]*)",
"(test[_.0-9]*)",
"(debug[_.0-9]*)",
"(unofficial[_.0-9]*)",
"(current)",
"(latest)",
"(fcs)",
"(release[_.0-9]*)",
"(nightly)",
"(final)",
"(incubating)",
"(incubator)",
"([ab][_.0-9]+)" };
private static final String VersionMegaPattern = StringUtils.join( versionPatterns, '|' );
public static final String SNAPSHOT = "SNAPSHOT";
public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-([0-9]{8}\\.[0-9]{6})-([0-9]+)$" );
/**
* <p>
* Tests if the unknown string contains elements that identify it as a version string (or not).
* </p>
*
* <p>
* The algorithm tests each part of the string that is delimited by a '-' (dash) character.
* If 75% or more of the sections are identified as 'version' strings, the result is
* determined to be of a high probability to be version identifier string.
* </p>
*
* @param unknown the unknown string to test.
* @return true if the unknown string is likely a version string.
*/
public static boolean isVersion( String unknown )
{
String versionParts[] = StringUtils.split( unknown, '-' );
Pattern pat = Pattern.compile( VersionMegaPattern, Pattern.CASE_INSENSITIVE );
Matcher mat;
int countValidParts = 0;
for ( int i = 0; i < versionParts.length; i++ )
{
String part = versionParts[i];
mat = pat.matcher( part );
if ( mat.matches() )
{
countValidParts++;
}
}
/* Calculate version probability as true if 3/4's of the input string has pieces of
* of known version identifier strings.
*/
int threshold = (int) Math.floor( Math.max( (double) 1.0, (double) ( versionParts.length * 0.75 ) ) );
return ( countValidParts >= threshold );
}
/**
* <p>
* Tests if the identifier is a known simple version keyword.
* </p>
*
* <p>
* This method is different from {@link #isVersion(String)} in that it tests the whole input string in
* one go as a simple identifier. (eg "alpha", "1.0", "beta", "debug", "latest", "rc#", etc...)
* </p>
*
* @param identifier the identifier to test.
* @return true if the unknown string is likely a version string.
*/
public static boolean isSimpleVersionKeyword( String identifier )
{
Pattern pat = Pattern.compile( VersionMegaPattern, Pattern.CASE_INSENSITIVE );
Matcher mat = pat.matcher( identifier );
return mat.matches();
}
public static boolean isSnapshot( String version )
{
Matcher m = UNIQUE_SNAPSHOT_PATTERN.matcher( version );