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.preferences.Preferences; 025import org.apache.wiki.preferences.Preferences.TimeFormat; 026 027import java.text.SimpleDateFormat; 028import java.util.Date; 029import java.util.Map; 030import java.util.ResourceBundle; 031 032/** 033 * Just displays the current date and time. 034 * The time format is exactly like in the java.text.SimpleDateFormat class. 035 * 036 * <p>Parameters : </p> 037 * NONE 038 * @since 1.7.8 039 * @see java.text.SimpleDateFormat 040 */ 041public class CurrentTimePlugin implements Plugin { 042 043 // private static Logger log = Logger.getLogger( CurrentTimePlugin.class ); 044 045 /** 046 * {@inheritDoc} 047 */ 048 @Override 049 public String execute( final Context context, final Map< String, String > params ) throws PluginException { 050 final String formatString = params.get( "format" ); 051 052 try { 053 final SimpleDateFormat fmt; 054 if( formatString != null ) { 055 fmt = new SimpleDateFormat( formatString ); 056 } else { 057 fmt = Preferences.getDateFormat( context, TimeFormat.DATETIME ); 058 } 059 060 final Date d = new Date(); // Now. 061 062 return fmt.format( d ); 063 } catch( final IllegalArgumentException e ) { 064 final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE ); 065 throw new PluginException( rb.getString( "currenttimeplugin.badformat" ) + e.getMessage() ); 066 } 067 } 068 069}