jdk/test/java/util/ResourceBundle/modules/xmlformat/src/bundles/jdk/test/resources/spi/MyResourcesProvider.java
author naoto
Thu, 01 Jun 2017 14:52:53 -0700
changeset 45439 5673d77a787b
parent 36511 jdk/test/java/util/ResourceBundle/modules/xmlformat/src/bundles/jdk/test/resources/MyResourcesProvider.java@9d0388c6b336
permissions -rw-r--r--
8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
45439
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 36511
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
45439
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 36511
diff changeset
    24
package jdk.test.resources.spi;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import java.io.BufferedInputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.InputStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.Enumeration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Locale;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Properties;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.ResourceBundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.spi.ResourceBundleProvider;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
public class MyResourcesProvider implements ResourceBundleProvider {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
    @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
    public ResourceBundle getBundle(String baseName, Locale locale) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
        String xmlName = toXMLName(baseName, locale);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
        try (InputStream is = this.getClass().getModule().getResourceAsStream(xmlName)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
            return new XMLResourceBundle(new BufferedInputStream(is));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
        } catch (IOException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
            throw new RuntimeException(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
    private String toXMLName(String baseName, Locale locale) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        StringBuilder sb = new StringBuilder(baseName.replace('.', '/'));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        String lang = locale.getLanguage();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        if (!lang.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
            sb.append('_').append(lang);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
            String country = locale.getCountry();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            if (!country.isEmpty()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
                sb.append('_').append(country);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        return sb.append(".xml").toString();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
    private static class XMLResourceBundle extends ResourceBundle {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
        private Properties props;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        XMLResourceBundle(InputStream stream) throws IOException {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            props = new Properties();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            props.loadFromXML(stream);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        protected Object handleGetObject(String key) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            if (key == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                throw new NullPointerException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            return props.get(key);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
        public Enumeration<String> getKeys() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
            // Not implemented
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
}