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