IDEA-51944
[fedora-idea.git] / platform / util / src / com / intellij / util / ResourceUtil.java
blob0ba7cc955ffc24dd6b660d790360119dc71cfe8a
1 /*
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;
26 import java.net.URL;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Locale;
31 /**
32 * @author max
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;
48 try {
49 url.openConnection();
51 catch (IOException e) {
52 continue;
55 return url;
58 return loaderClass.getResource(basePath + "/" + fileName);
61 /**
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 "", "", "".
77 return result;
80 final StringBuffer temp = new StringBuffer(baseName);
81 temp.append('_');
82 temp.append(language);
83 if (languageLength > 0) {
84 result.add(0, temp.toString());
87 if (countryLength + variantLength == 0) {
88 return result;
91 temp.append('_');
92 temp.append(country);
93 if (countryLength > 0) {
94 result.add(0, temp.toString());
97 if (variantLength == 0) {
98 return result;
100 temp.append('_');
101 temp.append(variant);
102 result.add(0, temp.toString());
104 return result;
107 @NotNull
108 public static String loadText(@NotNull URL url) throws IOException {
109 InputStream inputStream = new BufferedInputStream(URLUtil.openStream(url));
111 InputStreamReader reader = null;
112 try {
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();
123 finally {
124 if (reader != null) {
125 reader.close();