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 java.text.MessageFormat;
023import java.util.ResourceBundle;
024
025import javax.servlet.jsp.JspWriter;
026
027import org.apache.wiki.WikiContext;
028import org.apache.wiki.WikiEngine;
029import org.apache.wiki.i18n.InternationalizationManager;
030import org.apache.wiki.preferences.Preferences;
031import org.apache.wiki.rss.RSSGenerator;
032
033/**
034 *  Writes an image link to a JSPWiki RSS file.  If RSS generation has
035 *  been turned off in jspwiki.properties, returns an empty string.
036 *
037 *  @since 2.0
038 */
039public class RSSImageLinkTag
040    extends WikiTagBase
041{
042    private static final long serialVersionUID = 0L;
043
044    protected String m_title;
045    private   String m_mode;
046    private   String m_pageName;
047
048    /**
049     *  {@inheritDoc}
050     */
051    @Override
052    public void initTag()
053    {
054        super.initTag();
055        m_title = null;
056        m_mode  = RSSGenerator.MODE_FULL;
057        m_pageName = null;
058    }
059
060    /**
061     *  Sets the title for the link.  If not defined, no title is shown.
062     *
063     *  @param title A string for the title.
064     */
065    public void setTitle( String title )
066    {
067        m_title = title;
068    }
069
070    public void setMode( String mode )
071    {
072        m_mode = mode;
073    }
074
075
076    /**
077     *  Returns the title.
078     *
079     *  @return The title.
080     */
081    public String getTitle()
082    {
083        return m_title;
084    }
085
086    /**
087     *  {@inheritDoc}
088     */
089    @Override
090    public final int doWikiStartTag()
091        throws IOException
092    {
093        WikiEngine engine = m_wikiContext.getEngine();
094        JspWriter out = pageContext.getOut();
095        ResourceBundle rb = Preferences.getBundle( m_wikiContext, InternationalizationManager.CORE_BUNDLE );
096
097        if( engine.getRSSGenerator() != null && engine.getRSSGenerator().isEnabled() )
098        {
099            if( RSSGenerator.MODE_FULL.equals(m_mode) )
100            {
101                String rssURL = engine.getGlobalRSSURL();
102
103                if( rssURL != null )
104                {
105                    out.print("<a class=\"feed\" href=\""+rssURL+"\">&nbsp;</a>");
106                }
107            }
108            else
109            {
110                String page = m_pageName != null ? m_pageName : m_wikiContext.getPage().getName();
111
112                String params = "page="+page+"&mode="+m_mode;
113                out.print( "<a href='"+m_wikiContext.getURL( WikiContext.NONE, "rss.jsp", params ));
114                out.print( "' class='feed'" );
115                out.print( " title='"+MessageFormat.format( rb.getString( "rss.title" ), page )+"'>" );
116                out.print( "&nbsp;</a> ");
117            }
118        }
119
120        return SKIP_BODY;
121    }
122}