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.io.IOException; 022import java.util.List; 023 024import javax.servlet.jsp.JspWriter; 025import javax.servlet.jsp.PageContext; 026 027import org.apache.log4j.Logger; 028import org.apache.wiki.WikiContext; 029import org.apache.wiki.WikiEngine; 030import org.apache.wiki.WikiPage; 031import org.apache.wiki.api.exceptions.ProviderException; 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 044// FIXME: Too much in common with IteratorTag - REFACTOR 045public class HistoryIteratorTag 046 extends IteratorTag 047{ 048 private static final long serialVersionUID = 0L; 049 050 static Logger log = Logger.getLogger( HistoryIteratorTag.class ); 051 052 @Override 053 public final int doStartTag() { 054 m_wikiContext = (WikiContext) pageContext.getAttribute( WikiTagBase.ATTR_CONTEXT, PageContext.REQUEST_SCOPE ); 055 056 WikiEngine engine = m_wikiContext.getEngine(); 057 WikiPage page; 058 059 page = m_wikiContext.getPage(); 060 061 try 062 { 063 if( page != null && engine.pageExists(page) ) 064 { 065 @SuppressWarnings("unchecked") 066 List< WikiPage > versions = ( List< WikiPage > )engine.getVersionHistory( page.getName() ); 067 068 if( versions == null ) 069 { 070 // There is no history 071 return SKIP_BODY; 072 } 073 074 m_iterator = versions.iterator(); 075 076 if( m_iterator.hasNext() ) 077 { 078 WikiContext context = (WikiContext)m_wikiContext.clone(); 079 context.setPage( (WikiPage)m_iterator.next() ); 080 pageContext.setAttribute( WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE ); 081 pageContext.setAttribute( getId(), context.getPage() ); 082 } 083 else 084 { 085 return SKIP_BODY; 086 } 087 } 088 089 return EVAL_BODY_BUFFERED; 090 } 091 catch( ProviderException e ) 092 { 093 log.fatal("Provider failed while trying to iterator through history",e); 094 // FIXME: THrow something. 095 } 096 097 return SKIP_BODY; 098 } 099 100 @Override 101 public final int doAfterBody() 102 { 103 if( bodyContent != null ) 104 { 105 try 106 { 107 JspWriter out = getPreviousOut(); 108 out.print(bodyContent.getString()); 109 bodyContent.clearBody(); 110 } 111 catch( IOException e ) 112 { 113 log.error("Unable to get inner tag text", e); 114 // FIXME: throw something? 115 } 116 } 117 118 if( m_iterator != null && m_iterator.hasNext() ) 119 { 120 WikiContext context = (WikiContext)m_wikiContext.clone(); 121 context.setPage( (WikiPage)m_iterator.next() ); 122 pageContext.setAttribute( WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE ); 123 pageContext.setAttribute( getId(), context.getPage() ); 124 return EVAL_BODY_BUFFERED; 125 } 126 127 return SKIP_BODY; 128 } 129}