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.Session;
022import org.apache.wiki.util.TextUtil;
023
024import java.io.IOException;
025
026/**
027 * Returns or clears the current messages associated with the user's wiki
028 * session. This tag accepts four attributes: <ul> <li><code>action</code> -
029 * if "clear", the messages will be cleared. Otherwise, this tag will always
030 * print the set of current messages as either a single &lt;p&gt; tag (if there
031 * is only one message) or a bulleted list (if there is more than one).</li>
032 * <li><code>prefix</code> - the string to prepend to the list of errors, if
033 * there are any; default is empty string</li> <li><code>topic</code> - a
034 * collection for messages, for example those associated with a particular web
035 * form. If not supplied, defaults to a generic (non-specific) collection</li>
036 * <li><code>div</code> - the <code>div</code> class to wrap the
037 * messages in; if not supplied, <code>information</code> is assumed</li></ul>
038 * @since 2.3.54
039 */
040public class MessagesTag extends WikiTagBase {
041
042    private static final long   serialVersionUID = 0L;
043
044    private String              m_action;
045    private String              m_prefix         = "";
046    private String              m_topic;
047    private String              m_div            = "information";
048
049    private static final String CLEAR            = "clear";
050
051    @Override
052    public void initTag() {
053        super.initTag();
054        m_action = m_topic = null;
055        m_prefix = "";
056        m_div = "information";
057    }
058
059    public void setTopic( final String topic ) {
060        m_topic = topic;
061    }
062
063    public void setPrefix( final String prefix ) {
064        m_prefix = prefix;
065    }
066
067    public void setDiv( final String div ) {
068        m_div = div;
069    }
070
071    public void setAction( final String action ) {
072        m_action = action.toLowerCase();
073    }
074
075    @Override
076    public final int doWikiStartTag() throws IOException {
077        final Session session = m_wikiContext.getWikiSession();
078        if( CLEAR.equals( m_action ) ) {
079            if( m_topic == null ) {
080                session.clearMessages();
081            } else {
082                session.clearMessages( m_topic );
083            }
084        } else {
085            final String[] messages = ( m_topic == null ) ? session.getMessages() : session.getMessages( m_topic );
086            if( messages.length > 0 ) {
087                final StringBuilder sb = new StringBuilder();
088                if( messages.length == 1 ) {
089                    sb.append( "<div class=\"" ).append( m_div ).append( "\">" )
090                      .append( m_prefix )
091                      .append( TextUtil.replaceEntities( messages[ 0 ] ) )
092                      .append( "</div>" );
093                } else {
094                    sb.append( "<div class=\"" ).append( m_div ).append( "\">" ).append( m_prefix );
095                    sb.append( "<ul>" );
096                    for( final String message : messages ) {
097                        sb.append( "<li>" ).append( TextUtil.replaceEntities( message ) ).append( "</li>" );
098                    }
099                    sb.append( "</ul></div>" );
100                }
101                pageContext.getOut().println( sb.toString() );
102            }
103        }
104        return SKIP_BODY;
105    }
106
107}