1/*
2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package com.foo;
25
26import java.io.*;
27import java.net.*;
28import java.util.*;
29import static java.util.ResourceBundle.Control.*;
30
31public class UserXMLControl extends ResourceBundle.Control {
32    @Override
33    public List<String> getFormats(String baseName) {
34        if (baseName == null) {
35            throw new NullPointerException();
36        }
37        return Arrays.asList("xml");
38    }
39
40    @Override
41    public ResourceBundle newBundle(String baseName, Locale locale,
42                                    String format,
43                                    ClassLoader loader,
44                                    boolean reload)
45        throws IllegalAccessException,
46               InstantiationException, IOException {
47        if (baseName == null || locale == null
48            || format == null || loader == null) {
49            throw new NullPointerException();
50        }
51        ResourceBundle bundle = null;
52        if (format.equals("xml")) {
53            String bundleName = toBundleName(baseName, locale);
54            String resourceName = toResourceName(bundleName, format);
55            URL url = loader.getResource(resourceName);
56            if (url != null) {
57                URLConnection connection = url.openConnection();
58                if (connection != null) {
59                    if (reload) {
60                        // disable caches if reloading
61                        connection.setUseCaches(false);
62                    }
63                    try (InputStream stream = connection.getInputStream()) {
64                        if (stream != null) {
65                            BufferedInputStream bis = new BufferedInputStream(stream);
66                            bundle = new XMLResourceBundle(bis);
67                        }
68                    }
69                }
70            }
71        }
72        return bundle;
73    }
74
75    private static class XMLResourceBundle extends ResourceBundle {
76        private Properties props;
77
78        XMLResourceBundle(InputStream stream) throws IOException {
79            props = new Properties();
80            props.loadFromXML(stream);
81        }
82
83        protected Object handleGetObject(String key) {
84            if (key == null) {
85                throw new NullPointerException();
86            }
87            return props.get(key);
88        }
89
90        public Enumeration<String> getKeys() {
91            // Not implemented
92            return null;
93        }
94    }
95}
96