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.tags;
020
021import org.apache.wiki.api.core.Page;
022import org.apache.wiki.preferences.Preferences;
023import org.apache.wiki.preferences.Preferences.TimeFormat;
024
025import java.io.IOException;
026import java.text.SimpleDateFormat;
027import java.util.Date;
028
029
030/**
031 * Writes the modification date of the page, formatted
032 * as specified in the attribute "format".
033 * <UL>
034 * <LI>format = A string describing which format you want to use.
035 * This is exactly like in "java.text.SimpleDateFormat".
036 * </UL>
037 *
038 * @since 2.0
039 */
040
041// FIXME: Should also take the current user TimeZone into account.
042
043public class PageDateTag extends WikiTagBase {
044
045    private static final long serialVersionUID = 0L;
046
047    public static final String DEFAULT_FORMAT = "dd-MMM-yyyy HH:mm:ss zzz";
048
049    private String m_format;
050
051    @Override
052    public void initTag() {
053        super.initTag();
054        m_format = null;
055    }
056
057    public String getFormat() {
058        return m_format;
059    }
060
061    public void setFormat( final String arg ) {
062        m_format = arg;
063    }
064
065    @Override
066    public final int doWikiStartTag() throws IOException {
067        final Page page = m_wikiContext.getPage();
068        if( page != null ) {
069            final Date d = page.getLastModified();
070            //  Date may be null if the page does not exist.
071            if( d != null ) {
072                final SimpleDateFormat fmt;
073                if( m_format == null ) {
074                    fmt = Preferences.getDateFormat( m_wikiContext, TimeFormat.DATETIME );
075                } else {
076                    fmt = new SimpleDateFormat( m_format );
077                }
078
079                pageContext.getOut().write( fmt.format( d ) );
080            } else {
081                pageContext.getOut().write( "&lt;never&gt;" );
082            }
083        }
084        return SKIP_BODY;
085    }
086
087}