001    package org.apache.myfaces.tobago.taglib.component;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.apt.annotation.BodyContent;
021    import org.apache.myfaces.tobago.apt.annotation.Tag;
022    import org.apache.myfaces.tobago.apt.annotation.TagAttribute;
023    import org.apache.myfaces.tobago.util.BundleMapWrapper;
024    
025    import javax.faces.context.FacesContext;
026    import javax.faces.webapp.UIComponentTag;
027    import javax.servlet.jsp.JspException;
028    import javax.servlet.jsp.tagext.TagSupport;
029    import java.util.Map;
030    
031    /**
032     * Load a resource bundle localized for the Locale of the current view
033     * from the tobago resource path, and expose it (as a Map) in the request
034     * attributes of the current request.
035     */
036    @Tag(name = "loadBundle", bodyContent = BodyContent.EMPTY)
037    public class LoadBundleTag extends TagSupport {
038    
039      private String basename;
040    
041      private String var;
042    
043      public int doStartTag() throws JspException {
044    
045        String bundleBaseName;
046        FacesContext context = FacesContext.getCurrentInstance();
047        if (UIComponentTag.isValueReference(basename)) {
048          bundleBaseName = (String) context.getApplication().createValueBinding(basename).getValue(context);
049        } else {
050          bundleBaseName = basename;
051        }
052        Map toStore = new BundleMapWrapper(bundleBaseName);
053        // TODO find a better way
054        context.getExternalContext().getSessionMap().put(var, toStore);
055    //        .getRequestMap().put(var, toStore);
056    
057        return EVAL_BODY_INCLUDE;
058      }
059    
060      public void release() {
061        basename = null;
062        var = null;
063      }
064    
065      public String getBasename() {
066        return basename;
067      }
068    
069      /**
070       * Base name of the resource bundle to be loaded.
071       */
072      @TagAttribute(required = true)
073      public void setBasename(String basename) {
074        this.basename = basename;
075      }
076    
077      public String getVar() {
078        return var;
079      }
080      /**
081       *
082       * Name of a session-scope attribute under which the bundle data
083       * will be exposed.
084       *
085       */
086      @TagAttribute(required = true)
087      public void setVar(String var) {
088        this.var = var;
089      }
090    
091    }
092