Coverage Report - org.seasar.teeda.core.taglib.core.LoadBundleTag
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadBundleTag
91%
29/32
50%
3/6
1.833
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.taglib.core;
 17  
 
 18  
 import java.util.Locale;
 19  
 import java.util.MissingResourceException;
 20  
 import java.util.ResourceBundle;
 21  
 
 22  
 import javax.faces.component.UIViewRoot;
 23  
 import javax.faces.context.FacesContext;
 24  
 import javax.faces.el.ValueBinding;
 25  
 import javax.faces.internal.ValueBindingUtil;
 26  
 import javax.faces.webapp.UIComponentTag;
 27  
 import javax.servlet.jsp.JspException;
 28  
 import javax.servlet.jsp.tagext.Tag;
 29  
 import javax.servlet.jsp.tagext.TagSupport;
 30  
 
 31  
 import org.seasar.framework.log.Logger;
 32  
 import org.seasar.framework.util.AssertionUtil;
 33  
 import org.seasar.teeda.core.util.ResourceBundleMap;
 34  
 
 35  
 /**
 36  
  * @author shot
 37  
  * @author yone
 38  
  */
 39  5
 public class LoadBundleTag extends TagSupport {
 40  
 
 41  2
     private static Logger logger = Logger.getLogger(LoadBundleTag.class);
 42  
 
 43  
     private static final long serialVersionUID = 1L;
 44  
 
 45  
     private String basename;
 46  
 
 47  
     private String var;
 48  
 
 49  
     public void setBasename(String basename) {
 50  5
         this.basename = basename;
 51  5
     }
 52  
 
 53  
     public void setVar(String var) {
 54  4
         this.var = var;
 55  4
     }
 56  
 
 57  
     public String getBasename() {
 58  5
         return basename;
 59  
     }
 60  
 
 61  
     public String getVar() {
 62  1
         return var;
 63  
     }
 64  
 
 65  
     public int doStartTag() throws JspException {
 66  4
         FacesContext context = FacesContext.getCurrentInstance();
 67  4
         AssertionUtil.assertNotNull("FacesContext", context);
 68  
 
 69  4
         UIViewRoot viewRoot = context.getViewRoot();
 70  4
         AssertionUtil.assertNotNull("ViewRoot", viewRoot);
 71  
 
 72  4
         Locale locale = viewRoot.getLocale();
 73  4
         if (locale == null) {
 74  0
             locale = context.getApplication().getDefaultLocale();
 75  
         }
 76  4
         String bname = null;
 77  4
         final String basename = getBasename();
 78  4
         if (basename != null) {
 79  4
             if (UIComponentTag.isValueReference(basename)) {
 80  0
                 ValueBinding vb = ValueBindingUtil.createValueBinding(context,
 81  
                         basename);
 82  0
                 bname = (String) vb.getValue(context);
 83  
             } else {
 84  4
                 bname = basename;
 85  
             }
 86  
         }
 87  
         final ResourceBundle bundle;
 88  
         try {
 89  4
             bundle = ResourceBundle.getBundle(bname, locale, Thread
 90  
                     .currentThread().getContextClassLoader());
 91  1
         } catch (MissingResourceException e) {
 92  1
             logger.error("Resource bundle '" + bname + "' could not be found");
 93  1
             return Tag.SKIP_BODY;
 94  3
         }
 95  
 
 96  3
         context.getExternalContext().getRequestMap().put(var,
 97  
                 new ResourceBundleMap(bundle));
 98  
 
 99  3
         return Tag.SKIP_BODY;
 100  
     }
 101  
 
 102  
     public void release() {
 103  1
         basename = null;
 104  1
         var = null;
 105  1
     }
 106  
 
 107  
 }