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