Search in sources :

Example 1 with TextProvider

use of com.opensymphony.xwork2.TextProvider in project onebusaway-application-modules by camsys.

the class ResourceServiceImpl method getMessagesResourceAsSourceUrl.

private URL getMessagesResourceAsSourceUrl(String resourceName, LocaleProvider localeProvider) {
    int index = resourceName.indexOf('=');
    if (index == -1)
        throw new IllegalStateException("invalid resource messages specifier: " + resourceName);
    String messagesPrefix = resourceName.substring(0, index);
    String messagesResourceClassName = resourceName.substring(index + 1);
    Class<?> messagesResourceClass = null;
    try {
        messagesResourceClass = Class.forName(messagesResourceClassName);
    } catch (Throwable ex) {
        throw new IllegalStateException("error loading messages resource class " + messagesResourceClassName, ex);
    }
    TextProvider provider = _textProviderFactory.createInstance(messagesResourceClass, localeProvider);
    ResourceBundle bundle = provider.getTexts();
    Map<String, String> resourceMapping = new HashMap<String, String>();
    for (Enumeration<String> en = bundle.getKeys(); en.hasMoreElements(); ) {
        String key = en.nextElement();
        String value = bundle.getString(key);
        resourceMapping.put(key, value);
    }
    try {
        File file = getOutputFile(PREFIX_MESSAGES + messagesPrefix + ".js");
        PrintWriter out = new PrintWriter(file);
        JSONObject obj = new JSONObject(resourceMapping);
        out.println("var OBA = window.OBA || {};");
        out.println("if(!OBA.Resources) { OBA.Resources = {}; }");
        out.println("OBA.Resources." + messagesPrefix + " = " + obj.toString() + ";");
        out.close();
        return getFileAsUrl(file);
    } catch (IOException ex) {
        throw new IllegalStateException("error loading resources", ex);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) TextProvider(com.opensymphony.xwork2.TextProvider) JSONObject(org.json.JSONObject) ResourceBundle(java.util.ResourceBundle) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with TextProvider

use of com.opensymphony.xwork2.TextProvider in project onebusaway-application-modules by camsys.

the class MessageComponent method end.

@Override
public boolean end(Writer writer, String body) {
    if (_key != null) {
        ValueStack stack = getStack();
        CompoundRoot root = stack.getRoot();
        TextProvider textProvider = null;
        for (Object obj : root) {
            if (obj instanceof TextProvider) {
                textProvider = (TextProvider) obj;
                break;
            }
        }
        if (textProvider != null) {
            String message = textProvider.getText(_key, _arguments);
            if (message != null) {
                try {
                    writer.write(message);
                } catch (IOException e) {
                    LOG.error("Could not write out tag", e);
                }
            }
        }
    }
    return super.end(writer, "");
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) CompoundRoot(com.opensymphony.xwork2.util.CompoundRoot) IOException(java.io.IOException) TextProvider(com.opensymphony.xwork2.TextProvider)

Example 3 with TextProvider

use of com.opensymphony.xwork2.TextProvider in project onebusaway-application-modules by camsys.

the class ResourceBundleSupport method getLocaleMap.

public static Map<String, String> getLocaleMap(LocaleProvider localeProvider, Class<?> resourceType) {
    TextProviderFactory factory = new TextProviderFactory();
    TextProvider provider = factory.createInstance(resourceType, localeProvider);
    ResourceBundle bundle = provider.getTexts();
    Map<String, String> m = new LinkedHashMap<String, String>();
    for (Enumeration<String> en = bundle.getKeys(); en.hasMoreElements(); ) {
        String key = en.nextElement();
        String value = bundle.getString(key);
        m.put(key, value);
    }
    return m;
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) ResourceBundle(java.util.ResourceBundle) TextProvider(com.opensymphony.xwork2.TextProvider) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TextProvider (com.opensymphony.xwork2.TextProvider)3 IOException (java.io.IOException)2 ResourceBundle (java.util.ResourceBundle)2 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)1 CompoundRoot (com.opensymphony.xwork2.util.CompoundRoot)1 ValueStack (com.opensymphony.xwork2.util.ValueStack)1 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JSONObject (org.json.JSONObject)1