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: package org.apache.commons.vfs.util;
018:
019: import java.io.IOException;
020: import java.net.URL;
021: import java.util.Enumeration;
022: import java.util.Locale;
023: import java.util.Properties;
024: import java.util.ResourceBundle;
025:
026: public class CombinedResources extends ResourceBundle {
027: // locale.getLanguage()
028: // locale.getCountry()
029: // locale.getVariant()
030:
031: private final String resourceName;
032: private boolean inited;
033: private final Properties properties = new Properties();
034:
035: public CombinedResources(String resourceName) {
036: this .resourceName = resourceName;
037: }
038:
039: protected void init() {
040: if (inited) {
041: return;
042: }
043:
044: loadResources(getResourceName());
045: loadResources(Locale.getDefault());
046: loadResources(getLocale());
047: inited = true;
048: }
049:
050: protected void loadResources(Locale locale) {
051: if (locale == null) {
052: return;
053: }
054: String[] parts = new String[] { locale.getLanguage(),
055: locale.getCountry(), locale.getVariant() };
056: StringBuffer sb = new StringBuffer();
057: for (int i = 0; i < 3; i++) {
058: sb.append(getResourceName());
059: for (int j = 0; j < i; j++) {
060: sb.append('_').append(parts[j]);
061: }
062: if (parts[i].length() != 0) {
063: sb.append('_').append(parts[i]);
064: loadResources(sb.toString());
065: }
066: sb.setLength(0);
067: }
068: }
069:
070: protected void loadResources(String resourceName) {
071: ClassLoader loader = getClass().getClassLoader();
072: if (loader == null) {
073: loader = ClassLoader.getSystemClassLoader();
074: }
075: resourceName = resourceName.replace('.', '/') + ".properties";
076: try {
077: Enumeration resources = loader.getResources(resourceName);
078: while (resources.hasMoreElements()) {
079: URL resource = (URL) resources.nextElement();
080: try {
081: properties.load(resource.openConnection()
082: .getInputStream());
083: } catch (IOException e) {
084: // ignore
085: }
086: }
087: } catch (IOException e) {
088: // ignore
089: }
090: }
091:
092: public String getResourceName() {
093: return resourceName;
094: }
095:
096: public Enumeration getKeys() {
097: if (!inited) {
098: init();
099: }
100: return properties.keys();
101: }
102:
103: protected Object handleGetObject(String key) {
104: if (!inited) {
105: init();
106: }
107: return properties.get(key);
108: }
109: }
|