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