KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > EngineMessages


1 package org.jahia.engines;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import javax.servlet.ServletRequest JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12
13 /**
14  * <p>Title: Container for EngineMessage objects</p> <p>Description: Inspired by Struts
15  * ActionMessages, but more JavaBean compliant so that it can work with JSTL and better with
16  * Jahia's localization classes. </p> <p>Copyright: Copyright (c) 2002</p> <p>Company: Jahia
17  * Ltd</p>
18  *
19  * @author Serge Huber
20  * @version 1.0
21  */

22
23 public class EngineMessages {
24
25     public static final String JavaDoc GLOBAL_MESSAGE = "org.jahia.engines.global_message";
26     public static final String JavaDoc CONTEXT_KEY = "engineMessages";
27
28     Map JavaDoc messages = new HashMap JavaDoc ();
29
30     public EngineMessages () {
31     }
32
33     public void add (EngineMessage message) {
34         add (GLOBAL_MESSAGE, message);
35     }
36
37     public void add (String JavaDoc property,
38                      EngineMessage message) {
39         ArrayList JavaDoc propertyList = (ArrayList JavaDoc) messages.get (property);
40         if (propertyList == null) {
41             propertyList = new ArrayList JavaDoc ();
42         }
43         propertyList.add (message);
44         messages.put (property, propertyList);
45     }
46
47     public Set JavaDoc getProperties () {
48         return messages.keySet ();
49     }
50
51     public Set JavaDoc getEntrySet () {
52         return messages.entrySet ();
53     }
54
55     public int getSize () {
56         Iterator JavaDoc propertyIter = getProperties ().iterator ();
57         int size = 0;
58         while (propertyIter.hasNext ()) {
59             String JavaDoc curPropertyName = (String JavaDoc) propertyIter.next ();
60             ArrayList JavaDoc curPropertyList = (ArrayList JavaDoc) messages.get (curPropertyName);
61             size += curPropertyList.size ();
62         }
63         return size;
64     }
65
66     public List JavaDoc getMessages () {
67         ArrayList JavaDoc fullList = new ArrayList JavaDoc ();
68         Iterator JavaDoc propertyIter = getProperties ().iterator ();
69         while (propertyIter.hasNext ()) {
70             String JavaDoc curPropertyName = (String JavaDoc) propertyIter.next ();
71             ArrayList JavaDoc curPropertyList = (ArrayList JavaDoc) messages.get (curPropertyName);
72             fullList.addAll (curPropertyList);
73         }
74         return fullList;
75     }
76
77     public List JavaDoc getMessages (String JavaDoc property) {
78         ArrayList JavaDoc propertyList = (ArrayList JavaDoc) messages.get (property);
79         return propertyList;
80     }
81
82     public int getSize (String JavaDoc property) {
83         ArrayList JavaDoc propertyList = (ArrayList JavaDoc) messages.get (property);
84         if (propertyList != null) {
85             return propertyList.size ();
86         } else {
87             return 0;
88         }
89     }
90
91     public boolean isEmpty () {
92         return messages.isEmpty ();
93     }
94
95     /**
96      * save message as contextPrefix + CONTEXT_KEY attribute
97      *
98      * @param contextPrefix String
99      * @param request ServletRequest
100      */

101     public void saveMessages (String JavaDoc contextPrefix, ServletRequest JavaDoc request) {
102         request.setAttribute (contextPrefix + CONTEXT_KEY , this);
103     }
104
105     public void saveMessages (ServletRequest JavaDoc request) {
106         request.setAttribute (CONTEXT_KEY, this);
107     }
108
109     public void saveMessages (PageContext JavaDoc pageContext) {
110         pageContext.setAttribute (CONTEXT_KEY, this);
111     }
112
113     public void saveMessages (PageContext JavaDoc pageContext, int scope) {
114         pageContext.setAttribute (CONTEXT_KEY, this, scope);
115     }
116
117 }
118
Popular Tags