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 java.io.IOException; 022 023import org.apache.wiki.InternalWikiException; 024import org.apache.wiki.WikiEngine; 025import org.apache.wiki.WikiPage; 026import org.apache.wiki.api.exceptions.ProviderException; 027 028/** 029 * Does a version check on the page. Mode is as follows: 030 * <UL> 031 * <LI><b>latest</b> - Include body, if the page is the latest version.</li> 032 * <LI><b>notlatest</b> - Include body, if the page is NOT the latest version.</li> 033 * <li><b>first</b> - Include body, if page is the first version (version 1)</li> 034 * <li><b>notfirst</b> - Include bodt, if page is NOT the first version (version 1)</li> 035 * </UL> 036 * If the page does not exist, body content is never included. 037 * 038 * @since 2.0 039 */ 040public class CheckVersionTag 041 extends WikiTagBase 042{ 043 private static final long serialVersionUID = 0L; 044 045 private static enum VersionMode 046 { 047 LATEST, NOTLATEST, FIRST, NOTFIRST 048 } 049 050 private VersionMode m_mode; 051 052 /** 053 * {@inheritDoc} 054 */ 055 @Override 056 public void initTag() 057 { 058 super.initTag(); 059 m_mode = VersionMode.LATEST; 060 } 061 062 /** 063 * Sets the mode. 064 * 065 * @param arg The mode to set. 066 */ 067 public void setMode( String arg ) 068 { 069 if( "latest".equals(arg) ) 070 { 071 m_mode = VersionMode.LATEST; 072 } 073 else if( "notfirst".equals(arg) ) 074 { 075 m_mode = VersionMode.NOTFIRST; 076 } 077 else if( "first".equals(arg) ) 078 { 079 m_mode = VersionMode.FIRST; 080 } 081 else 082 { 083 m_mode = VersionMode.NOTLATEST; 084 } 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 @Override 091 public final int doWikiStartTag() 092 throws IOException, 093 ProviderException 094 { 095 WikiEngine engine = m_wikiContext.getEngine(); 096 WikiPage page = m_wikiContext.getPage(); 097 098 if( page != null && engine.pageExists(page.getName()) ) 099 { 100 int version = page.getVersion(); 101 boolean include = false; 102 103 WikiPage latest = engine.getPage( page.getName() ); 104 105 //log.debug("Doing version check: this="+page.getVersion()+ 106 // ", latest="+latest.getVersion()); 107 108 switch( m_mode ) 109 { 110 case LATEST: 111 include = (version < 0) || (latest.getVersion() == version); 112 break; 113 114 case NOTLATEST: 115 include = (version > 0) && (latest.getVersion() != version); 116 break; 117 118 case FIRST: 119 include = (version == 1 ) || (version < 0 && latest.getVersion() == 1); 120 break; 121 122 case NOTFIRST: 123 include = version > 1; 124 break; 125 126 default: 127 throw new InternalWikiException("Mode which is not available!"); 128 } 129 130 if( include ) 131 { 132 // log.debug("INCLD"); 133 return EVAL_BODY_INCLUDE; 134 } 135 } 136 137 return SKIP_BODY; 138 } 139}