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