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  import org.codehaus.plexus.util.StringUtils;
24  
25  /**
26   * @author <a href="mailto:olamy@apache.org">olamy</a>
27   * @author Dennis Lundberg
28   * @version $Id: FilteringUtils.java 1061510 2011-01-20 20:58:23Z dennisl $
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      public static String escapeWindowsPath( String val )
47      {
48          if ( !StringUtils.isEmpty( val ) && PATTERN.matcher( val ).matches() )
49          {
50              // Adapted from StringUtils.replace in plexus-utils to accommodate pre-escaped backslashes.
51              StringBuffer buf = new StringBuffer( val.length() );
52              int start = 0, end = 0;
53              while ( ( end = val.indexOf( '\\', start ) ) != -1 )
54              {
55                  buf.append( val.substring( start, end ) ).append( "\\\\" );
56                  start = end + 1;
57                  
58                  if ( val.indexOf( '\\', end + 1 ) == end + 1 )
59                  {
60                      start++;
61                  }
62              }
63              
64              buf.append( val.substring( start ) );
65              
66              return buf.toString();
67          }
68          return val;
69      }
70  
71  }