2 * Copyright 2000-2009 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.
17 package com
.intellij
.ide
.util
;
19 import com
.intellij
.openapi
.util
.SystemInfo
;
20 import com
.intellij
.openapi
.util
.text
.StringUtil
;
21 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
22 import com
.intellij
.openapi
.vfs
.VirtualFile
;
23 import com
.intellij
.psi
.PsiDirectory
;
24 import com
.intellij
.psi
.PsiManager
;
25 import com
.intellij
.util
.IncorrectOperationException
;
28 import java
.util
.StringTokenizer
;
30 public class DirectoryUtil
{
31 private DirectoryUtil() {
36 * Creates the directory with the given path via PSI, including any
37 * necessary but nonexistent parent directories. Must be run in write action.
38 * @param path directory path in the local file system; separators must be '/'
39 * @return true if path exists or has been created as the result of this method call; false otherwise
41 public static PsiDirectory
mkdirs(PsiManager manager
, String path
) throws IncorrectOperationException
{
42 if (File
.separatorChar
!= '/') {
43 if (path
.indexOf(File
.separatorChar
) != -1) {
44 throw new IllegalArgumentException("separators must be '/'; path is " + path
);
48 String existingPath
= path
;
50 PsiDirectory directory
= null;
52 // find longest existing path
53 while (existingPath
.length() > 0) {
54 VirtualFile file
= LocalFileSystem
.getInstance().findFileByPath(existingPath
);
56 directory
= manager
.findDirectory(file
);
57 if (directory
== null) {
63 if (StringUtil
.endsWithChar(existingPath
, '/')) {
64 existingPath
= existingPath
.substring(0, existingPath
.length() - 1);
65 if (SystemInfo
.isWindows
&& existingPath
.length() == 2 && existingPath
.charAt(1) == ':') {
70 int index
= existingPath
.lastIndexOf('/');
76 existingPath
= existingPath
.substring(0, index
);
79 if (directory
== null) {
83 if (existingPath
.equals(path
)) {
87 String postfix
= path
.substring(existingPath
.length() + 1, path
.length());
88 StringTokenizer tokenizer
= new StringTokenizer(postfix
, "/");
89 while (tokenizer
.hasMoreTokens()) {
90 String name
= tokenizer
.nextToken();
92 PsiDirectory subdirectory
= directory
.createSubdirectory(name
);
93 if (subdirectory
== null) {
97 directory
= subdirectory
;
103 public static PsiDirectory
createSubdirectories(final String subDirName
, PsiDirectory baseDirectory
, final String delim
) throws IncorrectOperationException
{
104 StringTokenizer tokenizer
= new StringTokenizer(subDirName
, delim
);
105 PsiDirectory dir
= baseDirectory
;
106 while (tokenizer
.hasMoreTokens()) {
107 String packName
= tokenizer
.nextToken();
108 if (tokenizer
.hasMoreTokens()) {
109 PsiDirectory existingDir
= dir
.findSubdirectory(packName
);
110 if (existingDir
!= null) {
115 dir
= dir
.createSubdirectory(packName
);