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