001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Ivan Popov
020: * @version $Revision$
021: */package org.eclipse.osgi.util;
022:
023: import java.lang.reflect.Field;
024: import java.lang.reflect.Modifier;
025: import java.util.Locale;
026: import java.util.MissingResourceException;
027: import java.util.ResourceBundle;
028:
029: /**
030: * This stub class replaces org.eclipse.osgi.util.NLS from Eclipse RCP bundle and provides
031: * only NLS functionality required for JDI implementation.
032: */
033: public class NLS {
034:
035: /**
036: * Fills given field with corresponding resource string from given resource bundle.
037: * If bundle is null or corresponding resource string is not found, then
038: * field name will be assigned to the field.
039: *
040: * @param field - static field to assign resource string to
041: * @param bundle - resource bundle where to find resource string or null
042: */
043: private static void fillFieldValue(Field field,
044: ResourceBundle bundle) {
045: String name = field.getName();
046: String value = null;
047: if (bundle != null) {
048: try {
049: value = bundle.getString(name);
050: } catch (MissingResourceException e) {
051: // ignore
052: e.printStackTrace(); // TODO: remove
053: }
054: }
055: if (value == null) {
056: value = name;
057: }
058: try {
059: field.set(null, value);
060: } catch (Exception e) {
061: // ignore
062: e.printStackTrace(); // TODO: remove
063: }
064: }
065:
066: /**
067: * Loads localized messages from given bundle and assigns them to corresponding
068: * static fields of given class.
069: *
070: * @param bundleName - name of resource bundle to load messages from
071: * @param cls - Class instance whose static fields will be filled with appropriate messages
072: */
073: public static void initializeMessages(String bundleName, Class cls) {
074: ClassLoader loader = cls.getClassLoader();
075: Field[] fields = cls.getDeclaredFields();
076:
077: // load bundle for specified class
078: ResourceBundle bundle = null;
079: try {
080: Locale locale = Locale.getDefault();
081: bundle = ResourceBundle.getBundle(bundleName, locale,
082: loader);
083: } catch (MissingResourceException e) {
084: // ignore
085: e.printStackTrace(); // TODO: remove
086: }
087:
088: // fill appropriate class fields with strings from resource bundle
089: for (int i = 0; i < fields.length; i++) {
090: Field field = fields[i];
091: // only fields declared in given class
092: if (field.getDeclaringClass() == cls) {
093: // only public static and not final fields
094: int modifiers = field.getModifiers();
095: if ((modifiers & Modifier.FINAL) == 0
096: && (modifiers & Modifier.STATIC) != 0
097: && (modifiers & Modifier.PUBLIC) != 0) {
098: fillFieldValue(fields[i], bundle);
099: }
100: }
101: }
102: }
103: }
|