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
.util
;
18 import com
.intellij
.util
.io
.URLUtil
;
19 import org
.jetbrains
.annotations
.NonNls
;
20 import org
.jetbrains
.annotations
.NotNull
;
22 import java
.io
.BufferedInputStream
;
23 import java
.io
.IOException
;
24 import java
.io
.InputStream
;
25 import java
.io
.InputStreamReader
;
27 import java
.util
.ArrayList
;
28 import java
.util
.List
;
29 import java
.util
.Locale
;
34 public class ResourceUtil
{
35 @NonNls private static final String ENCODING_UTF_8
= "UTF-8";
37 private ResourceUtil() {
40 public static URL
getResource(@NotNull Class loaderClass
, @NonNls @NotNull String basePath
, @NonNls @NotNull String fileName
) {
41 if (basePath
.endsWith("/")) basePath
= basePath
.substring(0, basePath
.length() - 1);
43 final List
<String
> bundles
= calculateBundleNames(basePath
, Locale
.getDefault());
44 for (String bundle
: bundles
) {
45 URL url
= loaderClass
.getResource(bundle
+ "/" + fileName
);
46 if (url
== null) continue;
51 catch (IOException e
) {
58 return loaderClass
.getResource(basePath
+ "/" + fileName
);
62 * Copied from java.util.ResourceBundle implementation
64 private static List
<String
> calculateBundleNames(String baseName
, Locale locale
) {
65 final List
<String
> result
= new ArrayList
<String
>(3);
66 final String language
= locale
.getLanguage();
67 final int languageLength
= language
.length();
68 final String country
= locale
.getCountry();
69 final int countryLength
= country
.length();
70 final String variant
= locale
.getVariant();
71 final int variantLength
= variant
.length();
73 result
.add(0, baseName
);
75 if (languageLength
+ countryLength
+ variantLength
== 0) {
76 //The locale is "", "", "".
80 final StringBuffer temp
= new StringBuffer(baseName
);
82 temp
.append(language
);
83 if (languageLength
> 0) {
84 result
.add(0, temp
.toString());
87 if (countryLength
+ variantLength
== 0) {
93 if (countryLength
> 0) {
94 result
.add(0, temp
.toString());
97 if (variantLength
== 0) {
101 temp
.append(variant
);
102 result
.add(0, temp
.toString());
108 public static String
loadText(@NotNull URL url
) throws IOException
{
109 InputStream inputStream
= new BufferedInputStream(URLUtil
.openStream(url
));
111 InputStreamReader reader
= null;
113 reader
= new InputStreamReader(inputStream
, ENCODING_UTF_8
);
114 StringBuffer text
= new StringBuffer();
115 char[] buf
= new char[5000];
116 while (reader
.ready()) {
117 final int length
= reader
.read(buf
);
118 if (length
== -1) break;
119 text
.append(buf
, 0, length
);
121 return text
.toString();
124 if (reader
!= null) {