Commit | Line | Data |
---|---|---|
05af6c11 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.core.project.model; | |
14 | ||
15 | import org.eclipse.core.resources.IResource; | |
16 | import org.eclipse.core.resources.ResourcesPlugin; | |
17 | import org.eclipse.core.runtime.Path; | |
18 | ||
19 | /** | |
20 | * Utility class for common tmf.core functionalities | |
05af6c11 BH |
21 | */ |
22 | public class TmfTraceCoreUtils { | |
23 | ||
24 | // ------------------------------------------------------------------------ | |
25 | // Constants | |
26 | // ------------------------------------------------------------------------ | |
27 | private static final boolean IS_WINDOWS = System.getProperty("os.name").contains("Windows") ? true : false; //$NON-NLS-1$ //$NON-NLS-2$ | |
28 | private static final String INVALID_RESOURCE_CHARACTERS_WIN = "[\\\\/:*?\\\"<>\\|]|\\.$"; //$NON-NLS-1$ | |
29 | private static final String INVALID_RESOURCE_CHARACTERS_OTHER = "[/\0]"; //$NON-NLS-1$ | |
30 | private static final char BACKSLASH_PUA = 0xF000 + '\\'; | |
31 | private static final char COLON_PUA = 0xF000 + ':'; | |
32 | ||
33 | /** | |
34 | * Validates whether the given input file or folder string is a valid | |
35 | * resource string for one of the given types. It replaces invalid | |
36 | * characters by '_' and prefixes the name with '_' if needed. | |
37 | * | |
38 | * @param input | |
39 | * a input name to validate | |
40 | * @return valid name | |
41 | */ | |
42 | public static String validateName(String input) { | |
43 | String output = input; | |
44 | String pattern; | |
45 | if (IS_WINDOWS) { | |
46 | pattern = INVALID_RESOURCE_CHARACTERS_WIN; | |
47 | } else { | |
48 | pattern = INVALID_RESOURCE_CHARACTERS_OTHER; | |
49 | } | |
50 | ||
51 | output = output.replaceAll(pattern, String.valueOf('_')); | |
52 | if(!ResourcesPlugin.getWorkspace().validateName(output, IResource.FILE | IResource.FOLDER).isOK()) { | |
53 | output = '_' + output; | |
54 | } | |
55 | return output; | |
56 | } | |
57 | ||
58 | /** | |
59 | * Creates a new safe path, replacing any invalid Windows file name | |
60 | * characters '\' and ':' with characters in the Unicode Private Use Area. | |
61 | * | |
62 | * @param path | |
63 | * a string path | |
64 | * @return a safe path | |
65 | */ | |
66 | public static Path newSafePath(String path) { | |
67 | return new Path(path.replace('\\', BACKSLASH_PUA).replace(':', COLON_PUA)); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Returns the string representation of a safe path, replacing characters | |
72 | * in the Unicode Private Use Area back to their original value. | |
73 | * | |
74 | * @param path | |
75 | * a safe path string | |
76 | * @return a string representation of this path | |
77 | */ | |
78 | public static String safePathToString(String path) { | |
79 | return path.replace(BACKSLASH_PUA, '\\').replace(COLON_PUA, ':'); | |
80 | } | |
81 | ||
82 | } |