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.apache.maven.plugin.surefire.report;
20
21 import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
22
23 /**
24 * Utils class for file-based reporters
25 *
26 * @author Andreas Gudian
27 */
28 public final class FileReporterUtils {
29 private FileReporterUtils() {
30 throw new IllegalStateException("non instantiable constructor");
31 }
32
33 public static String stripIllegalFilenameChars(String original) {
34 StringBuilder result = new StringBuilder(original);
35 String illegalChars = getOSSpecificIllegalChars();
36 for (int i = 0, len = result.length(); i < len; i++) {
37 char charFromOriginal = result.charAt(i);
38 boolean isIllegalChar = illegalChars.indexOf(charFromOriginal) != -1;
39 if (isIllegalChar) {
40 result.setCharAt(i, '_');
41 }
42 }
43 return result.toString();
44 }
45
46 private static String getOSSpecificIllegalChars() {
47 // forbidden and quoted characters
48 // https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
49 // https://cygwin.com/cygwin-ug-net/using-specialnames.html
50 // https://www.cyberciti.biz/faq/linuxunix-rules-for-naming-file-and-directory-names/
51 return IS_OS_WINDOWS ? "[],\\/:*?\"<>|\0" : "()&\\/:*?\"<>|\0";
52 }
53 }