Author: matzew
Date: Mon Oct 9 12:02:16 2006
New Revision: 454458
URL: http://svn.apache.org/viewvc?view=rev&rev=454458
Log:
issue 106, thx. to Gabrielle Crawford
Added:
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/LocaleElementsResourceLoader.java
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/TrTranslationsResourceLoader.java
Modified:
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/CoreRenderKitResourceLoader.java
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js
Modified:
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/CoreRenderKitResourceLoader.java
URL:
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/CoreRenderKitResourceLoader.java?view=diff&rev=454458&r1=454457&r2=454458
==============================================================================
---
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/CoreRenderKitResourceLoader.java
(original)
+++
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/CoreRenderKitResourceLoader.java
Mon Oct 9 12:02:16 2006
@@ -23,6 +23,10 @@
import java.net.URL;
+import javax.faces.context.FacesContext;
+
+import javax.servlet.http.HttpServletRequest;
+
import org.apache.myfaces.trinidad.logging.TrinidadLogger;
import org.apache.myfaces.trinidad.resource.RegexResourceLoader;
import org.apache.myfaces.trinidad.resource.ResourceLoader;
@@ -47,6 +51,8 @@
register("(/.*/DebugCommon.*\\.js)",
new CoreCommonScriptsResourceLoader(_getCommonLibraryURI(true),
true));
+ register("(/.*LocaleElements.*\\.js)",
+ new
LocaleElementsResourceLoader(getLocaleElementsURI("LocaleElements", true)));
register("(/.*\\.(css|jpg|gif|png|jpeg|svg|js))",
new CoreClassLoaderResourceLoader(parent));
@@ -60,6 +66,40 @@
return base.append(_VERSION)
.append(".js")
.toString();
+ }
+
+ static public String getLocaleElementsURI(String str,
+ Boolean incVersion)
+ {
+ StringBuffer base = new StringBuffer("/adf/jsLibs/resources/");
+
+ base.append(str);
+ base.append("_");
+
+ String locStr = getLocale();
+
+ base.append(locStr);
+ if(incVersion) base.append(_VERSION);
+ base.append(".js");
+
+ return base.toString();
+ }
+
+ static public String getLocale()
+ {
+ String path = ((HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext().getRequest()).getPathInfo();
+ String locStr = new String();
+
+ int locIndex = path.indexOf("LocaleElements")+ "LocaleElements_".length();
+ int index = path.indexOf(_VERSION);
+
+ if (index < 0)
+ index = path.indexOf(".js");
+
+ if(index >= 0)
+ locStr = path.substring(locIndex, index);
+
+ return locStr;
}
static public String __getVersion()
Added:
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/LocaleElementsResourceLoader.java
URL:
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/LocaleElementsResourceLoader.java?view=auto&rev=454458
==============================================================================
---
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/LocaleElementsResourceLoader.java
(added)
+++
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/LocaleElementsResourceLoader.java
Mon Oct 9 12:02:16 2006
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2004-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.myfaces.trinidadinternal.resource;
+
+import java.io.IOException;
+
+import java.net.URL;
+import java.net.URLConnection;
+
+import java.util.ArrayList;
+
+import org.apache.myfaces.trinidad.logging.TrinidadLogger;
+import org.apache.myfaces.trinidad.resource.AggregatingResourceLoader;
+import org.apache.myfaces.trinidad.resource.ClassLoaderResourceLoader;
+import org.apache.myfaces.trinidad.resource.ResourceLoader;
+
+/**
+ * A resource loader implementation which serves up the
+ * LocaleElements & ResourceBundle JS libs.
+ */
+public class LocaleElementsResourceLoader extends AggregatingResourceLoader
+{
+ public LocaleElementsResourceLoader(
+ String path)
+ {
+ super(path,
+ _INIT_LIBRARIES,
+ new ClassLoaderResourceLoader());
+
+ setSeparator(_NEWLINE_SEPARATOR);
+ }
+
+ @Override
+ protected URL findResource(
+ String path) throws IOException
+ {
+ return getURL(path);
+ }
+
+ /**
+ * Returns a URL which is an aggregate of all the paths.
+ *
+ * @param path the current path
+ * @return a aggregate url
+ * @throws IOException when something bad happens
+ */
+ @Override
+ protected URL getURL(String path) throws IOException
+ {
+ String[] _LIBRARIES = _getLiraries();
+
+ int len = _LIBRARIES.length;
+ ArrayList<URL> urls = new ArrayList<URL>(len);
+
+ for(int i = 0; i < len; i++)
+ {
+ URL u = _ResourceLoaders[i].getResource(_LIBRARIES[i]);
+ if(u != null)
+ {
+ urls.add(u);
+ }
+ else
+ {
+ _LOG.warning("Resource \"" + _LIBRARIES[i] +
+ "\" at path \"" + path + "\" not found");
+ }
+ }
+
+ urls.trimToSize();
+ URL[] urlArray = urls.toArray(new URL[0]);
+
+ AggregatingURLStreamHandler handler = new
AggregatingURLStreamHandler(urlArray, _NEWLINE_SEPARATOR);
+ return new URL("aggregating", null, -1, path, handler);
+ }
+
+ @Override
+ protected String getContentType(
+ URLConnection conn)
+ {
+ return "text/javascript";
+ }
+
+ private String[] _getLiraries()
+ {
+ String[] _LIBRARIES =
+ {
+ "META-INF" +
CoreRenderKitResourceLoader.getLocaleElementsURI("LocaleElements", false),
+ "META-INF" +
CoreRenderKitResourceLoader.getLocaleElementsURI("Translations", false)
+ };
+
+ return _LIBRARIES;
+ }
+
+ // List of all libraries
+ static private String[] _INIT_LIBRARIES = { "LocaleElements", "Translations"
};
+
+ // List of ResourceLoaders
+ static private final ResourceLoader[] _ResourceLoaders =
+ {
+ new ClassLoaderResourceLoader(),
+ new
TrTranslationsResourceLoader(CoreRenderKitResourceLoader.getLocaleElementsURI("Translations",
false))
+ };
+
+ static private final String _NEWLINE_SEPARATOR = "\n";
+ static private final TrinidadLogger _LOG =
TrinidadLogger.createTrinidadLogger(LocaleElementsResourceLoader.class);
+}
Added:
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/TrTranslationsResourceLoader.java
URL:
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/TrTranslationsResourceLoader.java?view=auto&rev=454458
==============================================================================
---
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/TrTranslationsResourceLoader.java
(added)
+++
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/resource/TrTranslationsResourceLoader.java
Mon Oct 9 12:02:16 2006
@@ -0,0 +1,150 @@
+/*
+* Copyright 2006 The Apache Software Foundation.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.myfaces.trinidadinternal.resource;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.URL;
+
+import org.apache.myfaces.trinidad.resource.StringContentResourceLoader;
+import org.apache.myfaces.trinidad.util.ClassLoaderUtils;
+
+import org.apache.myfaces.trinidadinternal.renderkit.core.xhtml.XhtmlUtils;
+
+public class TrTranslationsResourceLoader extends StringContentResourceLoader
+{
+ /**
+ * Constructs a dynamic resouce loader for this path which serves up
translations
+ *
+ * @param path the path of this dynamic resource loader
+ */
+ public TrTranslationsResourceLoader(String path)
+ {
+ super(path);
+ setMap("TrMessageFactory._TRANSLATIONS");
+ setBundle("org.apache.myfaces.trinidad.resource.MessageBundle");
+ }
+
+ protected void setMap(String mapName)
+ {
+ this.mapName = mapName;
+ }
+
+ protected void setBundle(String bundleName)
+ {
+ this.bundleName = bundleName;
+ }
+
+ @Override
+ protected String getString(String path) throws IOException
+ {
+ // its always better to initialize the StringBuffer with size instead of
leaving it unset
+ StringBuffer bundleMap = new StringBuffer(50000);
+ String content = "";
+
+ String locale = CoreRenderKitResourceLoader.getLocale();
+
+ bundleMap.append(mapName)
+ .append(" = ")
+ .append("\n{\n");
+
+ content += _processBundle(bundleName, locale);
+
+ //Remove the last 2 characters( ie, newLine & ',')
+ bundleMap.append(content.substring(0, content.length()-2))
+ .append("\n};");
+
+ return bundleMap.toString();
+ }
+
+ @Override
+ protected String getContentType(String path)
+ {
+ return _CONTENT_TYPE;
+ }
+
+ @Override
+ protected URL findResource(
+ String path) throws IOException
+ {
+ return getURL(path);
+ }
+
+ protected String _processBundle(
+ String bundleName,
+ String localeName
+ ) throws IOException
+ {
+ StringBuffer transMap = new StringBuffer(50000);
+ Object obj = invokeGetContents(bundleName, localeName);
+
+ if( obj != null)
+ {
+ Object entry[] = (Object [])obj;
+
+ for(int i=0; i< entry.length; i++)
+ {
+ transMap.append("'" + ((Object [])entry[i])[0] + "'")
+ .append(":")
+ .append("'" + XhtmlUtils.escapeJS(((Object
[])entry[i])[1].toString(), true) + "',")
+ .append("\n");
+ }
+ }
+
+ return transMap.toString();
+ }
+
+ protected Object invokeGetContents(
+ String bundleName,
+ String localeName
+ ) throws IOException
+ {
+ String className = bundleName + "_" + localeName;
+ Object obj = null;
+
+ try
+ {
+ Class<?> clazz;
+ try
+ {
+ clazz = ClassLoaderUtils.loadClass(className);
+ }
+ catch (ClassNotFoundException e)
+ {
+ // If there is no bundle specific to locale, use Default locale
+ clazz = ClassLoaderUtils.loadClass(bundleName);
+ }
+
+ //Invoke getContents() method which gives us the Translations
+ Method x = clazz.getMethod("getContents", null);
+ obj = x.invoke(clazz.newInstance(), null);
+
+ } catch (NoSuchMethodException e) { ;
+ } catch (InvocationTargetException e) { ;
+ } catch (IllegalAccessException e) { ;
+ } catch (InstantiationException e) { ;
+ } catch (ClassNotFoundException e) { ;
+ }
+
+ return obj;
+ }
+
+ protected String mapName;
+ protected String bundleName;
+
+ private static final String _CONTENT_TYPE = "text/javascript";
+}
Modified:
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js
URL:
http://svn.apache.org/viewvc/incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js?view=diff&rev=454458&r1=454457&r2=454458
==============================================================================
---
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js
(original)
+++
incubator/adffaces/trunk/trinidad/trinidad-impl/src/main/javascript/META-INF/adf/jsLibs/Locale.js
Mon Oct 9 12:02:16 2006
@@ -797,3 +797,58 @@
// simplifying it
return _formatErrorString(formatString, tempArray);
}
+
+var TrMessageFactory = new Object();
+
+TrMessageFactory.createFacesMessage = function(
+ key,
+ customDetail,
+ parameters,
+ messageSeverity
+ )
+{
+ // the strings to create a facesMessage to use have been sent down
+ var summary = TrMessageFactory.getSummaryString(key);
+ var detail = customDetail;
+ var severity = messageSeverity;
+
+ if ( severity == null)
+ {
+ severity = FacesMessage.SEVERITY_ERROR
+ }
+
+ if (detail == null)
+ {
+ detail = TrMessageFactory.getDetailString(key);
+ }
+
+ if ( detail != null )
+ {
+ if ( parameters != null )
+ {
+ detail = FastMessageFormatUtils.format(detail,parameters);
+ }
+ }
+
+ return new FacesMessage( summary, detail, severity);
+}
+
+TrMessageFactory.getSummaryString = function(
+ key
+ )
+{
+ if (key == null)
+ return null;
+ return TrMessageFactory._TRANSLATIONS[key];
+}
+
+TrMessageFactory.getDetailString = function(
+ key
+ )
+{
+ if (key == null)
+ return null;
+
+ // TODO should I be doing string concat here, or have a map of key ->
detailKey?
+ return TrMessageFactory._TRANSLATIONS[key+"_detail"];
+}