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.Engine; 023import org.apache.wiki.api.core.Page; 024import org.apache.wiki.api.exceptions.ProviderException; 025import org.apache.wiki.pages.PageManager; 026import org.apache.wiki.render.RenderingManager; 027 028import javax.servlet.jsp.JspWriter; 029import java.io.IOException; 030 031/** 032 * Renders WikiPage content. For InsertPage tag and the InsertPage plugin 033 * the difference is that the tag will always render in the context of the page 034 * which is referenced (i.e. a LeftMenu inserted on a JSP page with the InsertPage tag 035 * will always render in the context of the actual URL, e.g. Main.), whereas 036 * the InsertPage plugin always renders in local context. This allows this like 037 * ReferringPagesPlugin to really refer to the Main page instead of having to 038 * resort to any trickery. 039 * <p> 040 * This tag sets the "realPage" field of the WikiContext to point at the inserted 041 * page, while the "page" will contain the actual page in which the rendering 042 * is being made. 043 * 044 * <P><B>Attributes</B></P> 045 * <UL> 046 * <LI>page - Page name to refer to. Default is the current page. 047 * <li>mode - In which format to insert the page. Can be either "plain" or "html". 048 * </UL> 049 * 050 * @since 2.0 051 */ 052public class InsertPageTag extends WikiTagBase { 053 054 private static final long serialVersionUID = 0L; 055 private static final Logger log = Logger.getLogger( InsertPageTag.class ); 056 057 public static final int HTML = 0; 058 public static final int PLAIN = 1; 059 060 protected String m_pageName = null; 061 private int m_mode = HTML; 062 063 @Override 064 public void initTag() { 065 super.initTag(); 066 m_pageName = null; 067 m_mode = HTML; 068 } 069 070 public void setPage( final String page ) 071 { 072 m_pageName = page; 073 } 074 075 public String getPage() 076 { 077 return m_pageName; 078 } 079 080 public void setMode( final String arg ) { 081 if( "plain".equals( arg ) ) { 082 m_mode = PLAIN; 083 } else { 084 m_mode = HTML; 085 } 086 } 087 088 @Override 089 public final int doWikiStartTag() throws IOException, ProviderException { 090 final Engine engine = m_wikiContext.getEngine(); 091 final Page insertedPage; 092 093 // 094 // NB: The page might not really exist if the user is currently 095 // creating it (i.e. it is not yet in the cache or providers), 096 // AND we got the page from the wikiContext. 097 // 098 099 if( m_pageName == null ) { 100 insertedPage = m_wikiContext.getPage(); 101 if( !engine.getManager( PageManager.class ).wikiPageExists(insertedPage) ) return SKIP_BODY; 102 } else { 103 insertedPage = engine.getManager( PageManager.class ).getPage( m_pageName ); 104 } 105 106 if( insertedPage != null ) { 107 // FIXME: Do version setting later. 108 // page.setVersion( WikiProvider.LATEST_VERSION ); 109 110 log.debug("Inserting page "+insertedPage); 111 112 final JspWriter out = pageContext.getOut(); 113 final Page oldPage = m_wikiContext.setRealPage( insertedPage ); 114 115 switch( m_mode ) { 116 case HTML: out.print( engine.getManager( RenderingManager.class ).getHTML( m_wikiContext, insertedPage ) ); break; 117 case PLAIN: out.print( engine.getManager( PageManager.class ).getText( insertedPage ) ); break; 118 } 119 120 m_wikiContext.setRealPage( oldPage ); 121 } 122 123 return SKIP_BODY; 124 } 125 126}