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.
20 package com
.intellij
.lang
.properties
;
22 import com
.intellij
.lang
.properties
.psi
.PropertiesFile
;
23 import com
.intellij
.openapi
.fileTypes
.FileTypeManager
;
24 import com
.intellij
.openapi
.fileTypes
.StdFileTypes
;
25 import com
.intellij
.openapi
.project
.Project
;
26 import com
.intellij
.openapi
.util
.Comparing
;
27 import com
.intellij
.openapi
.vfs
.VirtualFile
;
28 import com
.intellij
.openapi
.vfs
.VirtualFileManager
;
29 import com
.intellij
.psi
.PsiFile
;
30 import com
.intellij
.psi
.PsiManager
;
31 import com
.intellij
.util
.SmartList
;
32 import com
.intellij
.util
.containers
.ContainerUtil
;
33 import org
.jetbrains
.annotations
.NonNls
;
34 import org
.jetbrains
.annotations
.NotNull
;
35 import org
.jetbrains
.annotations
.Nullable
;
37 import java
.util
.Collections
;
38 import java
.util
.Comparator
;
39 import java
.util
.List
;
41 public class ResourceBundleImpl
implements ResourceBundle
{
42 @NotNull private final VirtualFile myBaseDirectory
;
43 @NotNull private final String myBaseName
;
44 @NonNls private static final String RESOURCE_BUNDLE_PREFIX
= "resourceBundle:";
46 public ResourceBundleImpl(@NotNull VirtualFile baseDirectory
, @NotNull String baseName
) {
47 myBaseDirectory
= baseDirectory
;
48 myBaseName
= baseName
;
51 public static final ResourceBundle NULL
= new ResourceBundle() {
53 public List
<PropertiesFile
> getPropertiesFiles(final Project project
) {
54 return Collections
.emptyList();
58 public PropertiesFile
getDefaultPropertiesFile(final Project project
) {
59 throw new IllegalStateException();
63 public String
getBaseName() {
68 public VirtualFile
getBaseDirectory() {
69 throw new IllegalStateException();
74 public List
<PropertiesFile
> getPropertiesFiles(final Project project
) {
75 VirtualFile
[] children
= myBaseDirectory
.getChildren();
76 List
<PropertiesFile
> result
= new SmartList
<PropertiesFile
>();
77 FileTypeManager fileTypeManager
= FileTypeManager
.getInstance();
78 PsiManager psiManager
= PsiManager
.getInstance(project
);
79 for (VirtualFile file
: children
) {
80 if (!file
.isValid() || fileTypeManager
.getFileTypeByFile(file
) != StdFileTypes
.PROPERTIES
) continue;
81 if (Comparing
.strEqual(PropertiesUtil
.getBaseName(file
), myBaseName
)) {
82 PsiFile psiFile
= psiManager
.findFile(file
);
83 if (psiFile
instanceof PropertiesFile
) {
84 result
.add((PropertiesFile
)psiFile
);
92 public PropertiesFile
getDefaultPropertiesFile(final Project project
) {
93 List
<PropertiesFile
> files
= getPropertiesFiles(project
);
94 // put default properties file first
95 ContainerUtil
.quickSort(files
, new Comparator
<PropertiesFile
>() {
96 public int compare(final PropertiesFile o1
, final PropertiesFile o2
) {
97 return Comparing
.compare(o1
.getName(), o2
.getName());
104 public String
getBaseName() {
108 public boolean equals(final Object o
) {
109 if (this == o
) return true;
110 if (o
== null || getClass() != o
.getClass()) return false;
112 final ResourceBundleImpl resourceBundle
= (ResourceBundleImpl
)o
;
114 if (!myBaseDirectory
.equals(resourceBundle
.myBaseDirectory
)) return false;
115 if (!myBaseName
.equals(resourceBundle
.myBaseName
)) return false;
120 public int hashCode() {
121 int result
= myBaseDirectory
.hashCode();
122 result
= 29 * result
+ myBaseName
.hashCode();
127 public static ResourceBundle
createByUrl(String url
) {
128 if (!url
.startsWith(RESOURCE_BUNDLE_PREFIX
)) return null;
130 String defaultPropertiesUrl
= url
.substring(RESOURCE_BUNDLE_PREFIX
.length());
131 final int idx
= defaultPropertiesUrl
.lastIndexOf('/');
132 if (idx
== -1) return null;
133 String baseDir
= defaultPropertiesUrl
.substring(0, idx
);
134 String baseName
= defaultPropertiesUrl
.substring(idx
+ 1);
135 VirtualFile baseDirectory
= VirtualFileManager
.getInstance().findFileByUrl(baseDir
);
136 if (baseDirectory
!= null) {
137 return new ResourceBundleImpl(baseDirectory
, baseName
);
142 public String
getUrl() {
143 return RESOURCE_BUNDLE_PREFIX
+getBaseDirectory() + "/" + getBaseName();
147 public VirtualFile
getBaseDirectory() {
148 return myBaseDirectory
;