Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FilteringUtils |
|
| 3.5;3.5 |
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 Olivier Lamy | |
27 | * @author Dennis Lundberg | |
28 | * | |
29 | */ | |
30 | public final class FilteringUtils | |
31 | { | |
32 | private static final String WINDOWS_PATH_PATTERN = "^(.*)[a-zA-Z]:\\\\(.*)"; | |
33 | ||
34 | 1 | private static final Pattern PATTERN = Pattern.compile( WINDOWS_PATH_PATTERN ) ; |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | private FilteringUtils() | |
40 | 0 | { |
41 | // nothing just an util class | |
42 | 0 | } |
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 | 182 | if ( !StringUtils.isEmpty( val ) && PATTERN.matcher( val ).matches() ) |
49 | { | |
50 | // Adapted from StringUtils.replace in plexus-utils to accommodate pre-escaped backslashes. | |
51 | 12 | StringBuffer buf = new StringBuffer( val.length() ); |
52 | 12 | int start = 0, end = 0; |
53 | 36 | while ( ( end = val.indexOf( '\\', start ) ) != -1 ) |
54 | { | |
55 | 24 | buf.append( val.substring( start, end ) ).append( "\\\\" ); |
56 | 24 | start = end + 1; |
57 | ||
58 | 24 | if ( val.indexOf( '\\', end + 1 ) == end + 1 ) |
59 | { | |
60 | 0 | start++; |
61 | } | |
62 | } | |
63 | ||
64 | 12 | buf.append( val.substring( start ) ); |
65 | ||
66 | 12 | return buf.toString(); |
67 | } | |
68 | 170 | return val; |
69 | } | |
70 | ||
71 | } |