Java Regex Version Validate parseVersion(String version, String currentVersion)

Here you can find the source of parseVersion(String version, String currentVersion)

Description

parse Version

License

Open Source License

Declaration

public static boolean parseVersion(String version, String currentVersion) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 xored software, Inc.  
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html  
 *
 * Contributors:/*from  w  w w. j  a va 2s.com*/
 *     xored software, Inc. - initial API and Implementation (Andrei Sobolev)
 *******************************************************************************/

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static final Pattern VERSION_PATTERN = Pattern
            .compile("([\\(\\[][^\\(\\)]*[\\)\\]])");
    public static final Pattern INTERVAL_PATTERN = Pattern
            .compile("([\\(\\[])(.*)[:;](.*)([\\)\\]])");

    public static boolean parseVersion(String version, String currentVersion) {

        if (version == null || version.length() == 0)
            return false;

        Matcher versionMatcher = VERSION_PATTERN.matcher(version);

        boolean isValid = false;

        while (versionMatcher.find()) {

            boolean isIntervalValid = true;

            String interval = versionMatcher.group(1);
            Matcher intervalMatcher = INTERVAL_PATTERN.matcher(interval);

            while (intervalMatcher.find()) {

                String lowerType = intervalMatcher.group(1);
                String lowerVersion = intervalMatcher.group(2);
                String upperVersion = intervalMatcher.group(3);
                String upperType = intervalMatcher.group(4);

                if (!lowerVersion.equals("-")) { //$NON-NLS-1$
                    if (lowerType.equals("(") //$NON-NLS-1$
                            && compareVersions(currentVersion, lowerVersion) <= 0) {
                        isIntervalValid = false;
                        continue;
                    }

                    if (lowerType.equals("[") //$NON-NLS-1$
                            && compareVersions(currentVersion, lowerVersion) < 0) {
                        isIntervalValid = false;
                        continue;
                    }
                }
                if (!upperVersion.equals("-")) { //$NON-NLS-1$
                    if (upperType.equals(")") //$NON-NLS-1$
                            && compareVersions(currentVersion, upperVersion) >= 0) {
                        isIntervalValid = false;
                        continue;
                    }

                    if (upperType.equals("]") //$NON-NLS-1$
                            && compareVersions(currentVersion, upperVersion) > 0) {
                        isIntervalValid = false;
                        continue;
                    }
                }
            }
            if (isIntervalValid) {
                isValid = true;
                break;
            }
        }
        return isValid;
    }

    public static int compareVersions(String v1, String v2) {
        String[] splited1 = v1.split("\\."); //$NON-NLS-1$
        String[] splited2 = v2.split("\\."); //$NON-NLS-1$

        int res = splited1.length - splited2.length;

        int min = (splited1.length < splited2.length) ? splited1.length
                : splited2.length;

        for (int i = 0; i < min; i++) {
            if (Integer.parseInt(splited1[i]) > Integer
                    .parseInt(splited2[i]))
                return 1;
            if (Integer.parseInt(splited1[i]) < Integer
                    .parseInt(splited2[i]))
                return -1;
        }
        return res;
    }
}

Related

  1. parseVersion(final String versionString)
  2. parseVersion(String s, String groupId, String artifactId)
  3. parseVersion(String version, int defaultRevision)
  4. parseVersionNamespace(String versionNamespace)