[jboss-svn-commits] JBoss Common SVN: r4252 - in jboss-i18n/trunk: src and 6 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Apr 9 14:57:38 EDT 2010


Author: david.lloyd at jboss.com
Date: 2010-04-09 14:57:38 -0400 (Fri, 09 Apr 2010)
New Revision: 4252

Added:
   jboss-i18n/trunk/pom.xml
   jboss-i18n/trunk/src/
   jboss-i18n/trunk/src/main/
   jboss-i18n/trunk/src/main/java/
   jboss-i18n/trunk/src/main/java/org/
   jboss-i18n/trunk/src/main/java/org/jboss/
   jboss-i18n/trunk/src/main/java/org/jboss/i18n/
   jboss-i18n/trunk/src/main/java/org/jboss/i18n/Messages.java
   jboss-i18n/trunk/src/main/java/org/jboss/i18n/ParameterConverter.java
   jboss-i18n/trunk/src/test/
   jboss-i18n/trunk/src/test/java/
Log:
Import I18n project

Added: jboss-i18n/trunk/pom.xml
===================================================================
--- jboss-i18n/trunk/pom.xml	                        (rev 0)
+++ jboss-i18n/trunk/pom.xml	2010-04-09 18:57:38 UTC (rev 4252)
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.jboss.i18n</groupId>
+    <artifactId>jboss-i18n</artifactId>
+    <version>1.0.0.Beta1-SNAPSHOT</version>
+
+    <name>JBoss I18n</name>
+</project>
\ No newline at end of file

Added: jboss-i18n/trunk/src/main/java/org/jboss/i18n/Messages.java
===================================================================
--- jboss-i18n/trunk/src/main/java/org/jboss/i18n/Messages.java	                        (rev 0)
+++ jboss-i18n/trunk/src/main/java/org/jboss/i18n/Messages.java	2010-04-09 18:57:38 UTC (rev 4252)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.i18n;
+
+import java.lang.reflect.Field;
+import java.util.Locale;
+
+/**
+ * A factory class to produce message bundle implementations.
+ *
+ * @author <a href="mailto:david.lloyd at redhat.com">David M. Lloyd</a>
+ */
+public final class Messages {
+
+    private Messages() {
+    }
+
+    /**
+     * Get a message bundle of the given type.  Equivalent to <code>{@link #getBundle(Class, java.util.Locale) getBundle}(type, Locale.getDefault())</code>.
+     *
+     * @param type the bundle type class
+     * @param <T> the bundle type
+     * @return the bundle
+     */
+    public static <T> T getBundle(Class<T> type) {
+        return getBundle(type, Locale.getDefault());
+    }
+
+    /**
+     * Get a message bundle of the given type.
+     *
+     * @param type the bundle type class
+     * @param locale the message locale to use
+     * @param <T> the bundle type
+     * @return the bundle
+     */
+    public static <T> T getBundle(Class<T> type, Locale locale) {
+        String language = locale.getLanguage();
+        String country = locale.getCountry();
+        String variant = locale.getVariant();
+
+        Class<? extends T> bundleClass = null;
+        if (variant != null && variant.length() > 0) try {
+            bundleClass = Class.forName(join(type.getName(), "$bundle", language, country, variant), true, type.getClassLoader()).asSubclass(type);
+        } catch (ClassNotFoundException e) {
+            // ignore
+        }
+        if (bundleClass == null && country != null && country.length() > 0) try {
+            bundleClass = Class.forName(join(type.getName(), "$bundle", language, country, null), true, type.getClassLoader()).asSubclass(type);
+        } catch (ClassNotFoundException e) {
+            // ignore
+        }
+        if (bundleClass == null && language != null && language.length() > 0) try {
+            bundleClass = Class.forName(join(type.getName(), "$bundle", language, null, null), true, type.getClassLoader()).asSubclass(type);
+        } catch (ClassNotFoundException e) {
+            // ignore
+        }
+        if (bundleClass == null) try {
+            bundleClass = Class.forName(join(type.getName(), "$bundle", null, null, null), true, type.getClassLoader()).asSubclass(type);
+        } catch (ClassNotFoundException e) {
+            throw new IllegalArgumentException("Invalid bundle " + type + " (implementation not found)");
+        }
+        final Field field;
+        try {
+            field = bundleClass.getField("INSTANCE");
+        } catch (NoSuchFieldException e) {
+            throw new IllegalArgumentException("Bundle implementation " + bundleClass + " has no instance field");
+        }
+        try {
+            return type.cast(field.get(null));
+        } catch (IllegalAccessException e) {
+            throw new IllegalArgumentException("Bundle implementation " + bundleClass + " could not be instantiated", e);
+        }
+    }
+
+    private static String join(String interfaceName, String a, String b, String c, String d) {
+        final StringBuilder build = new StringBuilder();
+        build.append(interfaceName).append('_').append(a);
+        if (b != null && b.length() > 0) {
+            build.append('_');
+            build.append(b);
+        }
+        if (c != null && c.length() > 0) {
+            build.append('_');
+            build.append(c);
+        }
+        if (d != null && d.length() > 0) {
+            build.append('_');
+            build.append(d);
+        }
+        return build.toString();
+    }
+}
\ No newline at end of file

Added: jboss-i18n/trunk/src/main/java/org/jboss/i18n/ParameterConverter.java
===================================================================
--- jboss-i18n/trunk/src/main/java/org/jboss/i18n/ParameterConverter.java	                        (rev 0)
+++ jboss-i18n/trunk/src/main/java/org/jboss/i18n/ParameterConverter.java	2010-04-09 18:57:38 UTC (rev 4252)
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.i18n;
+
+import java.util.Locale;
+
+/**
+ * A converter for a specific parameter type.
+ *
+ * @author <a href="mailto:david.lloyd at redhat.com">David M. Lloyd</a>
+ * @type <I> the input type
+ */
+public interface ParameterConverter<I> {
+
+    /**
+     * Convert the parameter to its string or string-equivalent representation.  The returned value will be passed in
+     * as a parameter to either a {@link java.text.MessageFormat} or {@link java.util.Formatter} instance, depending
+     * on the setting of {@link org.jboss.i18ntool.annotation.Message#format()}.
+     *
+     * @param locale the locale
+     * @param parameter the parameter
+     * @return the converted value
+     */
+    Object convert(Locale locale, I parameter);
+}
\ No newline at end of file



More information about the jboss-svn-commits mailing list