You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by gv...@apache.org on 2005/09/14 05:25:17 UTC

svn commit: r280753 - in /struts/shale/trunk/clay-plugin/src: conf/faces-config.xml conf/view-config.xml java/org/apache/shale/clay/component/LoadBundle.java

Author: gvanmatre
Date: Tue Sep 13 20:25:05 2005
New Revision: 280753

URL: http://svn.apache.org/viewcvs?rev=280753&view=rev
Log:
Added a loadBundle component for full HTML views.

Added:
    struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/LoadBundle.java
Modified:
    struts/shale/trunk/clay-plugin/src/conf/faces-config.xml
    struts/shale/trunk/clay-plugin/src/conf/view-config.xml

Modified: struts/shale/trunk/clay-plugin/src/conf/faces-config.xml
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/conf/faces-config.xml?rev=280753&r1=280752&r2=280753&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/conf/faces-config.xml (original)
+++ struts/shale/trunk/clay-plugin/src/conf/faces-config.xml Tue Sep 13 20:25:05 2005
@@ -48,5 +48,16 @@
     <component-type>org.apache.shale.clay.component.Clay</component-type>
     <component-class>org.apache.shale.clay.component.Clay</component-class>
   </component>
+
+  <component>
+    <description>
+      The "LoadBundle" component is meant to simulate the JSF loadBundle 
+      JSP tag for full Clay HTML views.
+    </description>
+    <display-name>LoadBundle</display-name>
+    <component-type>org.apache.shale.clay.component.LoadBundle</component-type>
+    <component-class>org.apache.shale.clay.component.LoadBundle</component-class>
+  </component>
+
 
 </faces-config>

Modified: struts/shale/trunk/clay-plugin/src/conf/view-config.xml
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/conf/view-config.xml?rev=280753&r1=280752&r2=280753&view=diff
==============================================================================
--- struts/shale/trunk/clay-plugin/src/conf/view-config.xml (original)
+++ struts/shale/trunk/clay-plugin/src/conf/view-config.xml Tue Sep 13 20:25:05 2005
@@ -215,5 +215,15 @@
       <set name="shapeValidator" useMethodLateBinding="true"/> 
     </attributes>
   </component>	
+
+  <!-- Clay LoadBundle Component -->    
+  <component jsfid="loadBundle" componentType="org.apache.shale.clay.component.LoadBundle" 
+       allowBody="false">
+    <attributes>
+      <set name="basename" />
+      <set name="scope" value="request"/>
+      <set name="var" value="msgs"/> 
+    </attributes>
+  </component>	
 
 </view>

Added: struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/LoadBundle.java
URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/LoadBundle.java?rev=280753&view=auto
==============================================================================
--- struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/LoadBundle.java (added)
+++ struts/shale/trunk/clay-plugin/src/java/org/apache/shale/clay/component/LoadBundle.java Tue Sep 13 20:25:05 2005
@@ -0,0 +1,216 @@
+/*
+ * Copyright 2005 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.
+ *
+ * $Id$
+ */
+
+package org.apache.shale.clay.component;
+
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Map;
+import java.util.ResourceBundle;
+import java.util.TreeMap;
+
+import javax.faces.component.UIComponentBase;
+import javax.faces.context.FacesContext;
+import javax.faces.el.ValueBinding;
+
+/**
+ * <p>This component is meant to simulate the JSF loadBundle JSP tag for full 
+ * {@link Clay} HTML views.  It adds a scope property not found in the JSF JSP RI Tag.  
+ * This component is not immune to the rendered property like the JSP loadBundle tag.  
+ * This means that if this component is nested in a component and the rendered attribute 
+ * is false, the message bundle might not get loaded into the specified scope.  
+ * </p>
+ */
+public class LoadBundle extends UIComponentBase {
+
+    /**
+     * <p>Base bundle name of the target properties file.</p>
+     */
+    private String basename = null;
+    
+    /**
+     * <p>Key that the bundle will be loaded into session scope.</p>
+     */
+    private String var = null;
+    
+    /**
+     * <p>The scope the {@link org.apache.shale.clay.component.LoadBundle$BundleMap} 
+     * will be cached using the <code>var</code> as a key.  The default
+     * is "request" scope.</p>
+     */
+    private String scope = "request";
+    
+    
+    
+    /**
+     * <p>This component is not associated with a family.  
+     * Returns <code>null</code>.</p>
+     */
+    public String getFamily() {
+        return null;
+    }
+
+    /**
+     * <p>This component doesn't have a renderer and
+     * returns <code>null</code></p>
+     */
+    public String getRendererType() {
+       return null;
+    }
+
+    /**
+     *  <p>Resource bundle wrappered in a Map.</p>
+     */
+    private class BundleMap extends TreeMap {
+        
+        /**
+         * <p>Loads the resource bundle into a Map.</p> 
+         */
+        public BundleMap(ResourceBundle bundle) {
+           Enumeration ki = bundle.getKeys();
+           while (ki.hasMoreElements()) {
+               String key = (String) ki.nextElement();
+               String value = bundle.getString(key);
+               super.put(key, value);
+           }
+           
+        }
+
+        /**
+         * <p>Returns the message string for the key.  If the key doesn't
+         * exist, a friendly value "?key?" is returned for debuging.</p>
+         */
+        public Object get(Object key) {
+          
+            String value =  (String) super.get(key);
+            if (value == null) 
+               value = "?" + key + "?";
+            
+            return value;
+        }
+                 
+    };
+    
+    /**
+     * <p>Loads the resource bundle identified by the <code>basename</code>
+     * into <code>scope</code> using the <code>var</code> property as a key.</p>
+     */
+    public void encodeBegin(FacesContext context) throws IOException {
+        super.encodeBegin(context);
+          
+        StringBuffer targetExp = new StringBuffer();
+        targetExp.append("#{").append(getScope())
+                 .append("Scope").append("['")
+                 .append(getVar()).append("']}");
+        
+        ValueBinding vb = context.getApplication().createValueBinding(targetExp.toString());
+        
+        Map mapBundle = (Map) vb.getValue(context);
+        
+        if (mapBundle == null) {
+            Locale locale = context.getViewRoot().getLocale();
+            if (locale == null)
+               locale = context.getApplication().getDefaultLocale();
+            
+            ClassLoader classloader = Thread.currentThread().getContextClassLoader();
+            if(classloader == null)
+                classloader = this.getClass().getClassLoader();
+            
+            ResourceBundle bundle = ResourceBundle.getBundle(getBasename(), locale, classloader);
+            
+            mapBundle = new BundleMap(bundle);
+        }
+
+        vb.setValue(context, mapBundle);
+        
+    }
+
+    /**
+     * <p>Returns the name of the resource bundle.</p>
+     */
+    public String getBasename() {
+        return basename;
+    }
+    
+
+    /**
+     * <p>Sets the resource bundle name.</p>
+     */
+    public void setBasename(String basename) {
+        this.basename = basename;
+    }
+    
+
+    /**
+     * <p>Returns the key name the 
+     * {@link org.apache.shale.clay.component.LoadBundle$BundleMap} will be
+     * cached in <code>scope</code>.</p>
+     */
+    public String getVar() {
+        return var;
+    }
+    
+
+    /**
+     * <p>Sets the key name the 
+     * {@link org.apache.shale.clay.component.LoadBundle$BundleMap} will be
+     * cached in <code>scope</code>.</p>
+     */
+    public void setVar(String var) {
+        this.var = var;
+    }
+
+    /**
+     * <p>Returns the scope the 
+     * {@link org.apache.shale.clay.component.LoadBundle$BundleMap} will be cached.
+     * The valid enumerations are (request, session and application). 
+     * The default is "request" if a value is not specified.</p>
+     */
+    public String getScope() {
+        if (scope == null)
+          return "request";
+        else if (scope.equals("request") ||
+                 scope.equals("session") ||
+                 scope.equals("application")) {
+           return scope;
+        } else {
+           return "request";
+        }
+          
+    }
+    
+
+    /**
+    * <p>Sets the scope the 
+    * {@link org.apache.shale.clay.component.LoadBundle$BundleMap} will be cached.
+    * The valid enumerations are (request, session and application).
+    * </p>
+    */
+    public void setScope(String scope) {
+        this.scope = scope;
+    }
+
+    /**
+     * <p>Force the rendered property to <code>true</code>.</p>
+     */
+    public boolean isRendered() {    
+        return true;
+    }
+     
+}



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