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.InternalWikiException;
022import org.apache.wiki.api.core.ContextEnum;
023import org.apache.wiki.api.core.Page;
024
025import javax.servlet.jsp.JspWriter;
026import java.io.IOException;
027
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() throws IOException {
049        final Page page;
050        final String pageName;
051        
052        //  Determine the page and the link.
053        if( m_pageName == null ) {
054            page = m_wikiContext.getPage();
055            if( page == null ) {
056                // You can't call this on the page itself anyways.
057                return SKIP_BODY;
058            }
059            pageName = page.getName();
060        } else {
061            pageName = m_pageName;
062        }
063
064        //  Finally, print out the correct link, according to what user commanded.
065        final JspWriter out = pageContext.getOut();
066        switch( m_format ) {
067        case ANCHOR: out.print( "<a href=\"" + getCommentURL( pageName ) + "\">" ); break;
068        case URL: out.print( getCommentURL( pageName ) ); break;
069        default: throw new InternalWikiException( "Impossible format " + m_format );
070        }
071
072        return EVAL_BODY_INCLUDE;
073    }
074
075    private String getCommentURL( final String pageName ) {
076        return m_wikiContext.getURL( ContextEnum.PAGE_COMMENT.getRequestContext(), pageName );
077    }
078
079}