Coverage Report - org.codehaus.plexus.interpolation.PrefixedValueSourceWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
PrefixedValueSourceWrapper
50 %
13/26
33 %
2/6
1,5
 
 1  
 package org.codehaus.plexus.interpolation;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2008 Codehaus Foundation.
 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 implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.codehaus.plexus.interpolation.util.ValueSourceUtils;
 20  
 
 21  
 import java.util.Collections;
 22  
 import java.util.List;
 23  
 
 24  
 /**
 25  
  * {@link ValueSource} implementation which simply wraps another value source,
 26  
  * and trims any of a set of possible expression prefixes before delegating the
 27  
  * modified expression to be resolved by the real value source.
 28  
  *
 29  
  * @author jdcasey
 30  
  * @version $Id$
 31  
  */
 32  
 public class PrefixedValueSourceWrapper
 33  
     implements FeedbackEnabledValueSource, QueryEnabledValueSource
 34  
 {
 35  
 
 36  
     private final ValueSource valueSource;
 37  
 
 38  
     private final String[] possiblePrefixes;
 39  
 
 40  
     private boolean allowUnprefixedExpressions;
 41  
 
 42  
     private String lastExpression;
 43  
 
 44  
     /**
 45  
      * Wrap the given value source, but first trim the given prefix from any
 46  
      * expressions before they are passed along for resolution. If an expression
 47  
      * doesn't start with the given prefix, do not resolve it.
 48  
      *
 49  
      * @param valueSource The {@link ValueSource} to wrap.
 50  
      * @param prefix      The expression prefix to trim.
 51  
      */
 52  
     public PrefixedValueSourceWrapper( ValueSource valueSource, String prefix )
 53  3
     {
 54  3
         this.valueSource = valueSource;
 55  3
         possiblePrefixes = new String[]{ prefix };
 56  3
     }
 57  
 
 58  
     /**
 59  
      * Wrap the given value source, but first trim the given prefix from any
 60  
      * expressions before they are passed along for resolution. If an expression
 61  
      * doesn't start with the given prefix and the allowUnprefixedExpressions flag
 62  
      * is set to true, simply pass the expression through to the nested value source
 63  
      * unchanged. If this flag is false, only allow resolution of those expressions
 64  
      * that start with the specified prefix.
 65  
      *
 66  
      * @param valueSource                The {@link ValueSource} to wrap.
 67  
      * @param prefix                     The expression prefix to trim.
 68  
      * @param allowUnprefixedExpressions Flag telling the wrapper whether to
 69  
      *                                   continue resolving expressions that don't start with the prefix it tracks.
 70  
      */
 71  
     public PrefixedValueSourceWrapper( ValueSource valueSource, String prefix, boolean allowUnprefixedExpressions )
 72  0
     {
 73  0
         this.valueSource = valueSource;
 74  0
         possiblePrefixes = new String[]{ prefix };
 75  0
         this.allowUnprefixedExpressions = allowUnprefixedExpressions;
 76  0
     }
 77  
 
 78  
     /**
 79  
      * Wrap the given value source, but first trim one of the given prefixes from any
 80  
      * expressions before they are passed along for resolution. If an expression
 81  
      * doesn't start with one of the given prefixes, do not resolve it.
 82  
      *
 83  
      * @param valueSource      The {@link ValueSource} to wrap.
 84  
      * @param possiblePrefixes The List of expression prefixes to trim.
 85  
      */
 86  
     public PrefixedValueSourceWrapper( ValueSource valueSource, List<String> possiblePrefixes )
 87  0
     {
 88  0
         this.valueSource = valueSource;
 89  0
         this.possiblePrefixes = possiblePrefixes.toArray( new String[possiblePrefixes.size()] );
 90  0
     }
 91  
 
 92  
     /**
 93  
      * Wrap the given value source, but first trim one of the given prefixes from any
 94  
      * expressions before they are passed along for resolution. If an expression
 95  
      * doesn't start with the given prefix and the allowUnprefixedExpressions flag
 96  
      * is set to true, simply pass the expression through to the nested value source
 97  
      * unchanged. If this flag is false, only allow resolution of those expressions
 98  
      * that start with the specified prefix.
 99  
      *
 100  
      * @param valueSource                The {@link ValueSource} to wrap.
 101  
      * @param possiblePrefixes           The List of expression prefixes to trim.
 102  
      * @param allowUnprefixedExpressions Flag telling the wrapper whether to
 103  
      *                                   continue resolving expressions that don't start with one of the prefixes it tracks.
 104  
      */
 105  
     public PrefixedValueSourceWrapper( ValueSource valueSource, List<String> possiblePrefixes,
 106  
                                        boolean allowUnprefixedExpressions )
 107  3
     {
 108  3
         this.valueSource = valueSource;
 109  3
         this.possiblePrefixes = possiblePrefixes.toArray( new String[possiblePrefixes.size()] );
 110  3
         this.allowUnprefixedExpressions = allowUnprefixedExpressions;
 111  3
     }
 112  
 
 113  
     /**
 114  
      * Uses {@link ValueSourceUtils#trimPrefix(String, java.util.Collection, boolean)} to
 115  
      * get the trimmed expression. If this expression is null (because the original
 116  
      * expression was null, or because the expression is unprefixed and unprefixed
 117  
      * expressions are not allowed here), then return null; otherwise, return the
 118  
      * nested {@link ValueSource#getValue(String)} result.
 119  
      */
 120  
     public Object getValue( String expression )
 121  
     {
 122  6
         lastExpression = ValueSourceUtils.trimPrefix( expression, possiblePrefixes, allowUnprefixedExpressions );
 123  
 
 124  6
         if ( lastExpression == null )
 125  
         {
 126  1
             return null;
 127  
         }
 128  
 
 129  5
         return valueSource.getValue( lastExpression );
 130  
     }
 131  
 
 132  
     /**
 133  
      * If the nested {@link ValueSource} implements {@link FeedbackEnabledValueSource},
 134  
      * then return that source's feedback list. Otherwise, return {@link Collections#EMPTY_LIST}.
 135  
      */
 136  
     public List getFeedback()
 137  
     {
 138  0
         return ( valueSource instanceof FeedbackEnabledValueSource )
 139  
             ? valueSource.getFeedback()
 140  
             : Collections.EMPTY_LIST;
 141  
     }
 142  
 
 143  
     /**
 144  
      * If the nested {@link ValueSource} implements {@link QueryEnabledValueSource},
 145  
      * then return that source's last expression. Otherwise, return the last expression
 146  
      * that was processed by the wrapper itself.
 147  
      */
 148  
     public String getLastExpression()
 149  
     {
 150  0
         return ( valueSource instanceof QueryEnabledValueSource )
 151  
             ? ( (QueryEnabledValueSource) valueSource ).getLastExpression()
 152  
             : lastExpression;
 153  
     }
 154  
 
 155  
     /**
 156  
      * If the nested {@link ValueSource} implements {@link FeedbackEnabledValueSource},
 157  
      * then clear that source's feedback list.
 158  
      */
 159  
     public void clearFeedback()
 160  
     {
 161  0
         valueSource.clearFeedback();
 162  0
     }
 163  
 
 164  
 }