portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r332150 - in /portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl: LanguageImpl.java LanguageSetImpl.java
Date Wed, 09 Nov 2005 21:35:46 GMT
Author: ate
Date: Wed Nov  9 13:35:19 2005
New Revision: 332150

URL: http://svn.apache.org/viewcvs?rev=332150&view=rev
Log:
Major rewrite of the Language and LanguageSet implementations to get them in line with the
portlet specs.
Now the (possibly merged) title, shortTitle and keywords are actually retrievable from the
resourceBundle itself.

Modified:
    portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageImpl.java
    portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageSetImpl.java

Modified: portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageImpl.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageImpl.java?rev=332150&r1=332149&r2=332150&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageImpl.java
(original)
+++ portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageImpl.java
Wed Nov  9 13:35:19 2005
@@ -20,15 +20,14 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Locale;
-import java.util.Map;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 import java.util.StringTokenizer;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.jetspeed.om.common.MutableLanguage;
 import org.apache.jetspeed.util.HashCodeBuilder;
 import org.apache.jetspeed.util.JetspeedLocale;
@@ -46,20 +45,22 @@
  * that contains a localized title and short title.
  * 
  * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
+ * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
  * @version $Id$
  *  
  */
-public class LanguageImpl implements MutableLanguage, Serializable
+public class LanguageImpl extends ResourceBundle implements MutableLanguage, Serializable
 {
-
     public static final String JAVAX_PORTLET_KEYWORDS = "javax.portlet.keywords";
     public static final String JAVAX_PORTLET_SHORT_TITLE = "javax.portlet.short-title";
     public static final String JAVAX_PORTLET_TITLE = "javax.portlet.title";
-    private Locale locale; // new Locale("en");
+
+    private HashSet keys;
     private String title;
     private String shortTitle;
+    private Locale locale;
+    private String keywordStr;
     private Collection keywords;
-    private ResourceBundle resourceBundle;
 
     /**
      * This field can be used by persistence tools for storing PK info Otherwise
@@ -71,39 +72,75 @@
 
     public LanguageImpl()
     {
-        this(JetspeedLocale.getDefaultLocale(), null, "", "", "");
+        keys = new HashSet(3);
+        keys.add(JAVAX_PORTLET_TITLE);
+        keys.add(JAVAX_PORTLET_SHORT_TITLE);
+        keys.add(JAVAX_PORTLET_KEYWORDS);
+        this.locale = JetspeedLocale.getDefaultLocale();
     }
-
-    public LanguageImpl( Locale locale, String title )
+    
+    public Enumeration getKeys()
     {
-        this(locale, null, title, "", "");
+        return Collections.enumeration(keys);
     }
-
-    public LanguageImpl( Locale locale, ResourceBundle bundle, String defaultTitle, String
defaultShortTitle,
-            String defaultKeyWords )
+    
+    protected Object handleGetObject(String key)
     {
-        HashMap defaults = new HashMap(3);
-        defaults.put(JAVAX_PORTLET_TITLE, defaultTitle);
-        defaults.put(JAVAX_PORTLET_SHORT_TITLE, defaultShortTitle);
-        defaults.put(JAVAX_PORTLET_KEYWORDS, defaultKeyWords);
-        this.resourceBundle = new DefaultsResourceBundle(bundle, defaults);
-
-        setLocale(locale);
-        setTitle(getResourceBundle().getString(JAVAX_PORTLET_TITLE));
-        setShortTitle(getResourceBundle().getString(JAVAX_PORTLET_SHORT_TITLE));
-        setKeywords(getResourceBundle().getString(JAVAX_PORTLET_KEYWORDS));
+        if (key.equals(JAVAX_PORTLET_TITLE))
+        {
+            return getTitle();
+        }
+        else if (key.equals(JAVAX_PORTLET_SHORT_TITLE))
+        {
+            return getShortTitle();
+        }
+        else if (key.equals(JAVAX_PORTLET_KEYWORDS))
+        {
+            return getKeywordStr();
+        }
+        return null;
     }
-
-    public LanguageImpl( Locale locale, ResourceBundle bundle )
+    
+    private String getStringValue(ResourceBundle bundle, String key, String defaultValue)
     {
-        this.resourceBundle = new DefaultsResourceBundle(bundle, new HashMap());
-
-        setLocale(locale);
-        setTitle(getResourceBundle().getString(JAVAX_PORTLET_TITLE));
-        setShortTitle(getResourceBundle().getString(JAVAX_PORTLET_SHORT_TITLE));
-        setKeywords(getResourceBundle().getString(JAVAX_PORTLET_KEYWORDS));
+        String value = defaultValue;
+        try
+        {
+            value = (String)bundle.getObject(key);
+        }
+        catch (MissingResourceException mre)
+        {            
+        }
+        catch (ClassCastException cce)
+        {            
+        }
+        return value;
     }
-
+    
+    public void setResourceBundle(ResourceBundle bundle)
+    {
+        if ( parent == null && bundle != null )
+        {
+            Enumeration parentKeys = bundle.getKeys();
+            while ( parentKeys.hasMoreElements() )
+            {
+                keys.add(parentKeys.nextElement());
+            }
+            setParent(bundle);
+        }
+    }
+    
+    public void loadDefaults()
+    {
+        ResourceBundle bundle = getParentResourceBundle();
+        if ( bundle != null )
+        {
+            setTitle(getStringValue(bundle, JAVAX_PORTLET_TITLE, getTitle()));
+            setShortTitle(getStringValue(bundle, JAVAX_PORTLET_SHORT_TITLE, getShortTitle()));
+            setKeywords(getStringValue(bundle, JAVAX_PORTLET_TITLE, getKeywordStr()));
+        }
+    }
+    
     /**
      * @see org.apache.pluto.om.common.Language#getLocale()
      */
@@ -117,11 +154,6 @@
      */
     public String getTitle()
     {
-        if(title == null)
-        {
-            title = resourceBundle.getString(JAVAX_PORTLET_TITLE);
-        }
-        
         return title;
     }
 
@@ -130,12 +162,7 @@
      */
     public String getShortTitle()
     {
-        if(shortTitle == null)
-        {
-            shortTitle = resourceBundle.getString(JAVAX_PORTLET_SHORT_TITLE);
-        }
-        
-        return shortTitle;        
+        return shortTitle;
     }
 
     /**
@@ -143,11 +170,10 @@
      */
     public Iterator getKeywords()
     {
-        if (keywords == null)
+        if ( keywords == null )
         {
-            setKeywords(resourceBundle.getString(JAVAX_PORTLET_KEYWORDS));
+            return Collections.EMPTY_LIST.iterator();
         }
-
         return keywords.iterator();
     }
 
@@ -157,16 +183,20 @@
     public ResourceBundle getResourceBundle()
     {
 
-        return resourceBundle;
+        return this;
     }
-
+    
+    public ResourceBundle getParentResourceBundle()
+    {
+        return parent;
+    }
+    
     /**
      * @see org.apache.pluto.om.common.LanguageCtrl#setLocale(java.util.Locale)
      */
     public void setLocale( Locale locale )
     {
         this.locale = locale;
-
     }
 
     /**
@@ -180,32 +210,9 @@
     /**
      * @see org.apache.pluto.om.common.LanguageCtrl#setShortTitle(java.lang.String)
      */
-    public void setShortTitle( String title )
-    {
-        this.shortTitle = title;
-    }
-
-    /**
-     * @see java.lang.Object#equals(java.lang.Object)
-     */
-    public boolean equals( Object obj )
-    {
-        if (obj != null && obj instanceof Language)
-        {
-            return obj.hashCode() == this.hashCode();
-        }
-
-        return false;
-    }
-
-    /**
-     * @see java.lang.Object#hashCode()
-     */
-    public int hashCode()
+    public void setShortTitle( String shortTitle )
     {
-        HashCodeBuilder hasher = new HashCodeBuilder(19, 79);
-        hasher.append(locale.getCountry()).append(locale.getLanguage()).append(locale.getVariant());
-        return hasher.toHashCode();
+        this.shortTitle = shortTitle;
     }
 
     /**
@@ -215,119 +222,59 @@
     {
         this.keywords = keywords;
     }
-
-    /**
-     * 
-     * <p>
-     * setKeywords
-     * </p>
-     * 
-     * A comma delimited list of keywords
-     * 
-     * @param keywords
-     *  
-     */
-    public void setKeywords( String keywordStr )
+    
+    public void setKeywords(String keywordStr)
     {
         if (keywords == null)
         {
             keywords = new ArrayList();
         }
+        else
+        {
+            keywords.clear();
+        }
+        if ( keywordStr == null )
+        {
+            keywordStr = "";
+        }
         StringTokenizer tok = new StringTokenizer(keywordStr, ",");
         while (tok.hasMoreTokens())
         {
             keywords.add(tok.nextToken());
         }
+        this.keywordStr = keywordStr;
     }
-
-    private static class DefaultsResourceBundle extends ResourceBundle
+    
+    public String getKeywordStr()
     {
-        private ResourceBundle baseBundle;
-        private Map defaultValues;
-
-        public DefaultsResourceBundle( ResourceBundle baseBundle, Map defaultValues )
+        if ( keywordStr == null )
         {
-            this.baseBundle = baseBundle;
-            this.defaultValues = defaultValues;
-        }
-
-        /**
-         * <p>
-         * getKeys
-         * </p>
-         * 
-         * @see java.util.ResourceBundle#getKeys()
-         * @return
-         */
-        public Enumeration getKeys()
-        {
-            if (baseBundle != null)
-            {
-                Enumeration baseKeys = baseBundle.getKeys();
-                HashSet mergedKeys = new HashSet(defaultValues.keySet());
-                while (baseKeys.hasMoreElements())
-                {
-                    mergedKeys.add(baseKeys.nextElement());
-                }
-                return Collections.enumeration(mergedKeys);
-            }
-            else
-            {
-                return Collections.enumeration(defaultValues.keySet());
-            }
+            keywordStr = StringUtils.join(getKeywords(),",");
         }
+        return keywordStr;
+    }
 
-        /**
-         * <p>
-         * handleGetObject
-         * </p>
-         * 
-         * @see java.util.ResourceBundle#handleGetObject(java.lang.String)
-         * @param key
-         * @return
-         */
-        protected Object handleGetObject( String key )
+    /**
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    public boolean equals( Object obj )
+    {
+        if (obj != null && obj instanceof Language)
         {
-
-            try
-            {
-                if(baseBundle != null)
-                {
-                    return baseBundle.getObject(key);
-                }
-                else
-                {
-                    return getDefaultValue(key);
-                }
-            }
-            catch (MissingResourceException e)
-            {
-                return getDefaultValue(key);
-            }
+            return obj.hashCode() == this.hashCode();
         }
 
-        /**
-         * <p>
-         * getDefaultValue
-         * </p>
-         *
-         * @param key
-         * @return
-         */
-        protected Object getDefaultValue( String key )
-        {
-            Object value = defaultValues.get(key);
-            if (value != null)
-            {
-                return value;
-            }
-            else
-            {
-                return "";
-            }
-        }
+        return false;
     }
-    
-    
 
+    /**
+     * @see java.lang.Object#hashCode()
+     */
+    public int hashCode()
+    {
+        HashCodeBuilder hasher = new HashCodeBuilder(19, 79);
+        Locale locale = getLocale();
+        hasher.append(locale.getCountry()).append(locale.getLanguage()).append(locale.getVariant());
+        return hasher.toHashCode();
+    }
 }

Modified: portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageSetImpl.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageSetImpl.java?rev=332150&r1=332149&r2=332150&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageSetImpl.java
(original)
+++ portals/jetspeed-2/trunk/components/registry/src/java/org/apache/jetspeed/om/impl/LanguageSetImpl.java
Wed Nov  9 13:35:19 2005
@@ -24,7 +24,6 @@
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 
-import org.apache.commons.lang.StringUtils;
 import org.apache.jetspeed.om.common.MutableLanguage;
 import org.apache.jetspeed.om.common.Support;
 import org.apache.jetspeed.util.JetspeedLocale;
@@ -36,12 +35,12 @@
  * LanguageSetImpl
  * 
  * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a>
+ * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
  * @version $Id$
  *  
  */
 public class LanguageSetImpl implements LanguageSet, Serializable, Support
 {
-
     private ClassLoader classLoader = null;
 
     private String resources;
@@ -91,58 +90,61 @@
      */
     public Language get( Locale locale )
     {
-
-        Language fallBack = null;
+        LanguageImpl fallback = null;
         Iterator searchItr = innerCollection.iterator();
         while (searchItr.hasNext())
         {
-            Language lang = (Language) searchItr.next();
+            LanguageImpl lang = (LanguageImpl)searchItr.next();
 
             if (lang.getLocale().equals(locale))
             {
-                if (resources != null)
-                {
-                    LanguageImpl language = (LanguageImpl)createLanguage(lang.getLocale(),
loadResourceBundle(lang.getLocale()),"","","");
-                    language.setTitle(lang.getTitle());
-                    language.setShortTitle(lang.getShortTitle());
-                    language.setKeywords(StringUtils.join(lang.getKeywords(), ","));
-                    return language;
-                }
-                else
+                if (resources != null && lang.getParentResourceBundle() == null)
                 {
-                    return lang;
+                    lang.setResourceBundle(loadResourceBundle(lang.getLocale()));
                 }
+                return lang;
             }
             else if (lang.getLocale().getLanguage().equals(locale.getLanguage()))
             {
-                fallBack = lang;
+                fallback = lang;
             }
 
         }
-
-        if (fallBack != null && resources != null)
-        {
-            LanguageImpl language = (LanguageImpl)createLanguage(fallBack.getLocale(), loadResourceBundle(fallBack.getLocale()),"","","");
-            language.setTitle(fallBack.getTitle());
-            language.setShortTitle(fallBack.getShortTitle());
-            language.setKeywords(StringUtils.join(fallBack.getKeywords(), ","));
-            fallBack = language;
-        }
-
-        if (fallBack == null)
+        
+        if ( fallback == null )
         {
-            if(!getDefaultLocale().equals(locale))
+            if ( getDefaultLocale().equals(locale) )
             {
-                Language defaultLang=get(getDefaultLocale());
-                fallBack = new LanguageImpl(locale, loadResourceBundle(locale), defaultLang.getTitle(),
defaultLang.getShortTitle(), StringUtils.join(defaultLang.getKeywords(), ","));
+                // no default language stored yet
+                LanguageImpl defaultLanguage = new LanguageImpl();
+                defaultLanguage.setLocale(locale);
+                
+                if ( resources != null )
+                {
+                    defaultLanguage.setResourceBundle(loadResourceBundle(locale));
+                    defaultLanguage.loadDefaults();
+                    innerCollection.add(defaultLanguage);
+                    return defaultLanguage;
+                }
             }
             else
             {
-                fallBack = new LanguageImpl(locale, loadResourceBundle(locale), "","","");
+                fallback = (LanguageImpl)get(getDefaultLocale());
             }
-            innerCollection.add(fallBack);
         }
-        return fallBack;
+        
+        LanguageImpl language = new LanguageImpl();
+        language.setLocale(locale);
+        language.setTitle(fallback.getTitle());
+        language.setShortTitle(fallback.getShortTitle());
+        language.setKeywords(fallback.getKeywordStr());
+        if ( resources != null )
+        {
+          language.setResourceBundle(loadResourceBundle(locale));
+        }
+        language.loadDefaults();
+        innerCollection.add(language);
+        return language;
     }
 
     /**
@@ -153,51 +155,27 @@
         return JetspeedLocale.getDefaultLocale();
     }
 
-    /**
-     * @see java.util.Collection#add(java.lang.Object)
-     */
     public boolean add( Object o )
     {
         if (o instanceof Language)
         {
-        Language language = (Language) o;
-        if (language.getLocale() == null)
-        {
-                ((MutableLanguage) o).setLocale(getDefaultLocale());
-        }
-
-            Iterator ite = innerCollection.iterator();
-            while (ite.hasNext())
+            Language language = (Language) o;
+            if (language.getLocale() == null)
             {
-                Language lang = (Language) ite.next();
-                if (lang.getLocale().equals(language.getLocale()))
-                {
-                    innerCollection.remove(lang);
-        return innerCollection.add(o);
-    }
+                ((MutableLanguage) o).setLocale(getDefaultLocale());
             }
-            return innerCollection.add(o);
-        }
-        return false;
-    }
 
-    /**
-     * @see java.util.Collection#remove(java.lang.Object)
-     */
-    public boolean remove(Object o)
-    {
-        if (o instanceof Language)
-    {
-        Language language = (Language) o;
             Iterator ite = innerCollection.iterator();
             while (ite.hasNext())
             {
                 Language lang = (Language) ite.next();
-                if (lang.getLocale().equals(language.getLocale()))
+                if (lang.equals(language))
                 {
-                    return innerCollection.remove(lang);
+                    innerCollection.remove(lang);
+                    return innerCollection.add(o);
                 }
             }
+            return innerCollection.add(o);
         }
         return false;
     }
@@ -238,34 +216,14 @@
      */
     public void postLoad( Object parameter ) throws Exception
     {
-        String title = "";
-        String shortTitle = "";
-        String keywords = "";
-        Language defaultLang = get(getDefaultLocale());
-        if (defaultLang != null)
-        {
-            title = defaultLang.getTitle();
-            shortTitle = defaultLang.getShortTitle();
-            keywords = StringUtils.join(defaultLang.getKeywords(), ",");
-        }
-
-        ResourceBundle defaultResource = loadResourceBundle(getDefaultLocale());
         Iterator iter = ((Collection) parameter).iterator();
         while (iter.hasNext())
         {
-            Locale locale = (Locale) iter.next();
-            ResourceBundle resource = loadResourceBundle(locale);
-            if (resource == null)
-            {
-                resource = defaultResource;
-            }
-            Language language = createLanguage(locale, resource, title, shortTitle, keywords);
-            add(language);
+            LanguageImpl language = (LanguageImpl)get((Locale)iter.next());
+            language.loadDefaults();
         }
-
-        // add default Language
-        Language language = createLanguage(getDefaultLocale(), defaultResource, title, shortTitle,
keywords);
-        add(language);
+        // ensure default locale language is created
+        get(getDefaultLocale());
     }
 
     protected ResourceBundle loadResourceBundle( Locale locale )
@@ -302,21 +260,5 @@
     public void setClassLoader( ClassLoader loader )
     {
         classLoader = loader;
-    }
-
-    /**
-     * Creates Language instance.
-     * 
-     * @param locale
-     * @param bundle
-     * @param title
-     * @param shortTitle
-     * @param keywords
-     * @return
-     */
-    private Language createLanguage( Locale locale, ResourceBundle bundle, String title,
String shortTitle, String keywords)
-    {
-        LanguageImpl lang = new LanguageImpl(locale, bundle, title, shortTitle, keywords);
-        return (Language) lang;
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message