001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import org.apache.commons.lang3.StringUtils;
019
020public class VersionUtils {
021
022        public static final String MAVEN_SNAPSHOT_TOKEN = "SNAPSHOT";
023        private static final String[] DELIMITERS = new String[] { ".", "-" };
024        private static final String SEPARATOR_CHARS = Str.toString(DELIMITERS);
025
026        /**
027         * Return true if <code>version</code> ends with <code>-SNAPSHOT</code> or <code>.SNAPSHOT</code> (case insensitive).
028         */
029        public static final boolean isSnapshot(String version) {
030                for (String delimiter : DELIMITERS) {
031                        String suffix = delimiter + MAVEN_SNAPSHOT_TOKEN;
032                        if (StringUtils.endsWithIgnoreCase(version, suffix)) {
033                                return true;
034                        }
035                }
036                return false;
037        }
038
039        /**
040         * Return <code>version</code> with <code>.SNAPSHOT</code> or <code>-SNAPSHOT</code> removed from the end (if present)
041         * 
042         * <pre>
043         * 1.0.0-SNAPSHOT returns 1.0.0
044         * 1.0.0.SNAPSHOT returns 1.0.0
045         * 1.0.0          returns 1.0.0
046         * 1.0.0SNAPSHOT  returns 1.0.0SNAPSHOT
047         * </pre>
048         */
049        public static final String trimSnapshot(String version) {
050                if (isSnapshot(version)) {
051                        int length = MAVEN_SNAPSHOT_TOKEN.length() + 1;
052                        return StringUtils.left(version, version.length() - length);
053                } else {
054                        return version;
055                }
056        }
057
058        /**
059         * Parse a <code>Version</code> object from the <code>version</code> string. The logic here is crudely simple. First <code>SNAPSHOT</code> is trimmed off the end of the string
060         * (if it exists). Whatever remains is then split into tokens using <code>.</code> and <code>-</code> as delimiters. The first 3 tokens encountered are <code>major</code>,
061         * <code>minor</code>, and <code>incremental</code>, respectively. Anything after that is the <code>qualifier</code>
062         */
063        public static final Version getVersion(String version) {
064                boolean snapshot = isSnapshot(version);
065                String trimmed = trimSnapshot(version);
066                Version v = new Version();
067                v.setTrimmed(trimmed);
068                v.setSnapshot(snapshot);
069                String[] tokens = StringUtils.split(trimmed, SEPARATOR_CHARS);
070                if (tokens.length > 0) {
071                        v.setMajor(tokens[0]);
072                }
073                if (tokens.length > 1) {
074                        v.setMinor(tokens[1]);
075                }
076                if (tokens.length > 2) {
077                        v.setIncremental(tokens[2]);
078                }
079                String qualifier = getQualifier(trimmed, tokens);
080                v.setQualifier(qualifier);
081                return v;
082        }
083
084        /**
085         * 2.4.0-beta1-SNAPSHOT -> 240BETA1
086         */
087        public static String asSanitizedString(Version version) {
088                StringBuilder sb = new StringBuilder();
089                sb.append(StringUtils.trimToEmpty(version.getMajor()));
090                sb.append(StringUtils.trimToEmpty(version.getMinor()));
091                sb.append(StringUtils.trimToEmpty(version.getIncremental()));
092                sb.append(StringUtils.trimToEmpty(version.getQualifier()));
093                return sanitize(sb.toString());
094        }
095
096        /**
097         * Convert dots and dashes to underscores and convert to uppercase
098         */
099        protected static String sanitize(String s) {
100                s = StringUtils.replace(s, ".", "_");
101                s = StringUtils.replace(s, "-", "_");
102                return StringUtils.upperCase(s);
103        }
104
105        /**
106         * Convert dots and dashes to underscores and convert to uppercase
107         */
108        public static final String getSanitizedQualifier(String qualifier) {
109                if (qualifier == null) {
110                        return null;
111                } else {
112                        return sanitize(qualifier);
113                }
114        }
115
116        protected static final String getQualifier(String trimmed, String[] tokens) {
117                if (tokens.length < 4) {
118                        return null;
119                }
120                int pos = tokens[0].length() + 1 + tokens[1].length() + 1 + tokens[2].length() + 1;
121                return trimmed.substring(pos);
122        }
123
124}