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.text.SimpleDateFormat;
022    import java.util.Date;
023    import java.util.Map;
024    import java.util.ResourceBundle;
025    
026    import org.apache.wiki.WikiContext;
027    import org.apache.wiki.api.exceptions.PluginException;
028    import org.apache.wiki.api.plugin.WikiPlugin;
029    import org.apache.wiki.preferences.Preferences;
030    import org.apache.wiki.preferences.Preferences.TimeFormat;
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     */
041    public class CurrentTimePlugin implements WikiPlugin
042    {
043        // private static Logger log = Logger.getLogger( CurrentTimePlugin.class );
044    
045        /**
046         *  {@inheritDoc}
047         */
048        public String execute( WikiContext context, Map<String, String> params )
049            throws PluginException
050        {
051            String formatString = params.get("format");
052            
053            try
054            {
055                SimpleDateFormat fmt;
056                
057                if( formatString != null )
058                {
059                    fmt = new SimpleDateFormat( formatString );
060                }
061                else
062                {
063                    fmt = Preferences.getDateFormat( context, TimeFormat.DATETIME );
064                }
065    
066                Date d = new Date();  // Now.
067    
068                return fmt.format( d );
069            }
070            catch( IllegalArgumentException e )
071            {
072                ResourceBundle rb = Preferences.getBundle( context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE );
073                
074                throw new PluginException( rb.getString("currenttimeplugin.badformat") + e.getMessage() );
075            }
076        }
077    
078    }