    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 */
    019package org.apache.wiki.tags;
    020
    021import org.apache.wiki.InternalWikiException;
    022import org.apache.wiki.api.core.Engine;
    023import org.apache.wiki.api.core.Page;
    024import org.apache.wiki.pages.PageManager;
    025
    026
    027/**
    028 *  Does a version check on the page.  Mode is as follows:
    029 *  <UL>
    030 *   <LI><b>latest</b> - Include body, if the page is the latest version.</li>
    031 *   <LI><b>notlatest</b> - Include body, if the page is NOT the latest version.</li>
    032 *   <li><b>first</b> - Include body, if page is the first version (version 1)</li>
    033 *   <li><b>notfirst</b> - Include bodt, if page is NOT the first version (version 1)</li>
    034 *  </UL>
    035 *  If the page does not exist, body content is never included.
    036 *
    037 *  @since 2.0
    038 */
    039public class CheckVersionTag extends WikiTagBase {
    040
    041    private static final long serialVersionUID = 0L;
    042
    043    private enum VersionMode {
    044        LATEST, NOTLATEST, FIRST, NOTFIRST
    045    }
    046
    047    private VersionMode m_mode;
    048
    049    /**
    050     * {@inheritDoc}
    051     */
    052    @Override
    053    public void initTag() {
    054        super.initTag();
    055        m_mode = VersionMode.LATEST;
    056    }
    057
    058    /**
    059     *  Sets the mode.
    060     *
    061     *  @param arg The mode to set.
    062     */
    063    public void setMode( final String arg ) {
    064        if( "latest".equals(arg) ) {
    065            m_mode = VersionMode.LATEST;
    066        } else if( "notfirst".equals(arg) ) {
    067            m_mode = VersionMode.NOTFIRST;
    068        } else if( "first".equals(arg) ) {
    069            m_mode = VersionMode.FIRST;
    070        } else {
    071            m_mode = VersionMode.NOTLATEST;
    072        }
    073    }
    074
    075    /**
    076     *  {@inheritDoc}
    077     */
    078    @Override
    079    public final int doWikiStartTag() {
    080        final Engine engine = m_wikiContext.getEngine();
    081        final Page page = m_wikiContext.getPage();
    082
    083        if( page != null && engine.getManager( PageManager.class ).wikiPageExists( page.getName() ) ) {
    084            final int version = page.getVersion();
    085            final boolean include;
    086            final Page latest = engine.getManager( PageManager.class ).getPage( page.getName() );
    087
    088            switch( m_mode ) {
    089                case LATEST    : include = ( version < 0 ) || ( latest.getVersion() == version ); break;
    090                case NOTLATEST : include = ( version > 0 ) && ( latest.getVersion() != version ); break;
    091                case FIRST     : include = ( version == 1 ) || ( version < 0 && latest.getVersion() == 1 ); break;
    092                case NOTFIRST  : include = version > 1; break;
    093                default: throw new InternalWikiException( "Mode which is not available!" );
    094            }
    095            if( include ) {
    096                return EVAL_BODY_INCLUDE;
    097            }
    098        }
    099        return SKIP_BODY;
    100    }
    101
    102}