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;
022import org.apache.logging.log4j.LogManager;
023import org.apache.logging.log4j.Logger;
024
025/**
026 *  Root class for different internal wiki links.  Cannot be used directly,
027 *  but provides basic stuff for other classes.
028 *  <P>
029 *  Extend from this class if you need the following attributes.
030 *
031 *  <P><B>Attributes</B></P>
032 *  <UL>
033 *    <LI>page - Page name to refer to.  Default is the current page.
034 *    <li>format - Either "url" or "anchor".  If "url", will provide
035 *  just the URL for the link.  If "anchor", will output proper HTML
036 *  (&lt;a&gt; href="...).
037 *  </UL>
038 *
039 *  @since 2.0
040 */
041public abstract class WikiLinkTag extends WikiTagBase {
042    private static final Logger LOG = LogManager.getLogger( WikiLinkTag.class );
043    private static final long serialVersionUID = 4130732879352134867L;
044    public static final int   ANCHOR = 0;
045    public static final int   URL    = 1;
046
047    protected String m_pageName;
048    protected int    m_format = ANCHOR;
049    protected String m_template;
050
051    
052    @Override
053    public void initTag()
054    {
055        super.initTag();
056        m_pageName = m_template = null;
057        m_format = ANCHOR;
058    }
059    
060    public void setPage(final String page )
061    {
062        m_pageName = page;
063    }
064
065    public String getPage()
066    {
067        return m_pageName;
068    }
069
070
071    public String getTemplate()
072    {
073        return m_template;
074    }
075
076    public void setTemplate(final String arg )
077    {
078        m_template = arg;
079    }
080
081    public void setFormat(final String mode )
082    {
083        if( "url".equalsIgnoreCase(mode) )
084        {
085            m_format = URL;
086        }
087        else
088        {
089            m_format = ANCHOR;
090        }
091    }
092
093    @Override
094    public int doEndTag()
095    {
096        try
097        {
098            if( m_format == ANCHOR )
099            {
100                pageContext.getOut().print("</a>");
101            }
102        }
103        catch( final IOException e )
104        {
105            LOG.warn(e.getMessage(), e);
106        }
107
108        return EVAL_PAGE;
109    }
110}