Coverage Report - org.simpleframework.xml.filter.MapFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
MapFilter
92%
13/14
66%
4/6
2.667
 
 1  
 /*
 2  
  * MapFilter.java May 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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.filter;
 20  
 
 21  
 import java.util.Map;
 22  
 
 23  
 /**
 24  
  * The <code>MapFilter</code> object is a filter that can make use
 25  
  * of user specified mappings for replacement. This filter can be
 26  
  * given a <code>Map</code> of name value pairs which will be used
 27  
  * to resolve a value using the specified mappings. If there is
 28  
  * no match found the filter will delegate to the provided filter. 
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  */
 32  
 public class MapFilter implements Filter {
 33  
    
 34  
    /**
 35  
     * This will resolve the replacement if no mapping is found.
 36  
     */
 37  
    private Filter filter;        
 38  
 
 39  
    /**
 40  
     * This contains a collection of user specified mappings.
 41  
     */
 42  
    private Map map;
 43  
         
 44  
    /**
 45  
     * Constructor for the <code>MapFilter</code> object. This will
 46  
     * use the specified mappings to resolve replacements. If this
 47  
     * map does not contain a requested mapping null is resolved.
 48  
     * 
 49  
     * @param map this contains the user specified mappings
 50  
     */
 51  
    public MapFilter(Map map) {
 52  946
       this(map, null);           
 53  946
    }
 54  
      
 55  
    /**
 56  
     * Constructor for the <code>MapFilter</code> object. This will
 57  
     * use the specified mappings to resolve replacements. If this
 58  
     * map does not contain a requested mapping the provided filter
 59  
     * is used to resolve the replacement text.
 60  
     * 
 61  
     * @param map this contains the user specified mappings
 62  
     * @param filter this is delegated to if the map fails
 63  
     */   
 64  946
    public MapFilter(Map map, Filter filter) {
 65  946
       this.filter = filter;  
 66  946
       this.map = map;      
 67  946
    }
 68  
 
 69  
    /**
 70  
     * Replaces the text provided with the value resolved from the
 71  
     * specified <code>Map</code>. If the map fails this will
 72  
     * delegate to the specified <code>Filter</code> if it is not
 73  
     * a null object. If no match is found a null is returned.
 74  
     * 
 75  
     * @param text this is the text value to be replaced
 76  
     * 
 77  
     * @return this will return the replacement text resolved
 78  
     */
 79  
    public String replace(String text) {
 80  100048
       Object value = null;
 81  
       
 82  100048
       if(map != null) {
 83  100048
          value = map.get(text);
 84  
       }
 85  100048
       if(value != null) {
 86  12
          return value.toString();                       
 87  
       }
 88  100036
       if(filter != null) {
 89  0
          return filter.replace(text);              
 90  
       }      
 91  100036
       return null;
 92  
    }   
 93  
 }