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