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