Author: bdaw
Date: 2007-07-09 22:04:04 -0400 (Mon, 09 Jul 2007)
New Revision: 7699
Modified:
branches/JBoss_Portal_Branch_2_6/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java
Log:
JBPORTAL-1533 - Internationalize gadget metadata
Modified:
branches/JBoss_Portal_Branch_2_6/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java
===================================================================
---
branches/JBoss_Portal_Branch_2_6/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java 2007-07-09
23:50:46 UTC (rev 7698)
+++
branches/JBoss_Portal_Branch_2_6/widget/src/main/org/jboss/portal/widget/google/provider/GGWidgetInfoBuilder.java 2007-07-10
02:04:04 UTC (rev 7699)
@@ -37,6 +37,8 @@
import org.jboss.portal.widget.google.GGWidgetInfo;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
@@ -45,6 +47,8 @@
import java.util.Locale;
import java.util.Collection;
import java.util.ArrayList;
+import java.util.Map;
+import java.util.HashMap;
import java.io.ByteArrayInputStream;
/**
@@ -53,7 +57,7 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
-public class GGWidgetInfoBuilder
+public class GGWidgetInfoBuilder
{
/** . */
@@ -96,6 +100,110 @@
Document doc = builder.parse(new ByteArrayInputStream(bytes));
Element moduleElt = doc.getDocumentElement();
+ Map messagesBundle = new HashMap();
+
+ //first process locale bundles
+ Iterator modulePrefsEltIterator = XMLTools.getChildrenIterator(moduleElt,
"ModulePrefs");
+ if (modulePrefsEltIterator.hasNext())
+ {
+ Element modulePrefsElt = (Element)modulePrefsEltIterator.next();
+
+ Iterator localeEltIterator = XMLTools.getChildrenIterator(modulePrefsElt,
"Locale");
+
+ Map messages = new HashMap();
+
+ while(localeEltIterator.hasNext())
+ {
+ Element localeElt = (Element)localeEltIterator.next();
+
+ String langAttr = localeElt.getAttribute("lang");
+ String countryAttr = localeElt.getAttribute("country");
+ String messagesAttr = localeElt.getAttribute("messages");
+
+ //as portal spec defines fallback to EN we'll use it for all_ALL gg
bundle
+ if (langAttr == null || langAttr.length() == 0)
+ {
+ langAttr = "en";
+ }
+
+ Locale locale;
+ if (countryAttr == null || countryAttr.length() == 0)
+ {
+ locale = new Locale(langAttr);
+ }
+ else
+ {
+ locale = new Locale(langAttr, countryAttr);
+ }
+
+
+
+ URL bundleURL = new URL(url, messagesAttr);
+
+ byte[] bundleBytes = obtainLocaleBundle(bundleURL);
+
+ //if no response obtained - ignore this bundle
+ if (bundleBytes == null)
+ {
+ continue;
+ }
+
+ DocumentBuilder bundleDocBuilder = factory.newDocumentBuilder();
+ Document bundleDoc = bundleDocBuilder.parse(new
ByteArrayInputStream(bundleBytes));
+ Element messagebundleElt = bundleDoc.getDocumentElement();
+
+ Iterator messagesEltIterator = XMLTools.getChildrenIterator(messagebundleElt,
"msg");
+
+ while(messagesEltIterator.hasNext())
+ {
+ Element msgElt = (Element)messagesEltIterator.next();
+
+ String name = msgElt.getAttribute("name");
+
+ StringBuffer value = new StringBuffer();
+
+ //create value from the tag content
+ NodeList nodes = msgElt.getChildNodes();
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Node n = nodes.item(i);
+
+ if (n.getNodeType() == Element.TEXT_NODE) {
+ value.append(n.getNodeValue());
+ }
+ }
+
+
+ Map entries;
+ if (!messages.containsKey(name))
+ {
+ entries = new HashMap();
+ messages.put(name, entries);
+ }
+ else
+ {
+ entries = (Map)messages.get(name);
+ }
+
+ entries.put(locale, value.toString());
+ }
+ }
+
+ //Reiterate whole bundle to store LocalizedString values;
+ for (Iterator iterator = messages.keySet().iterator(); iterator.hasNext();)
+ {
+ String name = (String)iterator.next();
+ Map entries = (Map)messages.get(name);
+
+ messagesBundle.put(name, new LocalizedString(entries, Locale.ENGLISH));
+ }
+
+ }
+ else
+ {
+ throw new Exception(); // Basic for now
+ }
+
+
//
Collection tmp = null;
Iterator userPrefsEltIterator = XMLTools.getChildrenIterator(moduleElt,
"UserPref");
@@ -141,7 +249,8 @@
Element enumValueElt = (Element)i.next();
String valueAttr = enumValueElt.getAttribute("value");
String displayValueAttr =
enumValueElt.getAttribute("display_value");
- EnumType.Value value = new EnumType.Value(valueAttr,
displayValueAttr.length() > 0 ? displayValueAttr : null);
+
+ EnumType.Value value = new EnumType.Value(valueAttr,
resolveLocalizedValue(displayValueAttr, messagesBundle));
values.add(value);
}
dataType = new EnumType(values);
@@ -153,10 +262,12 @@
}
//
+
+
GGPreferenceInfo prefInfo = new GGPreferenceInfo(
nameAttr,
dataType,
- displayNameAttr.length() > 0 ? displayNameAttr : null,
+ resolveLocalizedValue(displayNameAttr, messagesBundle),
requiredAttr.length() > 0 ? Boolean.valueOf(requiredAttr).booleanValue() :
false,
defaultValueAttr.length() > 0 ? defaultValueAttr : null
);
@@ -174,21 +285,25 @@
prefsInfo = new GGPreferencesInfo(tmp);
}
- Iterator modulePrefsEltIterator = XMLTools.getChildrenIterator(moduleElt,
"ModulePrefs");
+ modulePrefsEltIterator = XMLTools.getChildrenIterator(moduleElt,
"ModulePrefs");
if (modulePrefsEltIterator.hasNext())
{
Element modulePrefsElt = (Element)modulePrefsEltIterator.next();
String titleAttr = modulePrefsElt.getAttribute("title");
+ String directoryTitleAttr =
modulePrefsElt.getAttribute("directory_title");
String descriptionAttr = modulePrefsElt.getAttribute("description");
String widthAttr = modulePrefsElt.getAttribute("width");
String heightAttr = modulePrefsElt.getAttribute("height");
//
- LocalizedString title = titleAttr != null ? new LocalizedString(titleAttr,
Locale.ENGLISH) : null;
- LocalizedString description = descriptionAttr != null ? new
LocalizedString(descriptionAttr, Locale.ENGLISH) : null;
+ LocalizedString title = resolveLocalizedStringValue(titleAttr, messagesBundle);
+ LocalizedString directoryTitle = resolveLocalizedStringValue(directoryTitleAttr,
messagesBundle);
+ LocalizedString description = resolveLocalizedStringValue(descriptionAttr,
messagesBundle);
+
+
int width = (widthAttr != null && widthAttr.length() > 0) ?
Integer.parseInt(widthAttr) : 320;
int height = (heightAttr != null && heightAttr.length() > 0) ?
Integer.parseInt(heightAttr) : 200;
- return new GGWidgetInfo(title, description, width, height, prefsInfo);
+ return new GGWidgetInfo(title, directoryTitle, description, width, height,
prefsInfo);
}
else
{
@@ -196,8 +311,61 @@
}
}
+ private LocalizedString resolveLocalizedStringValue(String value, Map bundle)
+ {
+ if(value != null)
+ {
+ if (value.startsWith("__MSG_"))
+ {
+ String name = value.substring("__MSG_".length(), value.length() -
2);
+
+ if (bundle.containsKey(name))
+ {
+ return (LocalizedString)bundle.get(name);
+ }
+ }
+
+ return new LocalizedString(value, Locale.ENGLISH);
+ }
+
+ return null;
+
+ }
+
+ private String resolveLocalizedValue(String value, Map bundle)
+ {
+ LocalizedString out = resolveLocalizedStringValue(value, bundle);
+
+ if (out != null)
+ {
+ return out.getDefaultString();
+ }
+
+ return null;
+
+ }
+
+
+
+ /**
+ * needed for testing
+ */
protected byte[] obtainWidget(URL url) throws Exception
{
+ return obtainURL(url);
+ }
+
+ /**
+ * needed for testing
+ */
+ protected byte[] obtainLocaleBundle(URL url) throws Exception
+ {
+ return obtainURL(url);
+ }
+
+ private byte[] obtainURL(URL url) throws Exception
+ {
return URLTools.performGET(url, 5000, 5000);
}
+
}