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.
16 package com
.intellij
.openapi
.roots
.libraries
;
18 import com
.intellij
.openapi
.roots
.OrderRootType
;
19 import com
.intellij
.openapi
.vfs
.JarFileSystem
;
20 import com
.intellij
.openapi
.vfs
.VirtualFile
;
21 import com
.intellij
.openapi
.vfs
.VirtualFileManager
;
22 import com
.intellij
.util
.text
.StringTokenizer
;
23 import org
.jetbrains
.annotations
.NonNls
;
28 public class LibrariesHelperImpl
extends LibrariesHelper
{
31 public VirtualFile
findJarByClass(Library library
, @NonNls String fqn
) {
32 return library
== null ?
null : findJarByClass(library
.getFiles(OrderRootType
.CLASSES
), fqn
);
35 private VirtualFile
findJarByClass(VirtualFile
[] files
, String fqn
) {
36 for (VirtualFile file
: files
) {
37 if (findInFile(file
, new StringTokenizer(fqn
, "."))) return file
;
42 public boolean isClassAvailableInLibrary(Library library
, String fqn
) {
43 final String
[] urls
= library
.getUrls(OrderRootType
.CLASSES
);
44 return isClassAvailable(urls
, fqn
);
47 public boolean isClassAvailable(final String
[] urls
, String fqn
) {
48 for (String url
: urls
) {
49 VirtualFile file
= VirtualFileManager
.getInstance().findFileByUrl(url
);
50 if (file
== null) continue;
51 if (!(file
.getFileSystem() instanceof JarFileSystem
) && !file
.isDirectory()) {
52 file
= JarFileSystem
.getInstance().findFileByPath(file
.getPath() + JarFileSystem
.JAR_SEPARATOR
);
54 if (file
== null) continue;
55 if (findInFile(file
, new StringTokenizer(fqn
, "."))) return true;
60 private static boolean findInFile(VirtualFile root
, final StringTokenizer filePath
) {
61 if (!filePath
.hasMoreTokens()) return true;
62 @NonNls String name
= filePath
.nextToken();
63 if (!filePath
.hasMoreTokens()) {
66 final VirtualFile child
= root
.findChild(name
);
67 return child
!= null && findInFile(child
, filePath
);