001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.net.URL;
015: import java.net.URLClassLoader;
016: import java.util.MissingResourceException;
017: import java.util.PropertyResourceBundle;
018: import java.util.StringTokenizer;
019:
020: import org.eclipse.core.runtime.Platform;
021:
022: public class NLResourceHelper {
023: public static final String KEY_PREFIX = "%"; //$NON-NLS-1$
024: public static final String KEY_DOUBLE_PREFIX = "%%"; //$NON-NLS-1$
025: private PropertyResourceBundle bundle = null;
026:
027: public NLResourceHelper(String name, URL[] locations) {
028: try {
029: InputStream stream = getResourceStream(name, locations);
030: if (stream != null) {
031: bundle = new PropertyResourceBundle(stream);
032: stream.close();
033: }
034: } catch (IOException e) {
035: }
036: }
037:
038: public void dispose() {
039: bundle = null;
040: }
041:
042: private InputStream getResourceStream(String name, URL[] locations) {
043: URLClassLoader resourceLoader = new URLClassLoader(locations,
044: null);
045:
046: StringTokenizer tokenizer = new StringTokenizer(Platform
047: .getNL(), "_"); //$NON-NLS-1$
048: String language = tokenizer.nextToken();
049: String country = (tokenizer.hasMoreTokens() ? tokenizer
050: .nextToken() : ""); //$NON-NLS-1$
051: String variant = (tokenizer.hasMoreTokens() ? tokenizer
052: .nextToken() : ""); //$NON-NLS-1$
053:
054: String suffix1 = "_" + language + "_" + country + "_" + variant; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
055: String suffix2 = "_" + language + "_" + country; //$NON-NLS-1$ //$NON-NLS-2$
056: String suffix3 = "_" + language; //$NON-NLS-1$
057: String suffix4 = ""; //$NON-NLS-1$
058:
059: String[] suffices = new String[] { suffix1, suffix2, suffix3,
060: suffix4 };
061:
062: InputStream stream = null;
063: for (int i = 0; i < suffices.length; i++) {
064: stream = resourceLoader.getResourceAsStream(name
065: + suffices[i] + ".properties"); //$NON-NLS-1$
066: if (stream != null)
067: break;
068: }
069: return stream;
070: }
071:
072: public String getResourceString(String value) {
073: String s = value.trim();
074:
075: if (!s.startsWith(KEY_PREFIX))
076: return s;
077:
078: if (s.startsWith(KEY_DOUBLE_PREFIX))
079: return s.substring(1);
080:
081: int ix = s.indexOf(" "); //$NON-NLS-1$
082: String key = ix == -1 ? s : s.substring(0, ix);
083: String dflt = ix == -1 ? s : s.substring(ix + 1);
084:
085: if (bundle == null)
086: return dflt;
087:
088: try {
089: return bundle.getString(key.substring(1));
090: } catch (MissingResourceException e) {
091: return dflt;
092: }
093: }
094:
095: public boolean resourceExists(String value) {
096: if (bundle == null)
097: return false;
098: try {
099: bundle.getString(value.trim().substring(1));
100: return true;
101: } catch (MissingResourceException e) {
102: return false;
103: }
104: }
105:
106: }
|