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.plugin; 020 021import org.apache.wiki.api.core.Context; 022import org.apache.wiki.api.exceptions.PluginException; 023import org.apache.wiki.api.plugin.Plugin; 024import org.apache.wiki.util.TextUtil; 025 026import java.util.Map; 027 028/** 029 * Provides a page-specific counter, it is reset every time a page is rendered, so it is not usable as a hitcounter. 030 * <br>Stores a variable in the WikiContext called "counter", with the name of the optionally specified variable "name". 031 * <br>For example: If name is "thispage", then the variable name is called "counter-thispage". 032 * 033 * <p>Parameters : </p> 034 * <ul> 035 * <li><b>name</b> - Name of the counter. Optional.</li> 036 * <li><b>increment</b> - The amount to increment, may be a negative value, default is 1. Optional.</li> 037 * <li><b>showResult</b> - Should the counter value be visible on the page, default is true. Optional.</li> 038 * </ul> 039 * 040 * @since 1.9.30 041 */ 042public class Counter implements Plugin { 043 // private static Logger log = LogManager.getLogger( Counter.class ); 044 045 /** Parameter name for setting the name. Value is <tt>{@value}</tt>. */ 046 public static final String PARAM_NAME = "name"; 047 /** Parameter name for setting the increment. Value is <tt>{@value}</tt>. */ 048 public static final String PARAM_INCREMENT = "increment"; 049 /** Parameter name for setting the showResult. Value is <tt>{@value}</tt>. */ 050 public static final String PARAM_SHOW_RESULT = "showResult"; 051 /** Parameter name for setting the start. Value is <tt>{@value}</tt>. */ 052 public static final String PARAM_START = "start"; 053 public static final String DEFAULT_NAME = "counter"; 054 private static final int DEFAULT_INCREMENT = 1; 055 private static final boolean DEFAULT_SHOW_RESULT = true; 056 057 /** 058 * {@inheritDoc} 059 */ 060 @Override 061 public String execute( final Context context, final Map< String, String > params ) throws PluginException { 062 // First, determine which kind of name we use to store in the WikiContext. 063 String countername = params.get( PARAM_NAME); 064 065 if( countername == null ) { 066 countername = DEFAULT_NAME; 067 } else { 068 countername = DEFAULT_NAME+"-"+countername; 069 } 070 071 // Fetch the old value 072 Integer val = context.getVariable( countername ); 073 if( val == null ) { 074 val = 0; 075 } 076 077 // Check if we need to reset this 078 final String start = params.get( PARAM_START ); 079 if( start != null ) { 080 val = Integer.parseInt( start ); 081 } else { 082 // Determine how much to increment 083 final String incrementObj = params.get( PARAM_INCREMENT ); 084 int increment = DEFAULT_INCREMENT; 085 if( incrementObj != null ) { 086 increment = Integer.parseInt( incrementObj ); 087 } 088 089 val = val + increment; 090 } 091 092 context.setVariable( countername, val ); 093 094 // check if we want to hide the result (just count, don't show result on the page 095 final String showObj = params.get(PARAM_SHOW_RESULT); 096 boolean show = DEFAULT_SHOW_RESULT; 097 if( showObj != null ) { 098 show = TextUtil.isPositive( showObj ); 099 } 100 101 if( show ) { 102 return val.toString(); 103 } 104 105 return ""; 106 } 107 108}