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    public void initTag() {
052        super.initTag();
053        m_format = null;
054    }
055
056    public String getFormat() {
057        return m_format;
058    }
059
060    public void setFormat( final String arg ) {
061        m_format = arg;
062    }
063
064    public final int doWikiStartTag() throws IOException {
065        final Page page = m_wikiContext.getPage();
066        if( page != null ) {
067            final Date d = page.getLastModified();
068            //  Date may be null if the page does not exist.
069            if( d != null ) {
070                final SimpleDateFormat fmt;
071                if( m_format == null ) {
072                    fmt = Preferences.getDateFormat( m_wikiContext, TimeFormat.DATETIME );
073                } else {
074                    fmt = new SimpleDateFormat( m_format );
075                }
076
077                pageContext.getOut().write( fmt.format( d ) );
078            } else {
079                pageContext.getOut().write( "&lt;never&gt;" );
080            }
081        }
082        return SKIP_BODY;
083    }
084
085}