2021-08-16

This commit is contained in:
2021-08-16 11:17:18 +09:00
parent 62360c41a3
commit ed54dbad52
20 changed files with 387 additions and 17 deletions

6
properties/README.md Normal file
View File

@@ -0,0 +1,6 @@
## 자바 Resource Bundle 로캐일 선택 순서
1. 사용자 로캐일과 일치하는 로캐일
2. 사용자 로캐일 언어와 일치하는 로캐일
3. 시스템 기본 로캐일
4. 루트 로캐일

View File

@@ -0,0 +1,14 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-java")
}
dependencies {
}

20
properties/logback.xml Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Examples for Java
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="TRACE">
<appender-ref ref="CONSOLE" />
</root>
</configuration>

View File

@@ -0,0 +1,136 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.*;
@Slf4j
public class MyResourceBundleControl extends ResourceBundle.Control {
@Override
public List<String> getFormats(String baseName) {
if (baseName == null) {
throw new NullPointerException();
}
return Collections.unmodifiableList(
Arrays.asList("xml", "properties"));
}
@Override
public ResourceBundle newBundle(String baseName, Locale locale,
String format,
ClassLoader loader,
boolean reload)
throws IllegalAccessException,
InstantiationException, IOException {
if (baseName == null || locale == null
|| format == null || loader == null) {
throw new NullPointerException();
}
ResourceBundle bundle = null;
if (format.equals("xml")) {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, format);
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
if (reload) {
// disable caches if reloading
connection.setUseCaches(false);
}
log.info("Bundle Name: {}", bundleName);
log.info("Res Name: {}", resourceName);
try (InputStream stream = connection.getInputStream()) {
if (stream != null) {
BufferedInputStream bis =
new BufferedInputStream(stream);
bundle = new XMLResourceBundle(bis);
}
}
}
}
} else if (format.equals("properties")) {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, format);
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
if (reload) {
// disable caches if reloading
connection.setUseCaches(false);
}
log.info("Bundle Name: {}", bundleName);
log.info("Res Name: {}", resourceName);
//System.out.println("Res Name: " + resourceName);
try (InputStream stream = connection.getInputStream()) {
if (stream != null) {
InputStreamReader reader =
new InputStreamReader(new BufferedInputStream(stream), StandardCharsets.UTF_8);
bundle = new PropertyResourceBundle(reader);
}
}
}
}
}
return bundle;
}
@Override
public List<Locale> getCandidateLocales(String baseName, Locale locale) {
List<Locale> locales = Arrays.asList(
locale,
new Locale(locale.getLanguage()),
Locale.ROOT);
log.info("locales: {} -> {}", locale, locales);
return locales;
}
private static class XMLResourceBundle extends ResourceBundle {
private final Properties props;
XMLResourceBundle(InputStream stream) throws IOException {
props = new Properties();
props.loadFromXML(stream);
}
protected Object handleGetObject(String key) {
if (key == null) {
throw new NullPointerException();
}
return props.getProperty(key);
}
public Enumeration<String> getKeys() {
Set<String> handleKeys = props.stringPropertyNames();
return Collections.enumeration(handleKeys);
}
@NotNull
@Override
protected Set<String> handleKeySet() {
return props.stringPropertyNames();
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import java.util.ResourceBundle;
import java.util.spi.ResourceBundleControlProvider;
public class MyResourceBundleControlProvider
implements ResourceBundleControlProvider {
static final ResourceBundle.Control XMLCONTROL =
new MyResourceBundleControl();
public ResourceBundle.Control getControl(String baseName) {
System.out.println("Class: " + getClass().getName()+".getControl");
System.out.println(" called for " + baseName);
// Throws a NPE if baseName is null.
if (baseName.startsWith("resources.Xml")) {
System.out.println(" returns " + XMLCONTROL);
return XMLCONTROL;
}
System.out.println(" returns null");
System.out.println();
return null;
}
}

View File

@@ -0,0 +1,9 @@
#
# Examples for Java
#
# Copyright (c) 2021. Elex. All Rights Reserved.
# https://www.elex-project.com/
#
hello = Hello!
only = Only in Root!

View File

@@ -0,0 +1,8 @@
#
# Examples for Java
#
# Copyright (c) 2021. Elex. All Rights Reserved.
# https://www.elex-project.com/
#
hello = 오하이오!

View File

@@ -0,0 +1,8 @@
#
# Examples for Java
#
# Copyright (c) 2021. Elex. All Rights Reserved.
# https://www.elex-project.com/
#
hello = 안녕!

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
~ Examples for Java
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="hello">Hello</entry>
<entry key="only">Only in Root</entry>
</properties>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--
~ Examples for Java
~
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="hello">안녕</entry>
</properties>

View File

@@ -0,0 +1,9 @@
#
# Examples for Java
#
# Copyright (c) 2021. Elex. All Rights Reserved.
# https://www.elex-project.com/
#
key1 = value1
key2 = value2

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties version="1.0">
<comment>Sample properties file in XML format.</comment>
<entry key="key1">Value 1</entry>
<entry key="key2">Value 2</entry>
</properties>

View File

@@ -0,0 +1,44 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
public class PropertiesTest {
@Test
void properties() throws IOException {
Properties properties = new Properties();
// 인풋스트림만 사용하면 인코딩이 꺠질 수 있으므로 리더를 사용해서 인코딩을 지정해서 불러 온다.
properties.load(new InputStreamReader(getClass()
.getResourceAsStream("/sample.properties"), StandardCharsets.UTF_8));
String value1 = properties.getProperty("key1");
System.out.println(value1);
// setProperty(key, value);
// properties.store(writer, comment);
}
@Test
void xml_properties() throws IOException {
Properties properties = new Properties();
properties.loadFromXML(getClass()
.getResourceAsStream("/sample.xml"));
String value1 = properties.getProperty("key1");
System.out.println(value1);
// setProperty(key, value);
//properties.storeToXML(os, comment);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Examples for Java
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.util.*;
@Slf4j
public class ResourceBundleTest {
@Test
void resource_bundle_xml() {
ResourceBundle bundle = ResourceBundle
.getBundle("language", new MyResourceBundleControl());
System.out.println(bundle.getString("hello"));
System.out.println(bundle.getString("only"));
}
@Test
void resource_bundle_xml_jp() {
ResourceBundle bundle = ResourceBundle
.getBundle("language", Locale.JAPANESE, new MyResourceBundleControl());
System.out.println(bundle.getLocale());
System.out.println(bundle.getString("hello"));
System.out.println(bundle.getString("only"));
}
@Test
void resource_bundle() {
ResourceBundle bundle = ResourceBundle
.getBundle("lang");
System.out.println(bundle.getString("hello"));
System.out.println(bundle.getString("only"));
}
@Test
void resource_bundle_jp() {
ResourceBundle bundle = ResourceBundle
.getBundle("lang", Locale.JAPAN, new MyResourceBundleControl());
log.info("Bundle locale: {}", bundle.getLocale());
System.out.println(bundle.getString("hello"));
System.out.println(bundle.getString("only"));
}
@Test
void resource_bundle_fr() {
Locale.setDefault(Locale.FRANCE);
ResourceBundle bundle = ResourceBundle
.getBundle("lang", Locale.FRENCH, new MyResourceBundleControl());
log.info("Bundle locale: {}", bundle.getLocale());
System.out.println(bundle.getString("hello"));
System.out.println(bundle.getString("only"));
}
}