View Javadoc
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      {
33      }
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          if ( expression == null )
54          {
55              return null;
56          }
57  
58          String realExpr = null;
59          for ( String prefix : possiblePrefixes )
60          {
61              if ( expression.startsWith( prefix ) )
62              {
63                  realExpr = expression.substring( prefix.length() );
64                  if ( realExpr.startsWith( "." ) )
65                  {
66                      realExpr = realExpr.substring( 1 );
67                  }
68                  break;
69              }
70          }
71  
72          if ( realExpr == null && allowUnprefixedExpressions )
73          {
74              realExpr = expression;
75          }
76  
77          return realExpr;
78      }
79  
80      public static String trimPrefix( String expression, String[] possiblePrefixes, boolean allowUnprefixedExpressions )
81      {
82          if ( expression == null )
83          {
84              return null;
85          }
86  
87          String realExpr = null;
88          for ( String prefix : possiblePrefixes )
89          {
90              if ( expression.startsWith( prefix ) )
91              {
92                  realExpr = expression.substring( prefix.length() );
93                  if ( realExpr.startsWith( "." ) )
94                  {
95                      realExpr = realExpr.substring( 1 );
96                  }
97                  break;
98              }
99          }
100 
101         if ( realExpr == null && allowUnprefixedExpressions )
102         {
103             realExpr = expression;
104         }
105 
106         return realExpr;
107     }
108 
109 }