    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.Attachment;
    022import org.apache.wiki.api.core.ContextEnum;
    023import org.apache.wiki.api.core.Page;
    024import org.apache.wiki.attachment.AttachmentManager;
    025
    026import javax.servlet.jsp.JspWriter;
    027import java.io.IOException;
    028
    029
    030/**
    031 * Writes a link to a Wiki page.  Body of the link becomes the actual text.
    032 * The link is written regardless to whether the page exists or not.
    033 * <P><B>Attributes</B></P>
    034 * <UL>
    035 * <LI>page - Page name to refer to.  Default is the current page.
    036 * <LI>format - either "anchor" or "url" to output either an <A>... or just the HREF part of one.
    037 * <LI>template - Which template should we link to.
    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 LinkToTag extends WikiLinkTag {
    045
    046    private static final long serialVersionUID = 0L;
    047
    048    private 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 String getVersion() {
    059        return m_version;
    060    }
    061
    062    public void setVersion( final String arg ) {
    063        m_version = arg;
    064    }
    065
    066    public void setTitle( final String title ) {
    067        m_title = title;
    068    }
    069
    070    public void setAccesskey( final String access ) {
    071        m_accesskey = access;
    072    }
    073
    074    @Override
    075    public int doWikiStartTag() throws IOException {
    076        String pageName = m_pageName;
    077        boolean isattachment = false;
    078
    079        if( m_pageName == null ) {
    080            final Page p = m_wikiContext.getPage();
    081
    082            if( p != null ) {
    083                pageName = p.getName();
    084
    085                isattachment = p instanceof Attachment;
    086            } else {
    087                return SKIP_BODY;
    088            }
    089        }
    090
    091        final JspWriter out = pageContext.getOut();
    092        final String url;
    093        final String linkclass;
    094        String forceDownload = "";
    095
    096        if( isattachment ) {
    097            url = m_wikiContext.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), pageName, ( getVersion() != null ) ? "version=" + getVersion() : null );
    098            linkclass = "attachment";
    099
    100            if( m_wikiContext.getEngine().getManager( AttachmentManager.class ).forceDownload( pageName ) ) {
    101                forceDownload = "download ";
    102            }
    103
    104        } else {
    105            final StringBuilder params = new StringBuilder();
    106            if( getVersion() != null ) {
    107                params.append( "version=" ).append( getVersion() );
    108            }
    109            if( getTemplate() != null ) {
    110                params.append( params.length() > 0 ? "&amp;" : "" ).append( "skin=" ).append( getTemplate() );
    111            }
    112
    113            url = m_wikiContext.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), pageName, params.toString() );
    114            linkclass = "wikipage";
    115        }
    116
    117        switch( m_format ) {
    118        case ANCHOR:
    119            out.print( "<a class=\"" + linkclass +
    120                       "\" href=\"" + url +
    121                       "\" accesskey=\"" + m_accesskey +
    122                       "\" title=\"" + m_title + "\" " + forceDownload + ">" );
    123            break;
    124        case URL:
    125            out.print( url );
    126            break;
    127        }
    128
    129        return EVAL_BODY_INCLUDE;
    130    }
    131
    132}