| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| EnvironmentFilter |
|
| 2.3333333333333335;2.333 |
| 1 | /* | |
| 2 | * EnvironmentFilter.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 | /** | |
| 24 | * The <code>EnvironmentFilter</code> object is used to provide a | |
| 25 | * filter that will replace the specified values with an environment | |
| 26 | * variable from the OS. This can be given a delegate filter which | |
| 27 | * can be used to resolve replacements should the value requested | |
| 28 | * not match an environment variable from the OS. | |
| 29 | * | |
| 30 | * @author Niall Gallagher | |
| 31 | */ | |
| 32 | public class EnvironmentFilter implements Filter { | |
| 33 | ||
| 34 | /** | |
| 35 | * Filter delegated to if no environment variable is resolved. | |
| 36 | */ | |
| 37 | private Filter filter; | |
| 38 | ||
| 39 | /** | |
| 40 | * Constructor for the <code>EnvironmentFilter</code> object. This | |
| 41 | * creates a filter that resolves replacements using environment | |
| 42 | * variables. Should the environment variables not contain the | |
| 43 | * requested mapping this will return a null value. | |
| 44 | */ | |
| 45 | public EnvironmentFilter() { | |
| 46 | 200 | this(null); |
| 47 | 200 | } |
| 48 | ||
| 49 | /** | |
| 50 | * Constructor for the <code>EnvironmentFilter</code> object. This | |
| 51 | * creates a filter that resolves replacements using environment | |
| 52 | * variables. Should the environment variables not contain the | |
| 53 | * requested mapping this will delegate to the specified filter. | |
| 54 | * | |
| 55 | * @param filter the filter delegated to should resolution fail | |
| 56 | */ | |
| 57 | 202 | public EnvironmentFilter(Filter filter) { |
| 58 | 202 | this.filter = filter; |
| 59 | 202 | } |
| 60 | ||
| 61 | /** | |
| 62 | * Replaces the text provided with the value resolved from the | |
| 63 | * environment variables. If the environment variables fail this | |
| 64 | * will delegate to the specified <code>Filter</code> if it is | |
| 65 | * not a null object. If no match is found a null is returned. | |
| 66 | * | |
| 67 | * @param text this is the text value to be replaced | |
| 68 | * | |
| 69 | * @return this will return the replacement text resolved | |
| 70 | */ | |
| 71 | public String replace(String text) { | |
| 72 | 10 | String value = System.getenv(text); |
| 73 | ||
| 74 | 10 | if(value != null) { |
| 75 | 0 | return value; |
| 76 | } | |
| 77 | 10 | if(filter != null) { |
| 78 | 5 | return filter.replace(text); |
| 79 | } | |
| 80 | 5 | return null; |
| 81 | } | |
| 82 | } |