001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package javax.microedition.global;
027:
028: import com.sun.j2me.global.LocaleHelpers;
029: import com.sun.j2me.global.ResourceManagerFactory;
030: import com.sun.j2me.global.ResourceAbstractionLayer;
031: import javax.microedition.global.ResourceException;
032:
033: // JAVADOC COMMENT ELIDED
034: public class ResourceManager {
035:
036: /** The instance of ResourceAbstractionLayer. */
037: private static ResourceAbstractionLayer abstractionLayer = ResourceAbstractionLayer
038: .getInstance();
039:
040: // JAVADOC COMMENT ELIDED
041: public final static String DEVICE = "";
042:
043: /** The base name. */
044: private String baseName;
045: /** The locale identifier. */
046: private String locale;
047:
048: /** The resource manager object. */
049: private com.sun.j2me.global.ResourceManager rm;
050:
051: // JAVADOC COMMENT ELIDED
052: public final static ResourceManager getManager(String baseName)
053: throws ResourceException {
054: // get system locale
055: String locale = System.getProperty("microedition.locale");
056: if (locale == null) {
057: throw new ResourceException(
058: ResourceException.NO_SYSTEM_DEFAULT_LOCALE,
059: "System default locale is undefined");
060: }
061: return getManager(baseName, locale);
062: }
063:
064: // JAVADOC COMMENT ELIDED
065: public final static ResourceManager getManager(String baseName,
066: String locale) throws ResourceException {
067: if (baseName == null) {
068: throw new NullPointerException("Base name is null");
069: }
070:
071: if (locale == null) {
072: throw new NullPointerException("Locale is null");
073: }
074:
075: if (!LocaleHelpers.isValidLocale(locale)
076: && !("".equals(locale))) {
077: throw new IllegalArgumentException("Invalid locale format");
078: }
079:
080: locale = LocaleHelpers.normalizeLocale(locale);
081:
082: return new ResourceManager(com.sun.j2me.global.ResourceManager
083: .getManager(baseName, locale));
084: }
085:
086: // JAVADOC COMMENT ELIDED
087: public final static ResourceManager getManager(String baseName,
088: String[] locales) {
089:
090: if (baseName == null) {
091: throw new NullPointerException("Base name is null");
092: }
093:
094: if (locales == null) {
095: throw new NullPointerException("Locales array is null");
096: }
097:
098: if (locales.length == 0) {
099: throw new IllegalArgumentException("Empty locales array");
100: }
101:
102: for (int i = 0; i < locales.length; i++) {
103: if (locales[i] == null) {
104: throw new NullPointerException("Locale at position "
105: + i + " is null");
106: }
107: if (!LocaleHelpers.isValidLocale(locales[i])
108: && !("".equals(locales[i]))) {
109: throw new IllegalArgumentException(
110: "Locale at position " + i
111: + " has invalid format");
112: }
113: }
114:
115: return new ResourceManager(com.sun.j2me.global.ResourceManager
116: .getManager(baseName, locales));
117: }
118:
119: // JAVADOC COMMENT ELIDED
120: public static String[] getSupportedLocales(String baseName) {
121: if (baseName == null) {
122: throw new NullPointerException("Base name is null");
123: }
124: return com.sun.j2me.global.ResourceManager
125: .getSupportedLocales(baseName);
126: }
127:
128: // JAVADOC COMMENT ELIDED
129: private ResourceManager(com.sun.j2me.global.ResourceManager rm) {
130: this .rm = rm;
131: }
132:
133: // JAVADOC COMMENT ELIDED
134: public String getBaseName() {
135: return rm.getBaseName();
136: }
137:
138: // JAVADOC COMMENT ELIDED
139: public String getLocale() {
140: return rm.getLocale();
141: }
142:
143: // JAVADOC COMMENT ELIDED
144: public byte[] getData(int id) throws ResourceException {
145: return rm.getData(id);
146: }
147:
148: // JAVADOC COMMENT ELIDED
149: public String getString(int id) throws ResourceException {
150: return rm.getString(id);
151: }
152:
153: // JAVADOC COMMENT ELIDED
154: public Object getResource(int id) throws ResourceException {
155: return rm.getResource(id);
156: }
157:
158: // JAVADOC COMMENT ELIDED
159: public boolean isCaching() {
160: return rm.isCaching();
161: }
162:
163: // JAVADOC COMMENT ELIDED
164: public boolean isValidResourceID(int id) {
165: return rm.isValidResourceID(id);
166: }
167: }
|