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  
  * @version $Id$
 28  
  */
 29  
 public final class ValueSourceUtils
 30  
 {
 31  
 
 32  
     private ValueSourceUtils()
 33  0
     {
 34  0
     }
 35  
 
 36  
     /**
 37  
      * If the expression starts with one of the provided prefixes, trim that prefix
 38  
      * and return the remaining expression. If it doesn't start with a provided
 39  
      * prefix, and the allowUnprefixedExpressions flag is true, then return the
 40  
      * expression unchanged; if the flag is false, return null. Finally, if the
 41  
      * original expression is null, return null without attempting to process it.
 42  
      *
 43  
      * @param expression                 The expression to trim
 44  
      * @param possiblePrefixes           The list of possible expression prefixes to trim
 45  
      * @param allowUnprefixedExpressions Whether to return the expression if it
 46  
      *                                   doesn't start with one of the prefixes. If true, simply return the
 47  
      *                                   original expression; if false, return null.
 48  
      * @return The trimmed expression, or null. See the behavior of
 49  
      *         allowUnprefixedExpressions in this method for more detail.
 50  
      */
 51  
     public static String trimPrefix( String expression, Collection<String> possiblePrefixes,
 52  
                                      boolean allowUnprefixedExpressions )
 53  
     {
 54  0
         if ( expression == null )
 55  
         {
 56  0
             return null;
 57  
         }
 58  
 
 59  0
         String realExpr = null;
 60  0
         for ( String prefix : possiblePrefixes )
 61  
         {
 62  0
             if ( expression.startsWith( prefix ) )
 63  
             {
 64  0
                 realExpr = expression.substring( prefix.length() );
 65  0
                 if ( realExpr.startsWith( "." ) )
 66  
                 {
 67  0
                     realExpr = realExpr.substring( 1 );
 68  
                 }
 69  
                 break;
 70  
             }
 71  0
         }
 72  
 
 73  0
         if ( realExpr == null && allowUnprefixedExpressions )
 74  
         {
 75  0
             realExpr = expression;
 76  
         }
 77  
 
 78  0
         return realExpr;
 79  
     }
 80  
 
 81  
     public static String trimPrefix( String expression, String[] possiblePrefixes, boolean allowUnprefixedExpressions )
 82  
     {
 83  22
         if ( expression == null )
 84  
         {
 85  0
             return null;
 86  
         }
 87  
 
 88  22
         String realExpr = null;
 89  31
         for ( String prefix : possiblePrefixes )
 90  
         {
 91  25
             if ( expression.startsWith( prefix ) )
 92  
             {
 93  16
                 realExpr = expression.substring( prefix.length() );
 94  16
                 if ( realExpr.startsWith( "." ) )
 95  
                 {
 96  5
                     realExpr = realExpr.substring( 1 );
 97  
                 }
 98  
                 break;
 99  
             }
 100  
         }
 101  
 
 102  22
         if ( realExpr == null && allowUnprefixedExpressions )
 103  
         {
 104  3
             realExpr = expression;
 105  
         }
 106  
 
 107  22
         return realExpr;
 108  
     }
 109  
 
 110  
 }