Coverage Report - org.seasar.teeda.core.el.impl.ValueBindingFactoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueBindingFactoryImpl
65%
17/26
40%
4/10
2
 
 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.el.impl;
 17  
 
 18  
 import java.util.Collections;
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.faces.application.Application;
 23  
 import javax.faces.component.StateHolder;
 24  
 import javax.faces.context.FacesContext;
 25  
 import javax.faces.el.ValueBinding;
 26  
 
 27  
 import org.seasar.framework.exception.EmptyRuntimeException;
 28  
 import org.seasar.framework.util.StringUtil;
 29  
 import org.seasar.teeda.core.el.ELParser;
 30  
 import org.seasar.teeda.core.el.ValueBindingFactory;
 31  
 
 32  
 /**
 33  
  * @author shot
 34  
  */
 35  
 public class ValueBindingFactoryImpl implements ValueBindingFactory {
 36  
 
 37  3
     private Map cache = Collections.synchronizedMap(new HashMap());
 38  
 
 39  
     private ELParser parser;
 40  
 
 41  3
     public ValueBindingFactoryImpl() {
 42  3
         this.parser = ELParserFactory.createELParser();
 43  3
     }
 44  
 
 45  
     public void setELParser(ELParser parser) {
 46  2
         this.parser = parser;
 47  2
     }
 48  
 
 49  
     public ELParser getELParser() {
 50  2
         return parser;
 51  
     }
 52  
 
 53  
     public ValueBinding createValueBinding(Application application,
 54  
             String expression) {
 55  2
         if (StringUtil.isEmpty(expression)) {
 56  0
             throw new EmptyRuntimeException("Expression string");
 57  
         }
 58  2
         if (application == null) {
 59  0
             throw new EmptyRuntimeException("Application");
 60  
         }
 61  2
         ValueBinding vb = getValueBindingFromCache(application, expression);
 62  2
         if (vb != null) {
 63  0
             return vb;
 64  
         }
 65  2
         vb = new ValueBindingImpl(application, expression, getELParser());
 66  2
         cache.put(expression, vb);
 67  2
         return vb;
 68  
     }
 69  
 
 70  
     public void clearCache() {
 71  0
         cache.clear();
 72  0
     }
 73  
 
 74  
     protected ValueBinding getRestoredValueBinding(StateHolder holder,
 75  
             String expression) {
 76  0
         FacesContext context = FacesContext.getCurrentInstance();
 77  0
         holder
 78  
                 .restoreState(context,
 79  
                         new Object[] { expression, getELParser() });
 80  0
         return (ValueBinding) holder;
 81  
     }
 82  
 
 83  
     protected ValueBinding getValueBindingFromCache(Application application,
 84  
             String expression) {
 85  2
         ValueBinding vb = (ValueBinding) cache.get(expression);
 86  2
         if (vb != null && vb instanceof StateHolder) {
 87  0
             vb = getRestoredValueBinding((StateHolder) vb, expression);
 88  
         }
 89  2
         return vb;
 90  
     }
 91  
 }