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.WikiContext;
026import org.apache.wiki.WikiPage;
027import org.apache.wiki.attachment.Attachment;
028
029/**
030 *  Writes a link to a Wiki page.  Body of the link becomes the actual text.
031 *  The link is written regardless to whether the page exists or not.
032 *
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
045    extends WikiLinkTag
046{
047    private static final long serialVersionUID = 0L;
048
049    private String m_version = null;
050    public String m_title = "";
051    public String m_accesskey = "";
052
053    public void initTag()
054    {
055        super.initTag();
056        m_version = null;
057    }
058
059    public String getVersion()
060    {
061        return m_version;
062    }
063
064    public void setVersion( String arg )
065    {
066        m_version = arg;
067    }
068
069    public void setTitle( String title )
070    {
071        m_title = title;
072    }
073
074    public void setAccesskey( String access )
075    {
076        m_accesskey = access;
077    }
078
079
080    public int doWikiStartTag()
081        throws IOException
082    {
083        String     pageName = m_pageName;
084        boolean    isattachment = false;
085
086        if( m_pageName == null )
087        {
088            WikiPage p = m_wikiContext.getPage();
089
090            if( p != null )
091            {
092                pageName = p.getName();
093
094                isattachment = p instanceof Attachment;
095            }
096            else
097            {
098                return SKIP_BODY;
099            }
100        }
101
102        JspWriter out = pageContext.getOut();
103        String url;
104        String linkclass;
105        String forceDownload = "";
106
107        if( isattachment )
108        {
109            url = m_wikiContext.getURL(WikiContext.ATTACH, pageName,
110                                       (getVersion() != null) ? "version="+getVersion() : null );
111            linkclass = "attachment";
112
113            if( m_wikiContext.getEngine().getAttachmentManager().forceDownload( pageName ) )
114            {
115                forceDownload = "download ";
116            }
117
118        }
119        else
120        {
121            StringBuilder params = new StringBuilder();
122            if( getVersion() != null ) params.append( "version="+getVersion() );
123            if( getTemplate() != null ) params.append( (params.length()>0?"&amp;":"") + "skin="+getTemplate() );
124
125            url = m_wikiContext.getURL( WikiContext.VIEW, pageName,
126                                        params.toString() );
127            linkclass = "wikipage";
128        }
129
130        switch( m_format )
131        {
132          case ANCHOR:
133            out.print("<a class=\""+linkclass+"\" href=\""+url+"\" accesskey=\""
134                          + m_accesskey + "\" title=\"" + m_title
135                          + "\" " + forceDownload + ">");
136            break;
137          case URL:
138            out.print( url );
139            break;
140        }
141
142        return EVAL_BODY_INCLUDE;
143    }
144
145}