KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > i18n > MessagesChain


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.i18n;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.apache.commons.collections.IteratorUtils;
22
23
24 /**
25  * Chains messages
26  * @author Philipp Bracher
27  * @version $Revision: 6341 $ ($Author: philipp $)
28  */

29 public class MessagesChain extends AbstractMessagesImpl {
30
31     /**
32      * The chain
33      */

34     private List JavaDoc chain = new ArrayList JavaDoc();
35
36     /**
37      * Create a chain passing the wrapped head of the chain
38      */

39     public MessagesChain(Messages head) {
40         super(head.getBasename(), head.getLocale());
41         chain.add(head);
42     }
43
44     /**
45      * Append messages to the chain
46      * @param messages
47      * @return the chain itself
48      */

49     public Messages chain(Messages messages) {
50         chain.add(messages);
51         return this;
52     }
53
54     /**
55      * Get the string searching in the chain
56      */

57     public String JavaDoc get(String JavaDoc key) {
58         for (Iterator JavaDoc iter = chain.iterator(); iter.hasNext();) {
59             Messages msgs = (Messages) iter.next();
60             String JavaDoc str = msgs.get(key);
61             if (!str.startsWith("???")) {
62                 return str;
63             }
64         }
65         return "???" + key + "???";
66     }
67
68     /**
69      * Return all keys contained in this chain
70      */

71     public Iterator JavaDoc keys() {
72         Set JavaDoc keys = new HashSet JavaDoc();
73         for (Iterator JavaDoc iter = chain.iterator(); iter.hasNext();) {
74             Messages msgs = (Messages) iter.next();
75             List JavaDoc current = IteratorUtils.toList(msgs.keys());
76             keys.addAll(current);
77         }
78         return keys.iterator();
79     }
80
81     /**
82      * Reload the chain
83      */

84     public void reload() throws Exception JavaDoc {
85         for (Iterator JavaDoc iter = chain.iterator(); iter.hasNext();) {
86             Messages msgs = (Messages) iter.next();
87             msgs.reload();
88         }
89     }
90 }
91
Popular Tags