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 "cvs" 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       = "M8";
054
055    /** The JSPWiki major version. */
056    public static final int        VERSION       = 2;
057
058    /** The JSPWiki revision. */
059    public static final int        REVISION      = 11;
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 the 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.length() != 0 ? "-" + POSTFIX : "" ) +
082                                                   ( BUILD.length() != 0 ? "-" + 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 reqVersion       = versionComponents.length > 0 ? Integer.parseInt( versionComponents[0] ) : Release.VERSION;
113        final int reqRevision      = versionComponents.length > 1 ? Integer.parseInt( versionComponents[1] ) : Release.REVISION;
114        final int reqMinorRevision = versionComponents.length > 2 ? Integer.parseInt( versionComponents[2] ) : Release.MINORREVISION;
115
116        if( VERSION == reqVersion ) {
117            if( REVISION == reqRevision ) {
118                if( MINORREVISION == reqMinorRevision ) {
119                    return true;
120                }
121                return MINORREVISION > reqMinorRevision;
122            }
123            return REVISION > reqRevision;
124        }
125        return VERSION > reqVersion;
126    }
127
128    /**
129     *  Returns true, if this version of JSPWiki is older or equal than what is requested.
130     *
131     *  @param version A version parameter string (a.b.c-something)
132     *  @return A boolean value describing whether the given version is older than the current JSPWiki version
133     *  @since 2.4.57
134     *  @throws IllegalArgumentException If the version string could not be parsed.
135     */
136    public static boolean isOlderOrEqual( final String version ) throws IllegalArgumentException {
137        if( version == null ) {
138            return true;
139        }
140
141        final String[] versionComponents = StringUtils.split( version, VERSION_SEPARATORS );
142        final int reqVersion       = versionComponents.length > 0 ? Integer.parseInt( versionComponents[0] ) : Release.VERSION;
143        final int reqRevision      = versionComponents.length > 1 ? Integer.parseInt( versionComponents[1] ) : Release.REVISION;
144        final int reqMinorRevision = versionComponents.length > 2 ? Integer.parseInt( versionComponents[2] ) : Release.MINORREVISION;
145
146        if( VERSION == reqVersion ) {
147            if( REVISION == reqRevision ) {
148                if( MINORREVISION == reqMinorRevision ) {
149                    return true;
150                }
151                return MINORREVISION < reqMinorRevision;
152            }
153            return REVISION < reqRevision;
154        }
155        return VERSION < reqVersion;
156    }
157
158    /**
159     *  Executing this class directly from command line prints out the current version.  It is very useful for
160     *  things like different command line tools.
161     *  <P>Example:
162     *  <PRE>
163     *  % java org.apache.wiki.api.Release
164     *  1.9.26-cvs
165     *  </PRE>
166     *
167     *  @param argv The argument string.  This class takes in no arguments.
168     */
169    public static void main( final String[] argv ) {
170        System.out.println( VERSTR );
171    }
172
173}