|
From: <cr...@us...> - 2008-10-03 12:59:19
|
Revision: 4599
http://jnode.svn.sourceforge.net/jnode/?rev=4599&view=rev
Author: crawley
Date: 2008-10-03 12:59:09 +0000 (Fri, 03 Oct 2008)
Log Message:
-----------
Tidying up the KeyboardInterpreterFactory API a bit
Modified Paths:
--------------
trunk/core/src/driver/org/jnode/driver/input/KeyboardInterpreterFactory.java
trunk/shell/src/shell/org/jnode/shell/command/LoadkeysCommand.java
Modified: trunk/core/src/driver/org/jnode/driver/input/KeyboardInterpreterFactory.java
===================================================================
--- trunk/core/src/driver/org/jnode/driver/input/KeyboardInterpreterFactory.java 2008-10-03 11:06:43 UTC (rev 4598)
+++ trunk/core/src/driver/org/jnode/driver/input/KeyboardInterpreterFactory.java 2008-10-03 12:59:09 UTC (rev 4599)
@@ -26,10 +26,14 @@
import org.apache.log4j.Logger;
/**
- * KeyboardInterpreterFactory.java
+ * The KeyboardInterpreterFactory class provides static methods for creating KeyboardInterpreter
+ * objects and forming keyboard layout identifiers.
+ * <p>
+ * This is an interim API. We intend to replace it with a JNode service that includes
+ * methods for managing the mapping of keyboard layout IDs to keyboard layout classes.
*
- * @author Created by Marc DENTY
- * @since 0.15
+ * @author Marc DENTY
+ * @author cr...@jn...
*/
public class KeyboardInterpreterFactory {
@@ -37,74 +41,107 @@
.getLogger(KeyboardInterpreterFactory.class);
/**
- * Method loadDefaultKeyboardInterpreter
+ * Load the default keyboard layout as specified in the 'org.jnode.driver.input.KeyboardLayout'
+ * resource bundle. If none is specified or the specified layout cannot be used, we use the
+ * 'US_en' layout as a fallback.
*
* @return a valid KeyboardInterpreter
- * @version 2/8/2004
*/
public static KeyboardInterpreter getDefaultKeyboardInterpreter() {
+ String country = null;
+ String region = null;
+ String variant = null;
try {
- ResourceBundle rb = null;
- String defaultCountry = null;
- String defaultRegion = null;
-
+ ResourceBundle rb = ResourceBundle.getBundle("org.jnode.driver.input.KeyboardLayout",
+ Locale.getDefault(), Thread.currentThread().getContextClassLoader());
+ country = getProperty(rb, "defaultCountry");
+ // FIXME the following property name should be 'defaultLanguage'
+ region = getProperty(rb, "defaultRegion");
+ variant = getProperty(rb, "defaultVariant");
+ } catch (Exception ex) {
+ log.error("Cannot find the 'org.jnode.driver.input.KeyboardLayout' resource bundle", ex);
+ }
+ if (country != null) {
try {
- rb = ResourceBundle.getBundle("org.jnode.driver.input.KeyboardLayout", Locale.getDefault(),
- Thread.currentThread().getContextClassLoader());
- defaultCountry = rb.getString("defaultCountry");
- if (defaultCountry.trim().length() == 0) {
- defaultCountry = null;
+ KeyboardInterpreter ki = createKeyboardInterpreter(country, region, variant);
+ if (ki != null) {
+ return ki;
}
} catch (Exception ex) {
- log.warn("Cannot load default keyboard layout, loading US layout instead", ex);
- return getKeyboardInterpreter("US", null, null);
+ log.error("Cannot load the default '" +
+ makeKeyboardInterpreterID(country, region, variant) +
+ "' keyboard interpreter", ex);
}
- try {
- defaultRegion = rb.getString("defaultRegion");
- if (defaultRegion.trim().length() == 0) {
- defaultRegion = null;
- }
- } catch (Exception e) {
- //todo empty?
- }
-
- KeyboardInterpreter ki = getKeyboardInterpreter(defaultCountry,
- defaultRegion, null);
- if (ki == null) {
- throw new NullPointerException("KeyboardInterpreter for "
- + defaultCountry + " not found");
- } else {
- return ki;
- }
- } catch (Exception e) {
- try {
- return getKeyboardInterpreter("US", null, null);
- } catch (Exception ex) {
- log.error("Cannot load US keyboard interpreter", ex);
- //FIXME : this should never happen
- return null;
- } catch (Error ex) {
- log.error("Cannot load US keyboard interpreter", ex);
- //FIXME : this should never happen
- return null;
- }
}
+ // Use the US_en keyboard layout as a fallback if there was no resource bundle, no
+ // usable default keyboard layout or the specified default layout had no interpreter.
+ log.error("Trying the 'US_en' keyboard interpreter as a fallback");
+ try {
+ return createKeyboardInterpreter("US", "en", null);
+ } catch (Throwable ex) {
+ log.error("Cannot load 'US_en' keyboard interpreter", ex);
+ }
+ // FIXME we should probably throw an exception ...
+ return null;
}
+
+ /**
+ * Get a String-valued property value from the resource bundle, dealing
+ * with empty and missing values.
+ *
+ * @param rb the resource bundle
+ * @param key the property name
+ * @return the property value or <code>null</null> if the value is missing or empty.
+ */
+ private static String getProperty(ResourceBundle rb, String key) {
+ try {
+ String res = rb.getString(key);
+ res = res.trim();
+ return (res.length() == 0) ? null : res;
+ } catch (RuntimeException e) { /* ClassCastException or MissingResourceException */
+ return null;
+ }
+ }
/**
- * Method getKeyboardInterpreter this method
+ * Create a new keyboard interpreter object. Note that keyboard interpreter
+ * objects are stateful and therefore cannot be shared by multiple keyboards.
*
- * @param country a String
- * @param language a String
- * @param variant
- * @return a KeyboardInterpreter
- * @version 2/8/2004
+ * @param country the country code; e.g. US, GB, FR, DE, etc.
+ * @param language the language code; e.g. en, fr, de etc or <code>null</code>
+ * @param variant a keyboard variant name or <code>null</code>.
+ * @return a KeyboardInterpreter or <code>null</code>
*/
- public static KeyboardInterpreter getKeyboardInterpreter(String country,
- String language, String variant)
- throws InstantiationException,
- IllegalAccessException {
+ public static KeyboardInterpreter createKeyboardInterpreter(
+ String country, String language, String variant) {
+ final String id = makeKeyboardInterpreterID(country, language, variant);
+ log.debug("Looking for interpreter for keyboard layout '" + id + "'");
+ final String classI10N = "org.jnode.driver.input.l10n.KeyboardInterpreter_" + id;
+ try {
+ final ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ return (KeyboardInterpreter) cl.loadClass(classI10N).newInstance();
+ } catch (ClassNotFoundException ex) {
+ log.error("No keyboard interpreter class found for layout id " + id +
+ ": expected class name '" + classI10N + "'.");
+ } catch (Exception ex) {
+ log.error("Error loading or instantiating keyboard interpreter class '" +
+ classI10N + "'.", ex);
+ }
+ return null;
+ }
+
+ /**
+ * Convert a country / language / variant keyboard triple into a keyboard
+ * layout identifier.
+ *
+ * @param country the country code; e.g. US, GB, FR, DE, etc.
+ * @param language the language code; e.g. en, fr, de etc or <code>null</code>.
+ * @param variant a keyboard variant name or <code>null</code>.
+ * @return the keyboard layout identifier.
+ */
+ public static String makeKeyboardInterpreterID(
+ String country, String language, String variant) {
final String id;
country = country.toUpperCase();
if (language != null) {
@@ -119,17 +156,6 @@
} else {
id = country;
}
-
- log.debug("Searching for " + id);
- final String classI10N = "org.jnode.driver.input.l10n.KeyboardInterpreter_" + id;
-
- try {
- final ClassLoader cl = Thread.currentThread().getContextClassLoader();
- return (KeyboardInterpreter) cl.loadClass(classI10N).newInstance();
- } catch (ClassNotFoundException e) {
- log.error("Keyboard interpreter for " + id + " not found.");
- }
-
- return null;
+ return id;
}
}
Modified: trunk/shell/src/shell/org/jnode/shell/command/LoadkeysCommand.java
===================================================================
--- trunk/shell/src/shell/org/jnode/shell/command/LoadkeysCommand.java 2008-10-03 11:06:43 UTC (rev 4598)
+++ trunk/shell/src/shell/org/jnode/shell/command/LoadkeysCommand.java 2008-10-03 12:59:09 UTC (rev 4599)
@@ -90,7 +90,7 @@
for (Device kb : kbDevs) {
final KeyboardAPI api = kb.getAPI(KeyboardAPI.class);
final KeyboardInterpreter kbInt =
- KeyboardInterpreterFactory.getKeyboardInterpreter(
+ KeyboardInterpreterFactory.createKeyboardInterpreter(
country, language, variant);
if (kbInt != null) {
out.println("Setting layout for keyboard " + kb.getId() + " to " +
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|