View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util;
20  
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import static java.util.Objects.requireNonNull;
26  
27  /**
28   * A reusable utility class for file paths.
29   *
30   * @since 2.0.13
31   */
32  public final class PathUtils {
33      private PathUtils() {
34          // hide constructor
35      }
36  
37      private static final Map<String, String> ILLEGAL_PATH_SEGMENT_REPLACEMENTS;
38  
39      static {
40          HashMap<String, String> illegalPathSegmentReplacements = new HashMap<>();
41          illegalPathSegmentReplacements.put("\\", "-BACKSLASH-");
42          illegalPathSegmentReplacements.put("/", "-SLASH-");
43          illegalPathSegmentReplacements.put(":", "-COLON-");
44          illegalPathSegmentReplacements.put("\"", "-QUOTE-");
45          illegalPathSegmentReplacements.put("<", "-LT-");
46          illegalPathSegmentReplacements.put(">", "-GT-");
47          illegalPathSegmentReplacements.put("|", "-PIPE-");
48          illegalPathSegmentReplacements.put("?", "-QMARK-");
49          illegalPathSegmentReplacements.put("*", "-ASTERISK-");
50          ILLEGAL_PATH_SEGMENT_REPLACEMENTS = Collections.unmodifiableMap(illegalPathSegmentReplacements);
51      }
52  
53      /**
54       * Method that makes sure that passed in string is valid "path segment" string. It achieves it by potentially
55       * changing it, replacing illegal characters in it with legal ones.
56       * <p>
57       * Note: this method considers empty string as "valid path segment", it is caller duty to ensure empty string
58       * is not used as path segment alone.
59       * <p>
60       * This method is simplistic on purpose, and if frequently used, best if results are cached (per session)
61       */
62      public static String stringToPathSegment(String string) {
63          requireNonNull(string);
64          StringBuilder result = new StringBuilder(string);
65          for (Map.Entry<String, String> entry : ILLEGAL_PATH_SEGMENT_REPLACEMENTS.entrySet()) {
66              String illegal = entry.getKey();
67              int pos = result.indexOf(illegal);
68              while (pos >= 0) {
69                  result.replace(pos, pos + illegal.length(), entry.getValue());
70                  pos = result.indexOf(illegal);
71              }
72          }
73          return result.toString();
74      }
75  }