    001/*
    002    Licensed to the Apache Software Foundation (ASF) under one
    003    or more contributor license agreements.  See the NOTICE file
    004    distributed with this work for additional information
    005    regarding copyright ownership.  The ASF licenses this file
    006    to you under the Apache License, Version 2.0 (the
    007    "License"); you may not use this file except in compliance
    008    with the License.  You may obtain a copy of the License at
    009
    010       http://www.apache.org/licenses/LICENSE-2.0
    011
    012    Unless required by applicable law or agreed to in writing,
    013    software distributed under the License is distributed on an
    014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015    KIND, either express or implied.  See the License for the
    016    specific language governing permissions and limitations
    017    under the License.
    018 */
    019
    020package org.apache.wiki.api;
    021
    022import org.apache.commons.lang3.StringUtils;
    023
    024
    025/**
    026 *  Contains release and version information.  You may also invoke this class directly, in which case it prints
    027 *  out the version string.  This is a handy way of checking which JSPWiki version you have - just type from a command line:
    028 *  <pre>
    029 *  % java -cp JSPWiki.jar org.apache.wiki.api.Release
    030 *  2.5.38
    031 *  </pre>
    032 *  <p>
    033 *  As a historical curiosity, this is the oldest JSPWiki file.  According to the CVS history, it dates from 6.7.2001, and it really hasn't
    034 *  changed much since.
    035 *  </p>
    036 *  @since  1.0
    037 */
    038public final class Release {
    039
    040    private static final String VERSION_SEPARATORS = ".-";
    041
    042    /**
    043     *  This is the default application name.
    044     */
    045    public static final String     APPNAME       = "JSPWiki";
    046
    047    /**
    048     *  This should be empty when doing a release - otherwise keep it as "-git-" so that whenever someone checks out the code, they know
    049     *  it is a bleeding-edge version.  Other possible values are "-alpha-" and "-beta-" for alpha and beta versions, respectively.
    050     *  <p>
    051     *  If the POSTFIX is empty, it is not added to the version string.
    052     */
    053    private static final String    POSTFIX       = "";
    054
    055    /** The JSPWiki major version. */
    056    public static final int        VERSION       = 3;
    057
    058    /** The JSPWiki revision. */
    059    public static final int        REVISION      = 0;
    060
    061    /** The minor revision.  */
    062    public static final int        MINORREVISION = 0;
    063
    064    /** The build number/identifier.  This is a String as opposed to an integer, just so that people can add other identifiers to it.
    065     * The build number is incremented every time a committer checks in code, and reset when a release is made.
    066     *  <p>
    067     *  If you are a person who likes to build his own releases, we recommend that you add your initials to this
    068     *  identifier (e.g. "13-jj", or "49-aj").
    069     *  <p>
    070     *  If the build identifier is empty, it is not added.
    071     */
    072    public static final String     BUILD         = "";
    073
    074    /**
    075     *  This is the generic version string you should use when printing out the version.  It is of
    076     *  the form "VERSION.REVISION.MINORREVISION[-POSTFIX][-BUILD]".
    077     */
    078    public static final String     VERSTR        = VERSION + "." +
    079                                                   REVISION + "." +
    080                                                   MINORREVISION +
    081                                                   POSTFIX +
    082                                                   BUILD;
    083
    084    /** Private constructor prevents instantiation. */
    085    private Release() {
    086    }
    087
    088    /**
    089     *  This method is useful for templates, because hopefully it will not be inlined, and thus any change to version number does not
    090     *  need recompiling the pages.
    091     *
    092     *  @since 2.1.26.
    093     *  @return The version string (e.g. 2.5.23).
    094     */
    095    public static String getVersionString() {
    096        return VERSTR;
    097    }
    098
    099    /**
    100     *  Returns true, if this version of JSPWiki is newer or equal than what is requested.
    101     *
    102     *  @param version A version parameter string (a.b.c-something). B and C are optional.
    103     *  @return A boolean value describing whether the given version is newer than the current JSPWiki.
    104     *  @since 2.4.57
    105     *  @throws IllegalArgumentException If the version string could not be parsed.
    106     */
    107    public static boolean isNewerOrEqual( final String version ) throws IllegalArgumentException {
    108        if( version == null ) {
    109            return true;
    110        }
    111        final String[] versionComponents = StringUtils.split( version, VERSION_SEPARATORS );
    112        final int versionCompoLength = versionComponents.length;
    113        final int reqVersion       = versionCompoLength > 0 ? Integer.parseInt( versionComponents[0] ) : Release.VERSION;
    114        final int reqRevision      = versionCompoLength > 1 ? Integer.parseInt( versionComponents[1] ) : Release.REVISION;
    115        final int reqMinorRevision = versionCompoLength > 2 ? Integer.parseInt( versionComponents[2] ) : Release.MINORREVISION;
    116
    117        if( VERSION == reqVersion ) {
    118            if( REVISION == reqRevision ) {
    119                if( MINORREVISION == reqMinorRevision ) {
    120                    return true;
    121                }
    122                return MINORREVISION > reqMinorRevision;
    123            }
    124            return REVISION > reqRevision;
    125        }
    126        return VERSION > reqVersion;
    127    }
    128
    129    /**
    130     *  Returns true, if this version of JSPWiki is older or equal than what is requested.
    131     *
    132     *  @param version A version parameter string (a.b.c-something)
    133     *  @return A boolean value describing whether the given version is older than the current JSPWiki version
    134     *  @since 2.4.57
    135     *  @throws IllegalArgumentException If the version string could not be parsed.
    136     */
    137    public static boolean isOlderOrEqual( final String version ) throws IllegalArgumentException {
    138        if( version == null ) {
    139            return true;
    140        }
    141
    142        final String[] versionComponents = StringUtils.split( version, VERSION_SEPARATORS );
    143        final int versionCompoLength = versionComponents.length;
    144        final int reqVersion       = versionCompoLength > 0 ? Integer.parseInt( versionComponents[0] ) : Release.VERSION;
    145        final int reqRevision      = versionCompoLength > 1 ? Integer.parseInt( versionComponents[1] ) : Release.REVISION;
    146        final int reqMinorRevision = versionCompoLength > 2 ? Integer.parseInt( versionComponents[2] ) : Release.MINORREVISION;
    147
    148        if( VERSION == reqVersion ) {
    149            if( REVISION == reqRevision ) {
    150                if( MINORREVISION == reqMinorRevision ) {
    151                    return true;
    152                }
    153                return MINORREVISION < reqMinorRevision;
    154            }
    155            return REVISION < reqRevision;
    156        }
    157        return VERSION < reqVersion;
    158    }
    159
    160    /**
    161     *  Executing this class directly from command line prints out the current version.  It is very useful for
    162     *  things like different command line tools.
    163     *  <P>Example:
    164     *  <PRE>
    165     *  % java org.apache.wiki.api.Release
    166     *  1.9.26-cvs
    167     *  </PRE>
    168     *
    169     *  @param argv The argument string.  This class takes in no arguments.
    170     */
    171    public static void main( final String[] argv ) {
    172        System.out.println( VERSTR );
    173    }
    174
    175}