Coverage Report - org.simpleframework.xml.filter.StackFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
StackFilter
90%
10/11
75%
3/4
2
 
 1  
 /*
 2  
  * StackFilter.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.Stack;
 22  
 
 23  
 /**
 24  
  * The <code>StackFilter</code> object provides a filter that can
 25  
  * be given a collection of filters which can be used to resolve a
 26  
  * replacement. The order of the resolution used for this filter
 27  
  * is last in first used. This order allows the highest priority
 28  
  * filter to be added last within the stack. 
 29  
  * 
 30  
  * @author Niall Gallagher 
 31  
  */
 32  
 public class StackFilter implements Filter {
 33  
 
 34  
    /**
 35  
     * This is used to store the filters that are used.
 36  
     */
 37  
    private Stack<Filter> stack;        
 38  
         
 39  
    /**
 40  
     * Constructor for the <code>StackFilter</code> object. This will
 41  
     * create an empty filter that initially resolves null for all
 42  
     * replacements requested. As filters are pushed into the stack
 43  
     * the <code>replace</code> method can resolve replacements. 
 44  
     */
 45  946
    public StackFilter() {
 46  946
       this.stack = new Stack<Filter>();
 47  946
    }
 48  
 
 49  
    /**
 50  
     * This pushes the the provided <code>Filter</code> on to the top
 51  
     * of the stack. The last filter pushed on to the stack has the
 52  
     * highes priority in the resolution of a replacement value.
 53  
     * 
 54  
     * @param filter this is a filter to be pushed on to the stack
 55  
     */
 56  
    public void push(Filter filter) {
 57  2838
       stack.push(filter);           
 58  2838
    }
 59  
    
 60  
    /**
 61  
     * Replaces the text provided with the value resolved from the
 62  
     * stacked filters. This attempts to resolve a replacement from
 63  
     * the top down. So the last <code>Filter</code> pushed on to
 64  
     * the stack will be the first filter queried for a replacement.
 65  
     * 
 66  
     * @param text this is the text value to be replaced
 67  
     * 
 68  
     * @return this will return the replacement text resolved
 69  
     */
 70  
    public String replace(String text) {
 71  100043
       for(int i = stack.size(); --i >= 0;) {
 72  200082
          String value = stack.get(i).replace(text);
 73  
 
 74  200082
          if(value != null){
 75  100043
             return value;                 
 76  
          }         
 77  100039
       }           
 78  0
       return null;
 79  
    }
 80  
 }