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 java.util.Date;
022import java.text.SimpleDateFormat;
023import java.io.IOException;
024
025import org.apache.wiki.WikiPage;
026import org.apache.wiki.preferences.Preferences;
027import org.apache.wiki.preferences.Preferences.TimeFormat;
028
029/**
030 *  Writes the modification date of the page, formatted
031 *  as specified in the attribute "format".
032 *
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
044    extends WikiTagBase
045{
046    private static final long serialVersionUID = 0L;
047    
048    public static final String DEFAULT_FORMAT = "dd-MMM-yyyy HH:mm:ss zzz";
049
050    private String m_format = null;
051
052    public void initTag()
053    {
054        super.initTag();
055        m_format = null;
056    }
057
058    public String getFormat()
059    {
060        return m_format;
061    }
062
063    public void setFormat( String arg )
064    {
065        m_format = arg;
066    }
067
068    public final int doWikiStartTag()
069        throws IOException
070    {
071        WikiPage   page   = m_wikiContext.getPage();
072
073        if( page != null )
074        {
075            Date d = page.getLastModified();
076
077            //
078            //  Date may be null if the page does not exist.
079            //
080            if( d != null )
081            {
082                SimpleDateFormat fmt;
083                
084                if( m_format == null )
085                    fmt = Preferences.getDateFormat( m_wikiContext, TimeFormat.DATETIME );
086                else
087                    fmt = new SimpleDateFormat( m_format );
088
089                pageContext.getOut().write( fmt.format( d ) );
090            }
091            else
092            {
093                pageContext.getOut().write( "&lt;never&gt;" );
094            }
095        }
096
097        return SKIP_BODY;
098    }
099}