J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | /* |
| 24 | * @test |
| 25 | * @bug 5102289 4403721 |
| 26 | * @summary Test XML support as shown in the ResourceBundle.Control description. |
| 27 | */ |
| 28 | |
| 29 | import java.io.*; |
| 30 | import java.net.*; |
| 31 | import java.util.*; |
| 32 | import static java.util.ResourceBundle.Control.*; |
| 33 | |
| 34 | public class XMLResourceBundleTest { |
| 35 | public static void main(String[] args) { |
| 36 | ResourceBundle.Control xmlControl = new ResourceBundle.Control() { |
| 37 | @Override |
| 38 | public List<String> getFormats(String baseName) { |
| 39 | if (baseName == null) { |
| 40 | throw new NullPointerException(); |
| 41 | } |
| 42 | return Arrays.asList("xml"); |
| 43 | } |
| 44 | @Override |
| 45 | public ResourceBundle newBundle(String baseName, Locale locale, |
| 46 | String format, |
| 47 | ClassLoader loader, |
| 48 | boolean reload) |
| 49 | throws IllegalAccessException, |
| 50 | InstantiationException, IOException { |
| 51 | if (baseName == null || locale == null |
| 52 | || format == null || loader == null) { |
| 53 | throw new NullPointerException(); |
| 54 | } |
| 55 | ResourceBundle bundle = null; |
| 56 | if (format.equals("xml")) { |
| 57 | String bundleName = toBundleName(baseName, locale); |
| 58 | String resourceName = toResourceName(bundleName, format); |
| 59 | URL url = loader.getResource(resourceName); |
| 60 | if (url != null) { |
| 61 | URLConnection connection = url.openConnection(); |
| 62 | if (connection != null) { |
| 63 | if (reload) { |
| 64 | // disable caches if reloading |
| 65 | connection.setUseCaches(false); |
| 66 | } |
| 67 | InputStream stream = connection.getInputStream(); |
| 68 | if (stream != null) { |
| 69 | BufferedInputStream bis = new BufferedInputStream(stream); |
| 70 | bundle = new XMLResourceBundle(bis); |
| 71 | bis.close(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return bundle; |
| 77 | } |
| 78 | }; |
| 79 | ResourceBundle rb = ResourceBundle.getBundle("XmlRB", new Locale(""), xmlControl); |
| 80 | String type = rb.getString("type"); |
| 81 | if (!type.equals("XML")) { |
| 82 | throw new RuntimeException("Root Locale: type: got " + type |
| 83 | + ", expected XML (ASCII)"); |
| 84 | } |
| 85 | |
| 86 | rb = ResourceBundle.getBundle("XmlRB", Locale.JAPAN, xmlControl); |
| 87 | type = rb.getString("type"); |
| 88 | // Expect fullwidth "XML" |
| 89 | if (!type.equals("\uff38\uff2d\uff2c")) { |
| 90 | throw new RuntimeException("Locale.JAPAN: type: got " + type |
| 91 | + ", expected \uff38\uff2d\uff2c (fullwidth XML)"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private static class XMLResourceBundle extends ResourceBundle { |
| 96 | private Properties props; |
| 97 | |
| 98 | XMLResourceBundle(InputStream stream) throws IOException { |
| 99 | props = new Properties(); |
| 100 | props.loadFromXML(stream); |
| 101 | } |
| 102 | |
| 103 | protected Object handleGetObject(String key) { |
| 104 | if (key == null) { |
| 105 | throw new NullPointerException(); |
| 106 | } |
| 107 | return props.get(key); |
| 108 | } |
| 109 | |
| 110 | public Enumeration<String> getKeys() { |
| 111 | // Not implemented |
| 112 | return null; |
| 113 | } |
| 114 | } |
| 115 | } |