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 */ 019 package org.apache.wiki.tags; 020 021 import java.io.IOException; 022 023 import org.apache.wiki.WikiSession; 024 import org.apache.wiki.util.TextUtil; 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 messsages will be cleared. Otherwise, this tag will always 030 * print the set of current messages as either a single <p> 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 suppled, 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 */ 040 public class MessagesTag extends WikiTagBase 041 { 042 private static final long serialVersionUID = 0L; 043 044 private String m_action = null; 045 046 private String m_prefix = ""; 047 048 private String m_topic = null; 049 050 private String m_div = "information"; 051 052 private static final String CLEAR = "clear"; 053 054 public void initTag() 055 { 056 super.initTag(); 057 m_action = m_topic = null; 058 m_prefix = ""; 059 m_div = "information"; 060 } 061 062 063 public void setTopic( String topic ) 064 { 065 m_topic = topic; 066 } 067 068 public void setPrefix( String prefix ) 069 { 070 m_prefix = prefix; 071 } 072 073 public void setDiv( String div ) 074 { 075 m_div = div; 076 } 077 078 public void setAction( String action ) 079 { 080 m_action = action.toLowerCase(); 081 } 082 083 public final int doWikiStartTag() throws IOException 084 { 085 WikiSession session = m_wikiContext.getWikiSession(); 086 if ( CLEAR.equals( m_action ) ) 087 { 088 if ( m_topic == null ) 089 { 090 session.clearMessages(); 091 } 092 else 093 { 094 session.clearMessages( m_topic ); 095 } 096 } 097 else 098 { 099 String[] messages = ( m_topic == null ) ? session.getMessages() : session.getMessages( m_topic ); 100 if ( messages.length > 0 ) 101 { 102 StringBuffer sb = new StringBuffer(); 103 if ( messages.length == 1 ) 104 { 105 sb.append( "<div class=\"" + m_div + "\">" + m_prefix + TextUtil.replaceEntities(messages[0]) + "</div>" ); 106 } 107 else 108 { 109 sb.append( "<div class=\"" + m_div + "\">" + m_prefix ); 110 sb.append( "<ul>" ); 111 for( int i = 0; i < messages.length; i++ ) 112 { 113 sb.append( "<li>" + TextUtil.replaceEntities(messages[i]) + "</li>" ); 114 } 115 sb.append( "</ul></div>" ); 116 } 117 pageContext.getOut().println( sb.toString() ); 118 } 119 } 120 return SKIP_BODY; 121 } 122 }