blob: d7208888d18db87e36976dc69d3882cb4faca5f0 [file] [log] [blame]
Simon Hunt0c85f112017-06-12 21:02:17 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onosproject.ui.lion;
19
20import java.util.ResourceBundle;
21
22/**
23 * Utility methods for dealing with Localization Bundles etc.
24 */
25public final class LionUtils {
26
27 private static final String DOT = ".";
28
29 // no instantiation
30 private LionUtils() {
31 }
32
33 /**
34 * This method takes a fully qualified name and returns a
35 * {@link ResourceBundle} which is loaded from a properties file with
36 * that base name.
37 * <p>
38 * For example, supposing the jar file contains:
39 * <p>
40 * <pre>
41 * org/onosproject/util/example/SomeBundle.properties
42 * </pre>
43 * <p>
44 * Then, to correctly load the resource bundle associated with
45 * <code>SomeBundle</code>, call:
46 * <pre>
47 * String fqname = "org.onosproject.util.example.SomeBundle";
48 * ResourceBundle res = ResourceUtils.getBundledResource(fqname);
49 * </pre>
50 * <p>
51 * Note that no error is thrown if the properties file does not exist.
52 * This condition will not become apparent until you try and access
53 * a property from the bundle, at which time a
54 * {@link java.util.MissingResourceException} will be thrown.
55 *
56 * @param basename the (fully qualified) basename of the bundle properties
57 * file
58 * @return the associated resource bundle
59 */
60 public static ResourceBundle getBundledResource(String basename) {
61 return ResourceBundle.getBundle(basename);
62 }
63
64 /**
65 * This method takes a class and returns a {@link ResourceBundle} which is
66 * loaded from a properties file with the same base name as the class.
67 * Note that both the class and the properties file(s) need to be in
68 * the same jar file.
69 * <p>
70 * For example, supposing the jar file contains:
71 * <p>
72 * <pre>
73 * org/onosproject/util/example/SomeObject.class
74 * org/onosproject/util/example/SomeObject.properties
75 * </pre>
76 * <p>
77 * Then, to correctly load the resource bundle associated with
78 * <code>SomeObject</code>, call:
79 * <pre>
80 * ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class);
81 * </pre>
82 * <p>
83 * Note that no error is thrown if the properties file does not exist.
84 * This condition will not become apparent until you try and access
85 * a property from the bundle, at which time a
86 * {@link java.util.MissingResourceException} will be thrown.
87 *
88 * @param c the class
89 * @return the associated resource bundle
90 */
91 public static ResourceBundle getBundledResource(Class<?> c) {
92 return ResourceBundle.getBundle(c.getName());
93 }
94
95 /**
96 * This method returns a {@link ResourceBundle} which is loaded from
97 * a properties file with the specified base name from the same package
98 * as the specified class.
99 * Note that both the class and the properties file(s) need to be in
100 * the same jar file.
101 * <p>
102 * For example, supposing the jar file contains:
103 * <p>
104 * <pre>
105 * org/onosproject/util/example/SomeObject.class
106 * org/onosproject/util/example/DisplayStrings.properties
107 * </pre>
108 * <p>
109 * Then, to correctly load the resource bundle call:
110 * <pre>
111 * ResourceBundle res = ResourceUtils.getBundledResource(SomeObject.class,
112 * "DisplayStrings");
113 * </pre>
114 * <p>
115 * Note that no error is thrown if the properties file does not exist.
116 * This condition will not become apparent until you try and access
117 * a property from the bundle, at which time a
118 * {@link java.util.MissingResourceException} will be thrown.
119 *
120 * @param c the class requesting the bundle
121 * @param baseName the base name of the resource bundle
122 * @return the associated resource bundle
123 */
124 public static ResourceBundle getBundledResource(Class<?> c, String baseName) {
125 String className = c.getName();
126 StringBuilder sb = new StringBuilder();
127 int dot = className.lastIndexOf(DOT);
128 sb.append(className.substring(0, dot));
129 sb.append(DOT).append(baseName);
130 return ResourceBundle.getBundle(sb.toString());
131 }
132}