JDK1.5에는 Properties 클래스에 loadFromXML(InputStream)과 storeToXML(OutputStream,String,String) 메소드가 추가되었다.
Properties 를 사용할때 항상 문제가 되던 한글 처리가 XML을 로드하게 되면서 간단하게
<?xml version="1.0" encoding="UTF-8"?>
하지만 아직 ResourceBundle 에선 ISO-8859-1로 인코딩된 properties 파일만 읽을수 있다.
이건 JDK 6 버전에서 ResourceBundle.control 이란 녀석을 확장해서 처리 가능하다.
ResourceBundle bundle =
ResourceBundle.getBundle("messages", new MyControl());
: MyControl 은 Resourcebundle.control 의 sub class
: java Class, properties, xml 외에도 원하는 타입으로 사용 가능하다.
/**
* JDK 6's {@link ResourceBundle.Control} subclass that allows
* loading of bundles in XML format.
* The bundles are searched first as Java classes, then as
* properties files (these two methods are the standard
* search mechanism of ResourceBundle), then as XML properties
* files.
* The filename extension of the XML properties files is assumed
* to be *.properties.xml
*/
public class ExtendedControl extends ResourceBundle.Control{
private static final String FORMAT_XML_SUFFIX = "properties.xml";
private static final String FORMAT_XML = "java." + FORMAT_XML_SUFFIX;
private static final List<String> FORMATS;
static {
List<String> formats = new ArrayList<String>(FORMAT_DEFAULT);
formats.add(FORMAT_XML);
FORMATS = Collections.unmodifiableList(formats);
}
@Override
public List<String> getFormats(String baseName) {
return FORMATS;
}
@Override
public ResourceBundle newBundle(String baseName, Locale locale,String format,
ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
if (!FORMAT_XML.equals(format))
return super.newBundle(baseName, locale, format, loader, reload);
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, FORMAT_XML_SUFFIX);
final URL resourceURL = loader.getResource(resourceName);
if (resourceURL == null) return null;
InputStream stream = getResourceInputStream(resourceURL, reload);
try {
PropertyXMLResourceBundle result = new PropertyXMLResourceBundle();
result.load(stream);
return result;
} finally {
stream.close();
}
}
private InputStream getResourceInputStream(final URL resourceURL,
boolean reload) throws IOException {
if (!reload) return resourceURL.openStream();
try {
// This permission has already been checked by
// ClassLoader.getResource(String), which will return null
// in case the code has not enough privileges.
return AccessController.doPrivileged(
new PrivilegedExceptionAction<InputStream>() {
public InputStream run() throws IOException {
URLConnection connection = resourceURL.openConnection();
connection.setUseCaches(false);
return connection.getInputStream();
}
});
} catch (PrivilegedActionException x) {
throw (IOException)x.getCause();
}
}
/**
* ResourceBundle that loads definitions from an XML properties file.
*/
public static class PropertyXMLResourceBundle extends ResourceBundle {
private final Properties properties = new Properties();
public void load(InputStream stream) throws IOException {
properties.loadFromXML(stream);
}
protected Object handleGetObject(String key) {
return properties.getProperty(key);
}
public Enumeration<String> getKeys() {
final Enumeration<Object> keys = properties.keys();
return new Enumeration<String>() {
public boolean hasMoreElements() {
return keys.hasMoreElements();
}
public String nextElement() {
return (String)keys.nextElement();
}
};
}
}}
http://bordet.blogspot.com/2007/01/utf-8-handling-for-resourcebundle-and.html
'java' 카테고리의 다른 글
NIO FileCopy (0) | 2008.05.15 |
---|---|
jdk 1.5 패키지 확장 (0) | 2008.05.15 |
날짜 (0) | 2008.05.14 |
utf-8 인코딩을 이용한 한글 url 처리 (0) | 2006.10.27 |
객체직렬화를 통해 직렬화된 객체를 Oracle BLOB에 저장하기 (0) | 2006.07.16 |