Coverage Report - org.codehaus.plexus.interpolation.util.ValueSourceUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueSourceUtils
40%
10/25
45%
11/24
5.667
 
 1  
 package org.codehaus.plexus.interpolation.util;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 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.ValueSource;
 20  
 
 21  
 import java.util.Collection;
 22  
 
 23  
 /**
 24  
  * Utility methods shared by multiple {@link ValueSource} implementations.
 25  
  *
 26  
  * @author jdcasey
 27  
  */
 28  
 public final class ValueSourceUtils
 29  
 {
 30  
 
 31  
     private ValueSourceUtils()
 32  0
     {
 33  0
     }
 34  
 
 35  
     /**
 36  
      * If the expression starts with one of the provided prefixes, trim that prefix
 37  
      * and return the remaining expression. If it doesn't start with a provided
 38  
      * prefix, and the allowUnprefixedExpressions flag is true, then return the
 39  
      * expression unchanged; if the flag is false, return null. Finally, if the
 40  
      * original expression is null, return null without attempting to process it.
 41  
      *
 42  
      * @param expression                 The expression to trim
 43  
      * @param possiblePrefixes           The list of possible expression prefixes to trim
 44  
      * @param allowUnprefixedExpressions Whether to return the expression if it
 45  
      *                                   doesn't start with one of the prefixes. If true, simply return the
 46  
      *                                   original expression; if false, return null.
 47  
      * @return The trimmed expression, or null. See the behavior of
 48  
      *         allowUnprefixedExpressions in this method for more detail.
 49  
      */
 50  
     public static String trimPrefix( String expression, Collection<String> possiblePrefixes,
 51  
                                      boolean allowUnprefixedExpressions )
 52  
     {
 53  0
         if ( expression == null )
 54  
         {
 55  0
             return null;
 56  
         }
 57  
 
 58  0
         String realExpr = null;
 59  0
         for ( String prefix : possiblePrefixes )
 60  
         {
 61  0
             if ( expression.startsWith( prefix ) )
 62  
             {
 63  0
                 realExpr = expression.substring( prefix.length() );
 64  0
                 if ( realExpr.startsWith( "." ) )
 65  
                 {
 66  0
                     realExpr = realExpr.substring( 1 );
 67  
                 }
 68  
                 break;
 69  
             }
 70  0
         }
 71  
 
 72  0
         if ( realExpr == null && allowUnprefixedExpressions )
 73  
         {
 74  0
             realExpr = expression;
 75  
         }
 76  
 
 77  0
         return realExpr;
 78  
     }
 79  
 
 80  
     public static String trimPrefix( String expression, String[] possiblePrefixes, boolean allowUnprefixedExpressions )
 81  
     {
 82  28
         if ( expression == null )
 83  
         {
 84  0
             return null;
 85  
         }
 86  
 
 87  28
         String realExpr = null;
 88  39
         for ( String prefix : possiblePrefixes )
 89  
         {
 90  31
             if ( expression.startsWith( prefix ) )
 91  
             {
 92  20
                 realExpr = expression.substring( prefix.length() );
 93  20
                 if ( realExpr.startsWith( "." ) )
 94  
                 {
 95  9
                     realExpr = realExpr.substring( 1 );
 96  
                 }
 97  
                 break;
 98  
             }
 99  
         }
 100  
 
 101  28
         if ( realExpr == null && allowUnprefixedExpressions )
 102  
         {
 103  3
             realExpr = expression;
 104  
         }
 105  
 
 106  28
         return realExpr;
 107  
     }
 108  
 
 109  
 }