| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MapFilter |
|
| 2.6666666666666665;2.667 |
| 1 | /* | |
| 2 | * MapFilter.java May 2006 | |
| 3 | * | |
| 4 | * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net> | |
| 5 | * | |
| 6 | * This library is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU Lesser General Public | |
| 8 | * License as published by the Free Software Foundation. | |
| 9 | * | |
| 10 | * This library is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU Lesser General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU Lesser General | |
| 16 | * Public License along with this library; if not, write to the | |
| 17 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
| 18 | * Boston, MA 02111-1307 USA | |
| 19 | */ | |
| 20 | ||
| 21 | package org.simpleframework.xml.filter; | |
| 22 | ||
| 23 | import java.util.Map; | |
| 24 | ||
| 25 | /** | |
| 26 | * The <code>MapFilter</code> object is a filter that can make use | |
| 27 | * of user specified mappings for replacement. This filter can be | |
| 28 | * given a <code>Map</code> of name value pairs which will be used | |
| 29 | * to resolve a value using the specified mappings. If there is | |
| 30 | * no match found the filter will delegate to the provided filter. | |
| 31 | * | |
| 32 | * @author Niall Gallagher | |
| 33 | */ | |
| 34 | public class MapFilter implements Filter { | |
| 35 | ||
| 36 | /** | |
| 37 | * This will resolve the replacement if no mapping is found. | |
| 38 | */ | |
| 39 | private Filter filter; | |
| 40 | ||
| 41 | /** | |
| 42 | * This contains a collection of user specified mappings. | |
| 43 | */ | |
| 44 | private Map map; | |
| 45 | ||
| 46 | /** | |
| 47 | * Constructor for the <code>MapFilter</code> object. This will | |
| 48 | * use the specified mappings to resolve replacements. If this | |
| 49 | * map does not contain a requested mapping null is resolved. | |
| 50 | * | |
| 51 | * @param map this contains the user specified mappings | |
| 52 | */ | |
| 53 | public MapFilter(Map map) { | |
| 54 | 200 | this(map, null); |
| 55 | 200 | } |
| 56 | ||
| 57 | /** | |
| 58 | * Constructor for the <code>MapFilter</code> object. This will | |
| 59 | * use the specified mappings to resolve replacements. If this | |
| 60 | * map does not contain a requested mapping the provided filter | |
| 61 | * is used to resolve the replacement text. | |
| 62 | * | |
| 63 | * @param map this contains the user specified mappings | |
| 64 | * @param filter this is delegated to if the map fails | |
| 65 | */ | |
| 66 | 200 | public MapFilter(Map map, Filter filter) { |
| 67 | 200 | this.filter = filter; |
| 68 | 200 | this.map = map; |
| 69 | 200 | } |
| 70 | ||
| 71 | /** | |
| 72 | * Replaces the text provided with the value resolved from the | |
| 73 | * specified <code>Map</code>. If the map fails this will | |
| 74 | * delegate to the specified <code>Filter</code> if it is not | |
| 75 | * a null object. If no match is found a null is returned. | |
| 76 | * | |
| 77 | * @param text this is the text value to be replaced | |
| 78 | * | |
| 79 | * @return this will return the replacement text resolved | |
| 80 | */ | |
| 81 | public String replace(String text) { | |
| 82 | 50015 | Object value = null; |
| 83 | ||
| 84 | 50015 | if(map != null) { |
| 85 | 50015 | value = map.get(text); |
| 86 | } | |
| 87 | 50015 | if(value != null) { |
| 88 | 4 | return value.toString(); |
| 89 | } | |
| 90 | 50011 | if(filter != null) { |
| 91 | 0 | return filter.replace(text); |
| 92 | } | |
| 93 | 50011 | return null; |
| 94 | } | |
| 95 | } |