Skip to content

Instantly share code, notes, and snippets.

@asicfr
Created May 2, 2016 13:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save asicfr/1b76ea60029264d7be15d019a866e1a4 to your computer and use it in GitHub Desktop.
Save asicfr/1b76ea60029264d7be15d019a866e1a4 to your computer and use it in GitHub Desktop.
Properties and ResourceBundle from Xml file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>labels francais</comment>
<entry key="maCle1">la valeur de la cle 1</entry>
<entry key="maCle2">la valeur de la cle 2</entry>
<entry key="maCle3">la valeur de la cle 3</entry>
</properties>
package test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
public class TestClass {
public static void main(String[] args) throws FileNotFoundException, IOException {
// Properties avec ressource xml
try( InputStream inputstream = PropertiesXml.class.getClass().getResourceAsStream("/labels_fr.xml"); ) {
Properties props = new Properties();
props.loadFromXML(inputstream);
System.out.println("maCle1: " + props.getProperty("maCle1"));
}
// ResourceBundle avec ressource xml
ResourceBundle bundle = ResourceBundle.getBundle("labels", new XMLResourceBundleControl());
System.out.println("maCle1: " + bundle.getString("maCle1"));
}
}
package test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
/**
* XML Resource Bundle
* @author slabbe
*/
public class XMLResourceBundle extends ResourceBundle {
private Properties props;
public XMLResourceBundle(InputStream stream) throws IOException {
props = new Properties();
props.loadFromXML(stream);
}
@Override
protected Object handleGetObject(String key) {
return props.getProperty(key);
}
@Override
public Enumeration<String> getKeys() {
Set<String> handleKeys = props.stringPropertyNames();
return Collections.enumeration(handleKeys);
}
}
package test;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* Xml resource bundle control
* @author slabbe
*/
public class XMLResourceBundleControl extends ResourceBundle.Control {
private static final String XML = "xml";
private static final List<String> SINGLETON_LIST = Collections.singletonList(XML);
@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 IllegalArgumentException("baseName, locale, format and loader cannot be null");
}
if (!format.equals(XML)) {
throw new IllegalArgumentException("format must be xml");
}
final String bundleName = toBundleName(baseName, locale);
final String resourceName = toResourceName(bundleName, format);
final URL url = loader.getResource(resourceName);
if (url == null) {
return null;
}
final URLConnection urlconnection = url.openConnection();
if (urlconnection == null) {
return null;
}
if (reload) {
urlconnection.setUseCaches(false);
}
try ( final InputStream stream = urlconnection.getInputStream();
final BufferedInputStream bis = new BufferedInputStream(stream);
) {
final ResourceBundle bundle = new XMLResourceBundle(bis);
return bundle;
}
}
@Override
public List<String> getFormats(String baseName) {
return SINGLETON_LIST;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment