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