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.HashMap; 023 import java.util.Map; 024 025 import javax.servlet.ServletException; 026 import javax.servlet.jsp.JspException; 027 028 import org.apache.log4j.Logger; 029 import org.apache.wiki.WikiContext; 030 import org.apache.wiki.api.exceptions.ProviderException; 031 032 033 /** 034 * Is used as a "super include" tag, which can include the proper context 035 * based on the wikicontext. 036 * 037 * @since 2.2 038 */ 039 public class ContentTag extends WikiTagBase { 040 041 private static final long serialVersionUID = 0L; 042 private static final Logger log = Logger.getLogger( ContentTag.class ); 043 044 private Map<String, String> m_mappings = new HashMap<String, String>(); 045 046 /** 047 * Set the template for the VIEW context. 048 * 049 * @param s The template name. 050 */ 051 public void setView( String s ) 052 { 053 m_mappings.put( WikiContext.VIEW, s ); 054 } 055 056 /** 057 * Set the template for the DIFF context. 058 * 059 * @param s The template name. 060 */ 061 public void setDiff( String s ) 062 { 063 m_mappings.put( WikiContext.DIFF, s ); 064 } 065 066 /** 067 * Set the template for the INFO context. 068 * 069 * @param s The template name. 070 */ 071 public void setInfo( String s ) 072 { 073 m_mappings.put( WikiContext.INFO, s ); 074 } 075 076 /** 077 * Set the template for the PREVIEW context. 078 * 079 * @param s The template name. 080 */ 081 public void setPreview( String s ) 082 { 083 m_mappings.put( WikiContext.PREVIEW, s ); 084 } 085 086 /** 087 * Set the template for the CONFLICT context. 088 * 089 * @param s The template name. 090 */ 091 public void setConflict( String s ) 092 { 093 m_mappings.put( WikiContext.CONFLICT, s ); 094 } 095 096 /** 097 * Set the template for the FIND context. 098 * 099 * @param s The template name. 100 */ 101 public void setFind( String s ) 102 { 103 m_mappings.put( WikiContext.FIND, s ); 104 } 105 106 /** 107 * Set the template for the PREFS context. 108 * 109 * @param s The template name. 110 */ 111 public void setPrefs( String s ) 112 { 113 m_mappings.put( WikiContext.PREFS, s ); 114 } 115 116 /** 117 * Set the template for the ERROR context. 118 * 119 * @param s The template name. 120 */ 121 public void setError( String s ) 122 { 123 m_mappings.put( WikiContext.ERROR, s ); 124 } 125 126 /** 127 * Set the template for the EDIT context. 128 * 129 * @param s The template name. 130 */ 131 public void setEdit( String s ) 132 { 133 m_mappings.put( WikiContext.EDIT, s ); 134 } 135 136 /** 137 * Set the template for the COMMENT context. 138 * 139 * @param s The template name. 140 */ 141 public void setComment( String s ) 142 { 143 m_mappings.put( WikiContext.COMMENT, s ); 144 } 145 146 /** 147 * {@inheritDoc} 148 */ 149 public final int doWikiStartTag() 150 throws IOException, 151 ProviderException 152 { 153 return SKIP_BODY; 154 } 155 156 /** 157 * {@inheritDoc} 158 */ 159 public final int doEndTag() 160 throws JspException 161 { 162 try 163 { 164 // Check the overridden templates first 165 String requestContext = m_wikiContext.getRequestContext(); 166 String contentTemplate = m_mappings.get( requestContext ); 167 168 // If not found, use the defaults 169 if ( contentTemplate == null ) 170 { 171 contentTemplate = m_wikiContext.getContentTemplate(); 172 } 173 174 // If still no, something fishy is going on 175 if( contentTemplate == null ) 176 { 177 throw new JspException("This template uses <wiki:Content/> in an unsupported context: " + requestContext ); 178 } 179 180 String page = m_wikiContext.getEngine().getTemplateManager().findJSP( pageContext, 181 m_wikiContext.getTemplate(), 182 contentTemplate ); 183 pageContext.include( page ); 184 } 185 catch( ServletException e ) 186 { 187 log.warn( "Including failed, got a servlet exception from sub-page. "+ 188 "Rethrowing the exception to the JSP engine.", e ); 189 throw new JspException( e.getMessage() ); 190 } 191 catch( IOException e ) 192 { 193 log.warn( "I/O exception - probably the connection was broken. "+ 194 "Rethrowing the exception to the JSP engine.", e ); 195 throw new JspException( e.getMessage() ); 196 } 197 198 return EVAL_PAGE; 199 } 200 }