KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > ErrorMessages


1 package org.sapia.validator;
2
3 import java.util.Locale JavaDoc;
4
5 /**
6  * Holds a hierarchy of error messages (organized by "locale").
7  *
8  * @author Yanick Duchesne
9  * <dl>
10  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
11  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
12  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
13  * </dl>
14  */

15 public class ErrorMessages {
16   private Hierarchy _root = new Hierarchy(null);
17
18   /**
19    * Constructor for ErrorMessages.
20    */

21   public ErrorMessages() {
22   }
23
24   /**
25    * Adds an <code>ErrorMessage</code> to this instance.
26    *
27    * @param msg an <code>ErrorMessage</code>.
28    */

29   public void addErrorMessage(ErrorMessage msg) {
30     _root.bind(msg);
31   }
32
33   /**
34    * Returns the <code>ErrorMessage<code> corresponding to the given locale,
35    * represented by a path.
36    *
37    * @return an <code>ErrorMessage</code>.
38    *
39    * @param path a locale, represented by a path of the form:
40    * <code>language/country/variant</code>.
41    */

42   public ErrorMessage getErrorMessageFor(String JavaDoc path) {
43     return _root.reverseLookup(path);
44   }
45
46   /**
47    * Returns the <code>ErrorMessage<code> corresponding to the given locale,
48    * represented by a path.
49    *
50    * @return an <code>ErrorMessage</code>.
51    *
52    * @param localel a <code>Locale</code>.
53    */

54   public ErrorMessage getErrorMessageFor(Locale JavaDoc locale) {
55     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
56
57     if ((locale.getLanguage() != null) && (locale.getLanguage().length() > 0)) {
58       buf.append(locale.getLanguage());
59     }
60
61     if ((locale.getCountry() != null) && (locale.getCountry().length() > 0)) {
62       buf.append('/').append(locale.getCountry());
63     }
64
65     if ((locale.getVariant() != null) && (locale.getVariant().length() > 0)) {
66       buf.append('/').append(locale.getVariant());
67     }
68     return getErrorMessageFor(buf.toString());
69   }
70 }
71
Popular Tags