blob: a368e8febf4a0bec5f24f9ba2aae75d39ddc2a67 [file] [log] [blame]
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileProvider;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.io.URLUtil;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class PathUtil {
private PathUtil() {
}
@Nullable
public static String getLocalPath(@Nullable VirtualFile file) {
if (file == null || !file.isValid()) {
return null;
}
if (file.getFileSystem().getProtocol().equals(URLUtil.JAR_PROTOCOL) && file.getParent() != null) {
return null;
}
return getLocalPath(file.getPath());
}
@NotNull
public static String getLocalPath(@NotNull String path) {
return FileUtil.toSystemDependentName(StringUtil.trimEnd(path, URLUtil.JAR_SEPARATOR));
}
@NotNull
public static VirtualFile getLocalFile(@NotNull VirtualFile file) {
if (!file.isValid()) {
return file;
}
if (file.getFileSystem() instanceof LocalFileProvider) {
final VirtualFile localFile = ((LocalFileProvider)file.getFileSystem()).getLocalVirtualFileFor(file);
if (localFile != null) {
return localFile;
}
}
return file;
}
@NotNull
public static String getJarPathForClass(@NotNull Class aClass) {
final String pathForClass = PathManager.getJarPathForClass(aClass);
assert pathForClass != null : aClass;
return pathForClass;
}
@NotNull
public static String toPresentableUrl(@NotNull String url) {
return getLocalPath(VirtualFileManager.extractPath(url));
}
public static String getCanonicalPath(@NonNls String path) {
return FileUtil.toCanonicalPath(path);
}
@NotNull
public static String getFileName(@NotNull String path) {
return PathUtilRt.getFileName(path);
}
@NotNull
public static String getParentPath(@NotNull String path) {
return PathUtilRt.getParentPath(path);
}
@NotNull
public static String suggestFileName(@NotNull String text) {
return PathUtilRt.suggestFileName(text);
}
@NotNull
public static String suggestFileName(@NotNull String text, final boolean allowDots, final boolean allowSpaces) {
return PathUtilRt.suggestFileName(text, allowDots, allowSpaces);
}
public static boolean isValidFileName(@NotNull String fileName) {
return PathUtilRt.isValidFileName(fileName);
}
@Contract("null -> null; !null -> !null")
public static String toSystemIndependentName(@Nullable String path) {
return path == null ? null : FileUtilRt.toSystemIndependentName(path);
}
@Contract("null -> null; !null -> !null")
public static String toSystemDependentName(@Nullable String path) {
return path == null ? null : FileUtilRt.toSystemDependentName(path);
}
}