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