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 static final long serialVersionUID = 4949984721486410191L; 040 041 private String basename; 042 private String var; 043 044 public int doStartTag() throws JspException { 045 046 String bundleBaseName; 047 FacesContext context = FacesContext.getCurrentInstance(); 048 if (UIComponentTag.isValueReference(basename)) { 049 bundleBaseName = (String) context.getApplication().createValueBinding(basename).getValue(context); 050 } else { 051 bundleBaseName = basename; 052 } 053 Map toStore = new BundleMapWrapper(bundleBaseName); 054 // TODO find a better way 055 context.getExternalContext().getSessionMap().put(var, toStore); 056 // .getRequestMap().put(var, toStore); 057 058 return EVAL_BODY_INCLUDE; 059 } 060 061 public void release() { 062 basename = null; 063 var = null; 064 } 065 066 public String getBasename() { 067 return basename; 068 } 069 070 /** 071 * Base name of the resource bundle to be loaded. 072 */ 073 @TagAttribute(required = true) 074 public void setBasename(String basename) { 075 this.basename = basename; 076 } 077 078 public String getVar() { 079 return var; 080 } 081 /** 082 * 083 * Name of a session-scope attribute under which the bundle data 084 * will be exposed. 085 * 086 */ 087 @TagAttribute(required = true) 088 public void setVar(String var) { 089 this.var = var; 090 } 091 092 } 093