juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamesbog...@apache.org
Subject [2/2] juneau git commit: Minor API changes.
Date Sun, 03 Dec 2017 17:15:23 GMT
Minor API changes.

Project: http://git-wip-us.apache.org/repos/asf/juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/juneau/commit/9b8b457f
Tree: http://git-wip-us.apache.org/repos/asf/juneau/tree/9b8b457f
Diff: http://git-wip-us.apache.org/repos/asf/juneau/diff/9b8b457f

Branch: refs/heads/master
Commit: 9b8b457fd1e504330836a56043670b5f90e48e40
Parents: 9c7f3a4
Author: JamesBognar <jamesbognar@apache.org>
Authored: Sun Dec 3 12:15:18 2017 -0500
Committer: JamesBognar <jamesbognar@apache.org>
Committed: Sun Dec 3 12:15:18 2017 -0500

----------------------------------------------------------------------
 .../org/apache/juneau/utils/MessageBundle.java  | 57 ++++++++++++++++++++
 1 file changed, 57 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/juneau/blob/9b8b457f/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MessageBundle.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MessageBundle.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MessageBundle.java
index 84bb9cb..304f8a8 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MessageBundle.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/utils/MessageBundle.java
@@ -84,6 +84,63 @@ public class MessageBundle extends ResourceBundle {
 
 	/**
 	 * Constructor.
+	 * 
+	 * <p>
+	 * When this method is used, the bundle path is determined by searching for the resource
bundle
+	 * in the following locations:
+	 * <ul>
+	 * 	<li><code>[package].ForClass.properties</code>
+	 * 	<li><code>[package].nls.ForClass.properties</code>
+	 * 	<li><code>[package].i18n.ForClass.properties</code>
+	 * </ul>
+	 * 
+	 * @param forClass The class 
+	 * @return A new message bundle belonging to the class.
+	 */
+	public static final MessageBundle create(Class<?> forClass) {
+		return create(forClass, findBundlePath(forClass));
+	}
+	
+	/**
+	 * Constructor.
+	 * 
+	 * <p>
+	 * A shortcut for calling <code>new MessageBundle(forClass, bundlePath)</code>.
+	 * 
+	 * 
+	 * @param forClass The class 
+	 * @param bundlePath The location of the resource bundle.
+	 * @return A new message bundle belonging to the class.
+	 */
+	public static final MessageBundle create(Class<?> forClass, String bundlePath) {
+		return new MessageBundle(forClass, bundlePath);
+	}
+	
+	private static final String findBundlePath(Class<?> forClass) {
+		String path = forClass.getName();
+		if (tryBundlePath(forClass, path))
+			return path;
+		path = forClass.getPackage().getName() + ".nls." + forClass.getSimpleName(); 
+		if (tryBundlePath(forClass, path))
+			return path;
+		path = forClass.getPackage().getName() + ".i18n." + forClass.getSimpleName();
+		if (tryBundlePath(forClass, path))
+			return path;
+		return null;
+	}
+	
+	private static final boolean tryBundlePath(Class<?> c, String path) {
+		try {
+			path = c.getName();
+			ResourceBundle.getBundle(path, Locale.getDefault(), c.getClassLoader());
+			return true;
+		} catch (MissingResourceException e) {
+			return false;
+		}
+	}
+
+	/**
+	 * Constructor.
 	 *
 	 * @param forClass The class using this resource bundle.
 	 * @param bundlePath


Mime
View raw message