2 * Copyright 2000-2011 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util;
18 import com.intellij.openapi.application.PathManager;
19 import com.intellij.openapi.util.io.FileUtil;
20 import com.intellij.openapi.util.text.StringUtil;
21 import com.intellij.openapi.vfs.JarFileSystem;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import com.intellij.openapi.vfs.VirtualFileManager;
24 import org.jetbrains.annotations.NonNls;
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
30 public class PathUtil {
35 public static String getLocalPath(@Nullable VirtualFile file) {
36 if (file == null || !file.isValid()) {
39 if (file.getFileSystem() instanceof JarFileSystem && file.getParent() != null) {
42 return getLocalPath(file.getPath());
46 public static String getLocalPath(@NotNull String path) {
47 return FileUtil.toSystemDependentName(StringUtil.trimEnd(path, JarFileSystem.JAR_SEPARATOR));
51 public static VirtualFile getLocalFile(@NotNull VirtualFile file) {
52 if (!file.isValid()) {
55 if (file.getFileSystem() instanceof JarFileSystem) {
56 final VirtualFile jarFile = JarFileSystem.getInstance().getVirtualFileForJar(file);
57 if (jarFile != null) {
65 public static String getJarPathForClass(@NotNull Class aClass) {
66 String resourceRoot = PathManager.getResourceRoot(aClass, "/" + aClass.getName().replace('.', '/') + ".class");
67 return new File(resourceRoot).getAbsoluteFile().getAbsolutePath();
71 public static String toPresentableUrl(@NotNull String url) {
72 return getLocalPath(VirtualFileManager.extractPath(url));
75 public static String getCanonicalPath(@NonNls String path) {
76 return FileUtil.toCanonicalPath(path);
80 public static String getFileName(@NotNull String path) {
81 if (path.length() == 0) {
84 final char c = path.charAt(path.length() - 1);
85 int end = c == '/' || c == '\\' ? path.length() - 1 : path.length();
86 int start = Math.max(path.lastIndexOf('/', end - 1), path.lastIndexOf('\\', end - 1)) + 1;
87 return path.substring(start, end);
91 public static String getParentPath(@NotNull String path) {
92 if (path.length() == 0) {
95 int end = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
96 if (end == path.length() - 1) {
97 end = Math.max(path.lastIndexOf('/', end - 1), path.lastIndexOf('\\', end - 1));
99 return end == -1 ? "" : path.substring(0, end);
103 public static String suggestFileName(@NotNull String text) {
104 StringBuilder result = new StringBuilder();
105 for (int i = 0; i < text.length(); i++) {
106 char c = text.charAt(i);
107 if (!isValidFileNameChar(c) || c == '.' || Character.isWhitespace(c)) {
114 return result.toString();
117 public static boolean isValidFileName(@NotNull String fileName) {
118 for (int i = 0; i < fileName.length(); i++) {
119 if (!isValidFileNameChar(fileName.charAt(i))) {
126 private static boolean isValidFileNameChar(char c) {
127 return c != '/' && c != '\\' && c != '\t' && c != '\n' && c != '\r' && c != ':' && c != ';' && c != '*' && c != '?'
128 && c != '"' && c != '\'' && c != '<' && c != '>';