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