Coverage Report - org.simpleframework.xml.core.ExpressionBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ExpressionBuilder
100%
14/14
75%
3/4
2
 
 1  
 /*
 2  
  * ExpressionBuilder.java November 2010
 3  
  *
 4  
  * Copyright (C) 2010, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 import org.simpleframework.xml.strategy.Type;
 22  
 import org.simpleframework.xml.stream.Format;
 23  
 import org.simpleframework.xml.util.Cache;
 24  
 import org.simpleframework.xml.util.LimitedCache;
 25  
 
 26  
 /**
 27  
  * The <code>ExpressionBuilder</code> object is used build and cache
 28  
  * path expressions. The expressions provided by this must be valid
 29  
  * XPath expressions. However, only a subset of the full XPath 
 30  
  * syntax is supported. Because these expressions require parsing
 31  
  * they are cached internally. This improves performance as if a
 32  
  * path expression is declared several times it is parsed once.
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  * 
 36  
  * @see org.simpleframework.xml.core.PathParser
 37  
  */
 38  
 class ExpressionBuilder {
 39  
    
 40  
    /**
 41  
     * This is the cache of path expressions previously built.
 42  
     */
 43  
    private final Cache<Expression> cache;
 44  
    
 45  
    /**
 46  
     * This is the format used to style the path segments.
 47  
     */
 48  
    private final Format format;
 49  
 
 50  
    /**
 51  
     * This is the type the expressions are being built for.
 52  
     */
 53  
    private final Class type;
 54  
    
 55  
    /**
 56  
     * Constructor for the <code>ExpressionBuilder</code>. This is
 57  
     * used to create a builder to cache frequently requested XPath
 58  
     * expressions. Such caching improves the overall performance.
 59  
     * 
 60  
     * @param detail the details for the the class with expressions
 61  
     * @param support this contains various support functions
 62  
     */
 63  2379
    public ExpressionBuilder(Detail detail, Support support) {
 64  2379
       this.cache = new LimitedCache<Expression>();
 65  2379
       this.format = support.getFormat();
 66  2379
       this.type = detail.getType();
 67  2379
    }
 68  
    
 69  
    /**
 70  
     * This is used to create an <code>Expression</code> from the
 71  
     * provided path. If the path does not conform to the syntax
 72  
     * supported then an exception is thrown to indicate the error.
 73  
     * If the path was requested before, a cached instance is used.
 74  
     * 
 75  
     * @param path this is the XPath expression to be parsed
 76  
     * 
 77  
     * @return this returns the resulting expression object
 78  
     */
 79  
    public Expression build(String path) throws Exception {
 80  312
       Expression expression = cache.fetch(path);
 81  
       
 82  312
       if(expression == null) {
 83  158
          return create(path);
 84  
       }
 85  154
       return expression;
 86  
    }
 87  
 
 88  
    /**
 89  
     * This is used to create an <code>Expression</code> from the
 90  
     * provided path. If the path does not conform to the syntax
 91  
     * supported then an exception is thrown to indicate the error.
 92  
     * 
 93  
     * @param path this is the XPath expression to be parsed
 94  
     * 
 95  
     * @return this returns the resulting expression object
 96  
     */
 97  
    private Expression create(String path) throws Exception {
 98  158
       Type detail = new ClassType(type);
 99  158
       Expression expression = new PathParser(path, detail, format);
 100  
       
 101  158
       if(cache != null) {
 102  158
          cache.cache(path, expression);
 103  
       }
 104  158
       return expression;
 105  
    }
 106  
 }