Hello! Goodbye! Hello! Goodbye! Hello! Goodbye!

How to customize message resources to xml files instead of properties in Struts 2

73 Views Asked by At

I have already xml messages which used for localization.

<messages>
  <message key="greeting">Hello!</entry>
  <message key="farewell">Goodbye!</entry>
</messages>

I wanted to reuse this xml messages for struts2 application. Please provide how it can be customized to read xml files instead properties files

1

There are 1 best solutions below

0
Roman C On

It can be customized if you define a custom TextProvider and configure it to use with the project.

When you want to use your own implementation for Struts 2 project you have to define following bean and constant in struts.xml:

<bean class="org.demo.MyTextProvider" name="myTextProvider" type="com.opensymphony.xwork2.TextProvider" />
<constant name="struts.xworkTextProvider" value="myTextProvider" />

Take a look on ActionSupport for example TextProvider implementation.


You can read struts2 localization guide for example of implementation of

Custom TextProvider and TextProviderFactory:

If you want to use a different logic to search for localized messages, or you want to use a database or just want to search default bundles, you must implement both those interfaces (or subclass the existing implementations). You can check a small example app how to use both. Please remember that the TextProvider interface is implemented by the ActionSupport class, that’s why an extra layer - TextProviderFactory is needed.


And here is the sample implementation for XMLResourceBundle.

public class XMLResourceBundleControl extends ResourceBundle.Control {
  private static String XML = "xml";

  public List<String> getFormats(String baseName) {
    return Collections.singletonList(XML);
  }

  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)) {
      return null;
    }

    String bundleName = toBundleName(baseName, locale);
    String resourceName = toResourceName(bundleName, format);
    URL url = loader.getResource(resourceName);
    if (url == null) {
      return null;
    }
    URLConnection connection = url.openConnection();
    if (connection == null) {
      return null;
    }
    if (reload) {
      connection.setUseCaches(false);
    }
    InputStream stream = connection.getInputStream();
    if (stream == null) {
      return null;
    }
    BufferedInputStream bis = new BufferedInputStream(stream);
    bundle = new XMLResourceBundle(bis);
    bis.close();

    return bundle;
  }

class XMLResourceBundle extends ResourceBundle {
  private Properties props;

  XMLResourceBundle(InputStream stream) throws IOException {
    props = new Properties();
    props.loadFromXML(stream);
  }

  protected Object handleGetObject(String key) {
    return props.getProperty(key);
  }

  public Enumeration<String> getKeys() {
    Set<String> handleKeys = props.stringPropertyNames();
    return Collections.enumeration(handleKeys);
  }
}

In the TextProvider implementation you can use

ResourceBundle bundle = ResourceBundle.getBundle("messages", new XMLResourceBundleControl());

The messages.xml has the following format

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <entry key="greeting">Hello!</entry>
  <entry key="farewell">Goodbye!</entry>
</properties>