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.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.core.Engine;
025import org.apache.wiki.api.core.Page;
026import org.apache.wiki.api.exceptions.ProviderException;
027import org.apache.wiki.pages.PageManager;
028
029import javax.servlet.jsp.JspWriter;
030import javax.servlet.jsp.PageContext;
031import java.io.IOException;
032import java.util.List;
033
034/**
035 *  Iterates through tags.
036 *
037 *  <P><B>Attributes</B></P>
038 *  <UL>
039 *    <LI>page - Page name to refer to.  Default is the current page.
040 *  </UL>
041 *
042 *  @since 2.0
043 */
044// FIXME: Too much in common with IteratorTag - REFACTOR
045public class HistoryIteratorTag extends IteratorTag  {
046
047    private static final long serialVersionUID = 0L;
048    private static final Logger LOG = LogManager.getLogger( HistoryIteratorTag.class );
049
050    /** {@inheritDoc} */
051    @Override
052    public final int doStartTag() {
053        m_wikiContext = (Context) pageContext.getAttribute( Context.ATTR_CONTEXT, PageContext.REQUEST_SCOPE );
054        final Engine engine = m_wikiContext.getEngine();
055        final Page page = m_wikiContext.getPage();
056
057        try {
058            if( page != null && engine.getManager( PageManager.class ).wikiPageExists( page ) ) {
059                final List< Page > versions = engine.getManager( PageManager.class ).getVersionHistory( page.getName() );
060
061                if( versions == null ) {
062                    // There is no history
063                    return SKIP_BODY;
064                }
065
066                m_iterator = versions.iterator();
067
068                if( m_iterator.hasNext() ) {
069                    final Context context = m_wikiContext.clone();
070                    context.setPage( ( Page )m_iterator.next() );
071                    pageContext.setAttribute( Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE );
072                    pageContext.setAttribute( getId(), context.getPage() );
073                } else {
074                    return SKIP_BODY;
075                }
076            }
077
078            return EVAL_BODY_BUFFERED;
079        } catch( final ProviderException e ) {
080            LOG.fatal("Provider failed while trying to iterator through history",e);
081            // FIXME: THrow something.
082        }
083
084        return SKIP_BODY;
085    }
086
087    /** {@inheritDoc} */
088    @Override
089    public final int doAfterBody() {
090        if( bodyContent != null ) {
091            try {
092                final JspWriter out = getPreviousOut();
093                out.print(bodyContent.getString());
094                bodyContent.clearBody();
095            } catch( final IOException e ) {
096                LOG.error("Unable to get inner tag text", e);
097                // FIXME: throw something?
098            }
099        }
100
101        if( m_iterator != null && m_iterator.hasNext() ) {
102            final Context context = m_wikiContext.clone();
103            context.setPage( ( Page )m_iterator.next() );
104            pageContext.setAttribute( Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE );
105            pageContext.setAttribute( getId(), context.getPage() );
106            return EVAL_BODY_BUFFERED;
107        }
108
109        return SKIP_BODY;
110    }
111
112}