Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.util

File MessageManager.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

2
33
7
1
192
95
13
0.39
4.71
7
1.86

Classes

Class Line # Actions
MessageManager 39 33 13
0.904761990.5%
 

Contributing tests

This file is covered by 237 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.util;
22   
23    import java.text.MessageFormat;
24    import java.util.HashSet;
25    import java.util.Locale;
26    import java.util.ResourceBundle;
27    import java.util.ResourceBundle.Control;
28    import java.util.Set;
29   
30    import org.apache.log4j.Logger;
31   
32    /**
33    *
34    * @author David Roldan Martinez
35    * @author Thomas Abeel
36    *
37    *
38    */
 
39    public class MessageManager
40    {
41    private static ResourceBundle rb;
42   
43    // BH 2018 switched to org.apache.llog4j.Logger
44    private static Logger log = Logger
45    .getLogger(MessageManager.class.getCanonicalName());
46   
47    private static Locale loc;
48   
49    private static Set<String> reportedMissing = new HashSet<>();
50   
 
51  19 toggle static
52    {
53  19 try
54    {
55    /* Localize Java dialogs */
56  19 loc = Locale.getDefault();
57    // Locale.setDefault(loc);
58    /* Getting messages for GV */
59  19 log.info("Getting messages for lang: " + loc);
60  19 Control control = Control.getControl(Control.FORMAT_PROPERTIES);
61  19 rb = ResourceBundle.getBundle("lang.Messages", loc, control);
62    // if (log.isLoggable(Level.FINEST))
63    // {
64    // // this might take a while, so we only do it if it will be shown
65    // log.info("Language keys: " + rb.keySet()); // was FINEST
66    // }
67    } catch (Exception q)
68    {
69  0 log.warn("Exception when initting Locale for i18n messages\n"
70    + q.getMessage());
71  0 q.printStackTrace();
72    } catch (Error v)
73    {
74  0 log.warn("Error when initting Locale for i18n messages\n"
75    + v.getMessage());
76  0 v.printStackTrace();
77    }
78   
79    }
80   
81    /**
82    * Returns the resource bundle text for the given key, or if not found, the
83    * key prefixed by "[missing key]"
84    *
85    * @param key
86    * @return
87    */
 
88  79558 toggle public static String getString(String key)
89    {
90  79559 String value = "[missing key] " + key;
91  79555 try
92    {
93  79555 value = rb.getString(key);
94    } catch (Exception e)
95    {
96  1 String msg = "I18N missing: " + loc + "\t" + key;
97  1 logWarning(key, msg);
98    }
99  79557 return value;
100    }
101   
 
102  90 toggle public static Locale getLocale()
103    {
104  90 return loc;
105    }
106   
107    /**
108    * Returns the resource bundle text for the given key, with tokens {@code {0},
109    * {1} etc replaced by the supplied parameters. If the key is not found,
110    * returns the key and values prefixed by "[missing key]"
111    *
112    * @param key
113    *
114    * @return
115    */
 
116  33549 toggle public static String formatMessage(String key, Object... params)
117    {
118  33549 try
119    {
120  33549 return MessageFormat.format(rb.getString(key), params);
121    } catch (Exception e)
122    {
123  1 log.warn("I18N missing: " + loc + "\t" + key);
124    }
125  1 String value = "[missing key] " + key + "";
126  1 for (Object p : params)
127    {
128  2 value += " '" + p.toString() + "'";
129    }
130  1 return value;
131    }
132   
133    /**
134    * Returns the resource bundle text for the given key, with tokens {@code {0},
135    * {1} etc replaced by the supplied parameters. If the key is not found,
136    * returns the key and values prefixed by "[missing key]"
137    *
138    * @param key
139    *
140    * @return
141    */
 
142  22541 toggle public static String formatMessage(String key, String[] params)
143    {
144  22541 return formatMessage(key, (Object[]) params);
145    }
146   
147    /**
148    * Returns resource bundle text given a root and a human-readable(ish) name
149    * that when combined might resolve to an i18n string. {@code name} is forced
150    * to lower case, with any spaces removed, and concatenated to {@code keyroot}
151    * to form a lookup key.
152    * <p>
153    * If the key doesn't resolve, then {@code name} is returned.
154    * <p>
155    * Use this for programmatically constructed keys that might have a human
156    * readable alternative used in the program (e.g. BLOSUM62 and
157    * label.score_blosum62).
158    *
159    * @param keyroot
160    * @param name
161    * @return
162    */
 
163  3938 toggle public static String getStringOrReturn(String keyroot, String name)
164    {
165  3938 String smkey = keyroot + name.toLowerCase().replaceAll(" ", "");
166  3938 try
167    {
168  3938 name = rb.getString(smkey);
169    } catch (Exception x)
170    {
171  71 String msg = "I18N missing key with root " + keyroot + ": " + loc + "\t"
172    + smkey;
173  71 logWarning(smkey, msg);
174    }
175  3938 return name;
176    }
177   
178    /**
179    * Logs missing keys (each key once only per run)
180    *
181    * @param key
182    * @param msg
183    */
 
184  72 toggle private static void logWarning(String key, String msg)
185    {
186  72 if (!reportedMissing.contains(key))
187    {
188  4 reportedMissing.add(key);
189  4 log.info(msg);
190    }
191    }
192    }