diff options
author | Joakim Erdfelt <joakime@apache.org> | 2007-04-19 20:56:45 +0000 |
---|---|---|
committer | Joakim Erdfelt <joakime@apache.org> | 2007-04-19 20:56:45 +0000 |
commit | 93ab972d740e13e0a4607af4ff235bdef6df667e (patch) | |
tree | adec5edf69a3c44afa82150347f4576531793cc7 /archiva-base | |
parent | a2d0393d9413d5e5b98b80601eab4aedf6248b71 (diff) | |
download | archiva-93ab972d740e13e0a4607af4ff235bdef6df667e.tar.gz archiva-93ab972d740e13e0a4607af4ff235bdef6df667e.zip |
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
Diffstat (limited to 'archiva-base')
-rw-r--r-- | archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java | 94 |
1 files changed, 93 insertions, 1 deletions
diff --git a/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java b/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java index 91111e82c..228c3ff0f 100644 --- a/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java +++ b/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java @@ -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 ); |