View Javadoc
1   package org.apache.maven.shared.filtering;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.regex.Pattern;
23  
24  import org.apache.maven.shared.utils.StringUtils;
25  
26  /**
27   * @author Olivier Lamy
28   * @author Dennis Lundberg
29   */
30  public final class FilteringUtils
31  {
32      private static final String WINDOWS_PATH_PATTERN = "^(.*)[a-zA-Z]:\\\\(.*)";
33  
34      private static final Pattern PATTERN = Pattern.compile( WINDOWS_PATH_PATTERN );
35  
36      /**
37       * 
38       */
39      private FilteringUtils()
40      {
41          // nothing just an util class
42      }
43  
44      // TODO: Correct to handle relative windows paths. (http://jira.codehaus.org/browse/MSHARED-121)
45      // How do we distinguish a relative windows path from some other value that happens to contain backslashes??
46      /**
47       * @param val The value to be escaped.
48       * @return Escaped value
49       */
50      public static String escapeWindowsPath( String val )
51      {
52          if ( !StringUtils.isEmpty( val ) && PATTERN.matcher( val ).matches() )
53          {
54              // Adapted from StringUtils.replace in plexus-utils to accommodate pre-escaped backslashes.
55              StringBuilder buf = new StringBuilder( val.length() );
56              int start = 0, end = 0;
57              while ( ( end = val.indexOf( '\\', start ) ) != -1 )
58              {
59                  buf.append( val.substring( start, end ) ).append( "\\\\" );
60                  start = end + 1;
61  
62                  if ( val.indexOf( '\\', end + 1 ) == end + 1 )
63                  {
64                      start++;
65                  }
66              }
67  
68              buf.append( val.substring( start ) );
69  
70              return buf.toString();
71          }
72          return val;
73      }
74  
75  }