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