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 */
040public class PageDateTag extends WikiTagBase {
041
042    private static final long serialVersionUID = 0L;
043
044    public static final String DEFAULT_FORMAT = "dd-MMM-yyyy HH:mm:ss zzz";
045
046    private String m_format;
047
048    @Override
049    public void initTag() {
050        super.initTag();
051        m_format = null;
052    }
053
054    public String getFormat() {
055        return m_format;
056    }
057
058    public void setFormat( final String arg ) {
059        m_format = arg;
060    }
061
062    @Override
063    public final int doWikiStartTag() throws IOException {
064        final Page page = m_wikiContext.getPage();
065        if( page != null ) {
066            final Date d = page.getLastModified();
067            //  Date may be null if the page does not exist.
068            if( d != null ) {
069                final SimpleDateFormat fmt;
070                if( m_format == null ) {
071                    fmt = Preferences.getDateFormat( m_wikiContext, TimeFormat.DATETIME );
072                } else {
073                    fmt = new SimpleDateFormat( m_format );
074                }
075
076                pageContext.getOut().write( fmt.format( d ) );
077            } else {
078                pageContext.getOut().write( "&lt;never&gt;" );
079            }
080        }
081        return SKIP_BODY;
082    }
083
084}