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