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