001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.jsp.tag.presentation;
016:
017: import java.io.Writer;
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import javax.servlet.jsp.JspException;
024: import org.apache.commons.lang.StringEscapeUtils;
025: import org.araneaframework.framework.MessageContext;
026: import org.araneaframework.jsp.tag.PresentationTag;
027: import org.araneaframework.jsp.util.JspUtil;
028:
029: /**
030: * Message tag - show the messages in {@link MessageContext}
031: * with given type.
032: *
033: * @author Taimo Peelo (taimo@araneaframework.org)
034: *
035: * @jsp.tag
036: * name = "messages"
037: * body-content = "empty"
038: */
039:
040: public class MessagesHtmlTag extends PresentationTag {
041: protected String type;
042: protected String divId;
043:
044: {
045: baseStyleClass = "aranea-messages";
046: }
047:
048: protected int doStartTag(Writer out) throws Exception {
049: super .doStartTag(out);
050: return SKIP_BODY;
051: }
052:
053: protected int doEndTag(Writer out) throws Exception {
054: super .doEndTag(out);
055:
056: MessageContext messageContext = (MessageContext) getEnvironment()
057: .requireEntry(MessageContext.class);
058: Map messageMap = messageContext.getMessages();
059:
060: List entries = new ArrayList();
061: if (messageMap != null) {
062: for (Iterator i = messageMap.entrySet().iterator(); i
063: .hasNext();) {
064: Map.Entry entry = (Map.Entry) i.next();
065: if (type == null
066: || ((String) entry.getKey()).equals(type)) {
067: entries.add(entry);
068: }
069: }
070: }
071:
072: /* matching messages, write them out */
073: writeMessagesStart(out, entries);
074: writeMessages(out, entries);
075: writeMessagesEnd(out, entries);
076:
077: return EVAL_PAGE;
078: }
079:
080: /**
081: * @since 1.1
082: */
083: protected void writeMessagesStart(Writer out, List entries)
084: throws Exception {
085: JspUtil.writeOpenStartTag(out, "div");
086: JspUtil.writeAttribute(out, "id", getDivId());
087: JspUtil.writeAttribute(out, "class", getStyleClass());
088: if (type != null) {
089: JspUtil.writeAttribute(out, "arn-msgs-type", type);
090: }
091: JspUtil.writeAttribute(out, "style",
092: entries.size() == 0 ? "display: none" : getStyle());
093: JspUtil.writeAttributes(out, attributes);
094: JspUtil.writeCloseStartTag(out);
095: }
096:
097: /**
098: * @since 1.1
099: */
100: protected void writeMessagesEnd(Writer out, List entries)
101: throws Exception {
102: JspUtil.writeEndTag(out, "div");
103: }
104:
105: /**
106: * @since 1.1
107: */
108: protected void writeMessages(Writer out, List entries)
109: throws Exception {
110: for (Iterator i = entries.iterator(); i.hasNext();) {
111: Collection messages = (Collection) ((Map.Entry) i.next())
112: .getValue();
113:
114: for (Iterator j = messages.iterator(); j.hasNext();) {
115: writeMessageBody(out, (String) j.next());
116: if (j.hasNext())
117: writeMessageSeparator(out);
118: }
119: if (i.hasNext())
120: writeMessageSeparator(out);
121: }
122: }
123:
124: /**
125: * @since 1.1
126: */
127: protected void writeMessageBody(Writer out, String message)
128: throws Exception {
129: out.write(StringEscapeUtils.escapeHtml(message));
130: }
131:
132: /**
133: * @since 1.1
134: */
135: protected void writeMessageSeparator(Writer out) throws Exception {
136: JspUtil.writeStartEndTag(out, "br");
137: }
138:
139: /** @since 1.1 */
140: protected String getDivId() {
141: return this .divId;
142: }
143:
144: /* ***********************************************************************************
145: * Tag attributes
146: * ***********************************************************************************/
147:
148: /**
149: * @jsp.attribute
150: * type = "java.lang.String"
151: * required = "false"
152: * description = "Type of messages to show."
153: */
154: public void setType(String type) throws JspException {
155: this .type = (String) evaluate("type", type, String.class);
156: }
157:
158: /**
159: * @jsp.attribute
160: * type = "java.lang.String"
161: * required = "false"
162: * description = "Sets the ID of the HTML <DIV> inside which the messages are rendered."
163: *
164: * @since 1.1
165: */
166: public void setDivId(String divId) throws JspException {
167: this .divId = (String) evaluate("divId", divId, String.class);
168: }
169: }
|