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.wiki.api.core.ContextEnum;
022import org.apache.wiki.api.core.Engine;
023import org.apache.wiki.api.core.Page;
024import org.apache.wiki.pages.PageManager;
025
026import javax.servlet.jsp.JspWriter;
027import java.io.IOException;
028
029/**
030 *  Writes an edit link.  Body of the link becomes the link text.
031 *  <P><B>Attributes</B></P>
032 *  <UL>
033 *    <LI>page - Page name to refer to.  Default is the current page.
034 *    <LI>format - Format, either "anchor" or "url".
035 *    <LI>version - Version number of the page to refer to.  Possible values
036 *        are "this", meaning the version of the current page; or a version
037 *        number.  Default is always to point at the latest version of the page.
038 *    <LI>title - Is used in page actions to display hover text (tooltip)
039 *    <LI>accesskey - Set an accesskey (ALT+[Char])
040 *  </UL>
041 *
042 *  @since 2.0
043 */
044public class EditLinkTag extends WikiLinkTag {
045
046    private static final long serialVersionUID = 0L;
047    
048    public String m_version;
049    public String m_title = "";
050    public String m_accesskey = "";
051    
052    @Override
053    public void initTag() {
054        super.initTag();
055        m_version = null;
056    }
057
058    public void setVersion( final String vers )
059    {
060        m_version = vers;
061    }
062    
063    public void setTitle( final String title )
064    {
065        m_title = title;
066    }
067
068    public void setAccesskey( final String access )
069    {
070        m_accesskey = access;
071    }
072
073    @Override
074    public final int doWikiStartTag() throws IOException {
075        final Engine engine   = m_wikiContext.getEngine();
076        Page page = null;
077        String versionString = "";
078        final String pageName;
079        
080        //  Determine the page and the link.
081        if( m_pageName == null ) {
082            page = m_wikiContext.getPage();
083            if( page == null ) {
084                // You can't call this on the page itself anyways.
085                return SKIP_BODY;
086            }
087
088            pageName = page.getName();
089        } else {
090            pageName = m_pageName;
091        }
092
093        //
094        //  Determine the latest version, if the version attribute is "this".
095        //
096        if( m_version != null ) {
097            if( "this".equalsIgnoreCase( m_version ) ) {
098                if( page == null ) {
099                    // No page, so go fetch according to page name.
100                    page = engine.getManager( PageManager.class ).getPage( m_pageName );
101                }
102
103                if( page != null ) {
104                    versionString = "version=" + page.getVersion();
105                }
106            } else {
107                versionString = "version=" + m_version;
108            }
109        }
110
111        //
112        //  Finally, print out the correct link, according to what user commanded.
113        //
114        final JspWriter out = pageContext.getOut();
115        switch( m_format ) {
116          case ANCHOR:
117            out.print( "<a href=\"" + m_wikiContext.getURL( ContextEnum.PAGE_EDIT.getRequestContext(), pageName, versionString ) +
118                       "\" accesskey=\"" + m_accesskey + "\" title=\"" + m_title + "\">" );
119            break;
120          case URL:
121            out.print( m_wikiContext.getURL( ContextEnum.PAGE_EDIT.getRequestContext(), pageName, versionString ) );
122            break;
123        }
124
125        return EVAL_BODY_INCLUDE;
126    }
127
128}