    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.Locale;
    031import java.util.Map;
    032import java.util.ResourceBundle;
    033
    034/**
    035 * Just displays the current date and time. The time format is exactly like in
    036 * the java.text.SimpleDateFormat class.
    037 *
    038 * <p>
    039 * Parameters : </p>
    040 * NONE
    041 *
    042 * @since 1.7.8
    043 * @see java.text.SimpleDateFormat
    044 */
    045public class CurrentTimePlugin implements Plugin {
    046
    047    @Override
    048    public String getDisplayName(Locale locale) {
    049        final ResourceBundle rb = ResourceBundle.getBundle(PluginManager.PLUGIN_I18N_RESOURCE, locale);
    050        return rb.getString(this.getClass().getSimpleName());
    051
    052    }
    053
    054    @Override
    055    public String getSnipExample() {
    056        return "CurrentTimePlugin format='yyyy mm-dd'";
    057    }
    058
    059    /**
    060     * {@inheritDoc}
    061     */
    062    @Override
    063    public String execute(final Context context, final Map< String, String> params) throws PluginException {
    064        final String formatString = params.get("format");
    065
    066        try {
    067            final SimpleDateFormat fmt;
    068            if (formatString != null) {
    069                fmt = new SimpleDateFormat(formatString);
    070            } else {
    071                fmt = Preferences.getDateFormat(context, TimeFormat.DATETIME);
    072            }
    073
    074            final Date d = new Date();  // Now.
    075
    076            return TextUtil.replaceEntities(fmt.format(d));
    077
    078        } catch (final IllegalArgumentException e) {
    079            final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    080            throw new PluginException(rb.getString("currenttimeplugin.badformat") + e.getMessage());
    081        }
    082    }
    083
    084}