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 package org.apache.wiki.plugin;
020
021 import java.util.Map;
022
023 import org.apache.wiki.WikiContext;
024 import org.apache.wiki.api.exceptions.PluginException;
025 import org.apache.wiki.api.plugin.WikiPlugin;
026 import org.apache.wiki.util.TextUtil;
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 */
042 public class Counter
043 implements WikiPlugin
044 {
045 // private static Logger log = Logger.getLogger( Counter.class );
046
047 /** Parameter name for setting the name. Value is <tt>{@value}</tt>. */
048 public static final String PARAM_NAME = "name";
049 /** Parameter name for setting the increment. Value is <tt>{@value}</tt>. */
050 public static final String PARAM_INCREMENT = "increment";
051 /** Parameter name for setting the showResult. Value is <tt>{@value}</tt>. */
052 public static final String PARAM_SHOW_RESULT = "showResult";
053 /** Parameter name for setting the start. Value is <tt>{@value}</tt>. */
054 public static final String PARAM_START = "start";
055 public static final String DEFAULT_NAME = "counter";
056 private static final int DEFAULT_INCREMENT = 1;
057 private static final boolean DEFAULT_SHOW_RESULT = true;
058
059 /**
060 * {@inheritDoc}
061 */
062 public String execute( WikiContext context, Map<String, String> params )
063 throws PluginException
064 {
065 //
066 // First, determine which kind of name we use to store in
067 // the WikiContext.
068 //
069 String countername = params.get( PARAM_NAME);
070
071 if( countername == null )
072 {
073 countername = DEFAULT_NAME;
074 }
075 else
076 {
077 countername = DEFAULT_NAME+"-"+countername;
078 }
079
080 //
081 // Fetch the old value
082 //
083 Integer val = (Integer)context.getVariable( countername );
084
085 if( val == null )
086 {
087 val = 0;
088 }
089
090 //
091 // Check if we need to reset this
092 //
093
094 String start = params.get( PARAM_START );
095
096 if( start != null )
097 {
098 val = Integer.parseInt( start );
099 }
100 else
101 {
102 //
103 // Determine how much to increment
104 //
105 String incrementObj = params.get( PARAM_INCREMENT );
106
107 int increment = DEFAULT_INCREMENT;
108
109 if (incrementObj != null)
110 {
111 increment = ( Integer.valueOf( incrementObj ) ).intValue();
112 }
113
114 val = val + increment;
115 }
116
117 context.setVariable( countername, val );
118
119 //
120 // check if we want to hide the result (just count, don't show result on the page
121 //
122 String showObj = params.get(PARAM_SHOW_RESULT);
123
124 boolean show = DEFAULT_SHOW_RESULT;
125
126 if( showObj != null )
127 {
128 show = TextUtil.isPositive( showObj );
129 }
130
131 if( show )
132 {
133 return val.toString();
134 }
135
136 return "";
137 }
138
139 }