Search in sources :

Example 1 with LocaleProvider

use of com.opensymphony.xwork2.LocaleProvider 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 LocaleProvider

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

the class ResourceServiceImpl method getLocalResourceForExternalId.

@Override
public Resource getLocalResourceForExternalId(String externalId, Locale locale) {
    Resource resource = _resourceEntriesByExternalId.get(externalId);
    if (resource == null) {
        /**
         * In case the resource has not been first requested as a resource(url)
         * first
         */
        String resourcePath = getExternalIdAsResourcePath(externalId);
        if (resourcePath != null) {
            LocaleProvider localeProvider = new LocaleProviderImpl(locale);
            /**
             * First we see if this is a resource identified by id
             */
            if (_resourcePathsById.containsKey(resourcePath)) {
                List<String> paths = _resourcePathsById.get(resourcePath);
                resource = getResourceForPaths(resourcePath, paths, localeProvider);
            }
            if (resource == null)
                resource = getResourceForPath(resourcePath, localeProvider, null);
        }
    }
    if (resource == null) {
        _log.warn("resource not found for external id: " + externalId);
        return null;
    }
    return resource;
}
Also used : LocaleProvider(com.opensymphony.xwork2.LocaleProvider) Resource(org.onebusaway.presentation.services.resources.Resource)

Example 3 with LocaleProvider

use of com.opensymphony.xwork2.LocaleProvider 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)

Example 4 with LocaleProvider

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

the class ResourceServiceImpl method getExternalUrlForResources.

@Override
public String getExternalUrlForResources(String resourceId, List<String> resourcePaths, Locale locale) {
    LocaleProvider localeProvider = new LocaleProviderImpl(locale);
    Resource resource = getResourceForPaths(resourceId, resourcePaths, localeProvider);
    if (resource == null) {
        _log.warn("resource not found: " + resourceId);
        return null;
    }
    if (_debug)
        refreshResource(resource);
    return resource.getExternalUrl();
}
Also used : LocaleProvider(com.opensymphony.xwork2.LocaleProvider) Resource(org.onebusaway.presentation.services.resources.Resource)

Example 5 with LocaleProvider

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

the class ResourceServiceImpl method getExternalUrlForResource.

/**
 **
 * {@link ResourceService} Interface
 ***
 */
@Override
public String getExternalUrlForResource(String resourcePath, Locale locale) {
    LocaleProvider localeProvider = new LocaleProviderImpl(locale);
    Resource resource = getResourceForPath(resourcePath, localeProvider, null);
    if (resource == null) {
        _log.warn("resource not found: " + resourcePath);
        return null;
    }
    if (_debug)
        refreshResource(resource);
    return resource.getExternalUrl();
}
Also used : LocaleProvider(com.opensymphony.xwork2.LocaleProvider) Resource(org.onebusaway.presentation.services.resources.Resource)

Aggregations

LocaleProvider (com.opensymphony.xwork2.LocaleProvider)3 Resource (org.onebusaway.presentation.services.resources.Resource)3 TextProvider (com.opensymphony.xwork2.TextProvider)2 ResourceBundle (java.util.ResourceBundle)2 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)1 File (java.io.File)1 IOException (java.io.IOException)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