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 java.io.IOException;
022
023import javax.servlet.jsp.JspWriter;
024
025import org.apache.wiki.InternalWikiException;
026import org.apache.wiki.WikiContext;
027import org.apache.wiki.WikiPage;
028
029/**
030 *  Writes a comment 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 *  </UL>
036 *
037 *  @since 2.0
038 */
039public class CommentLinkTag
040    extends WikiLinkTag
041{
042    private static final long serialVersionUID = 0L;
043    
044    /**
045     *  {@inheritDoc}
046     */
047    @Override
048    public final int doWikiStartTag()
049        throws IOException
050    {
051        WikiPage   page     = null;
052        String     pageName = null;
053        
054        //
055        //  Determine the page and the link.
056        //
057        if( m_pageName == null )
058        {
059            page = m_wikiContext.getPage();
060            if( page == null )
061            {
062                // You can't call this on the page itself anyways.
063                return SKIP_BODY;
064            }
065
066            pageName = page.getName();
067        }
068        else
069        {
070            pageName = m_pageName;
071        }
072
073        //
074        //  Finally, print out the correct link, according to what
075        //  user commanded.
076        //
077        JspWriter out = pageContext.getOut();
078
079        switch( m_format )
080        {
081            case ANCHOR:
082                out.print("<a href=\""+getCommentURL(pageName)+"\">");
083                break;
084
085            case URL:
086                out.print( getCommentURL(pageName) );
087                break;
088                
089            default:
090                throw new InternalWikiException("Impossible format "+m_format);
091            
092        }
093
094        return EVAL_BODY_INCLUDE;
095    }
096
097    private String getCommentURL( String pageName )
098    {
099        return m_wikiContext.getURL(WikiContext.COMMENT, pageName);
100    }
101
102}