Author: joakime
Date: Thu Apr 19 13:56:45 2007
New Revision: 530544

URL: http://svn.apache.org/viewvc?view=rev&rev=530544
Log:
Beefing up VersionUtil to help in webapp.


Modified:
    
maven/archiva/branches/archiva-jpox-database-refactor/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java

Modified: 
maven/archiva/branches/archiva-jpox-database-refactor/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java
URL: 
http://svn.apache.org/viewvc/maven/archiva/branches/archiva-jpox-database-refactor/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java?view=diff&rev=530544&r1=530543&r2=530544
==============================================================================
--- 
maven/archiva/branches/archiva-jpox-database-refactor/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java
 (original)
+++ 
maven/archiva/branches/archiva-jpox-database-refactor/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/utils/VersionUtil.java
 Thu Apr 19 13:56:45 2007
@@ -19,20 +19,112 @@
  * 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:[EMAIL PROTECTED]">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 [EMAIL PROTECTED] #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 )
     {


Reply via email to