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; 032import org.apache.wiki.attachment.Attachment; 033import org.apache.wiki.attachment.AttachmentManager; 034 035/** 036 * Iterates through the list of attachments one has. 037 * 038 * <P><B>Attributes</B></P> 039 * <UL> 040 * <LI>page - Page name to refer to. Default is the current page. 041 * </UL> 042 * 043 * @since 2.0 044 */ 045 046// FIXME: Too much in common with IteratorTag - REFACTOR 047public class AttachmentsIteratorTag 048 extends IteratorTag 049{ 050 private static final long serialVersionUID = 0L; 051 052 static Logger log = Logger.getLogger( AttachmentsIteratorTag.class ); 053 054 /** 055 * {@inheritDoc} 056 */ 057 @Override 058 public final int doStartTag() 059 { 060 m_wikiContext = (WikiContext) pageContext.getAttribute( WikiTagBase.ATTR_CONTEXT, 061 PageContext.REQUEST_SCOPE ); 062 063 WikiEngine engine = m_wikiContext.getEngine(); 064 AttachmentManager mgr = engine.getAttachmentManager(); 065 WikiPage page; 066 067 page = m_wikiContext.getPage(); 068 069 if( !mgr.attachmentsEnabled() ) 070 { 071 return SKIP_BODY; 072 } 073 074 try 075 { 076 if( page != null && engine.pageExists(page) ) 077 { 078 List< Attachment > atts = mgr.listAttachments( page ); 079 080 if( atts == null ) 081 { 082 log.debug("No attachments to display."); 083 // There are no attachments included 084 return SKIP_BODY; 085 } 086 087 m_iterator = atts.iterator(); 088 089 if( m_iterator.hasNext() ) 090 { 091 Attachment att = (Attachment) m_iterator.next(); 092 093 WikiContext context = (WikiContext)m_wikiContext.clone(); 094 context.setPage( att ); 095 pageContext.setAttribute( WikiTagBase.ATTR_CONTEXT, 096 context, 097 PageContext.REQUEST_SCOPE ); 098 099 pageContext.setAttribute( getId(), att ); 100 } 101 else 102 { 103 return SKIP_BODY; 104 } 105 } 106 else 107 { 108 return SKIP_BODY; 109 } 110 111 return EVAL_BODY_BUFFERED; 112 } 113 catch( ProviderException e ) 114 { 115 log.fatal("Provider failed while trying to iterator through history",e); 116 // FIXME: THrow something. 117 } 118 119 return SKIP_BODY; 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 @Override 126 public final int doAfterBody() 127 { 128 if( bodyContent != null ) 129 { 130 try 131 { 132 JspWriter out = getPreviousOut(); 133 out.print(bodyContent.getString()); 134 bodyContent.clearBody(); 135 } 136 catch( IOException e ) 137 { 138 log.error("Unable to get inner tag text", e); 139 // FIXME: throw something? 140 } 141 } 142 143 if( m_iterator != null && m_iterator.hasNext() ) 144 { 145 Attachment att = (Attachment) m_iterator.next(); 146 147 WikiContext context = (WikiContext)m_wikiContext.clone(); 148 context.setPage( att ); 149 pageContext.setAttribute( WikiTagBase.ATTR_CONTEXT, 150 context, 151 PageContext.REQUEST_SCOPE ); 152 153 pageContext.setAttribute( getId(), att ); 154 155 return EVAL_BODY_BUFFERED; 156 } 157 158 return SKIP_BODY; 159 } 160}