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 org.apache.wiki.WikiEngine;
024import org.apache.wiki.WikiPage;
025import org.apache.wiki.i18n.InternationalizationManager;
026import org.apache.wiki.parser.MarkupParser;
027import org.apache.wiki.parser.WikiDocument;
028import org.apache.wiki.preferences.Preferences;
029import org.apache.wiki.render.RenderingManager;
030import org.apache.wiki.util.TextUtil;
031
032/**
033 *  Writes the author name of the current page, including a link to that page,
034 *  if that page exists.
035 *
036 *  @since 2.0
037 */
038public class AuthorTag
039    extends WikiTagBase
040{
041    private static final long serialVersionUID = 0L;
042
043
044    public String m_format = "";
045
046    public void setFormat( String format )
047    {
048        m_format = format;  //empty or "plain"
049    }
050
051    /**
052     *  {@inheritDoc}
053     */
054    @Override
055    public final int doWikiStartTag()
056        throws IOException
057    {
058        WikiEngine engine = m_wikiContext.getEngine();
059        WikiPage   page   = m_wikiContext.getPage();
060
061        String author = page.getAuthor();
062
063        if( author != null && author.length() > 0 )
064        {
065            author = TextUtil.replaceEntities(author);
066
067            if( engine.pageExists(author) && !( "plain".equalsIgnoreCase( m_format ) ) )
068            {
069                // FIXME: It's very boring to have to do this.
070                //        Slow, too.
071
072                RenderingManager mgr = engine.getRenderingManager();
073
074                MarkupParser p = mgr.getParser( m_wikiContext, "["+author+"|"+author+"]" );
075
076                WikiDocument d = p.parse();
077
078                author = mgr.getHTML( m_wikiContext, d );
079            }
080
081            pageContext.getOut().print( author );
082        }
083        else
084        {
085            pageContext.getOut().print( Preferences.getBundle( m_wikiContext, InternationalizationManager.CORE_BUNDLE )
086                                                   .getString( "common.unknownauthor" ) );
087        }
088
089        return SKIP_BODY;
090    }
091}