summaryrefslogtreecommitdiff
path: root/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleImpl.java
blob: 1ac96a96f6afd7c4d2012443f61479c76914a174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @author Alexey
 */
package com.intellij.lang.properties;

import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiManager;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Comparator;
import java.util.List;

public class ResourceBundleImpl extends ResourceBundle {
  @NonNls private static final String RESOURCE_BUNDLE_PREFIX = "resourceBundle:";
  @NotNull protected final VirtualFile myBaseDirectory;
  @NotNull protected final String myBaseName;

  public ResourceBundleImpl(@NotNull VirtualFile baseDirectory, @NotNull String baseName) {
    myBaseDirectory = baseDirectory;
    myBaseName = baseName;
  }

  @NotNull
  public List<PropertiesFile> getPropertiesFiles(final Project project) {
    VirtualFile[] children = myBaseDirectory.getChildren();
    List<PropertiesFile> result = new SmartList<PropertiesFile>();
    PsiManager psiManager = PsiManager.getInstance(project);
    for (VirtualFile file : children) {
      if (!file.isValid() || file.getExtension() == null) continue;
      if (Comparing.strEqual(PropertiesUtil.getBaseName(file), myBaseName)) {
        PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(psiManager.findFile(file));
        if (propertiesFile != null) {
          result.add(propertiesFile);
        }
      }
    }
    return result;
  }

  @NotNull
  public PropertiesFile getDefaultPropertiesFile(final Project project) {
    List<PropertiesFile> files = getPropertiesFiles(project);
    // put default properties file first
    ContainerUtil.quickSort(files, new Comparator<PropertiesFile>() {
      public int compare(final PropertiesFile o1, final PropertiesFile o2) {
        return Comparing.compare(o1.getName(), o2.getName());
      }
    });
    return files.get(0);
  }

  @NotNull
  public String getBaseName() {
    return myBaseName;
  }

  public boolean equals(final Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    final ResourceBundleImpl resourceBundle = (ResourceBundleImpl)o;

    if (!myBaseDirectory.equals(resourceBundle.myBaseDirectory)) return false;
    if (!myBaseName.equals(resourceBundle.myBaseName)) return false;

    return true;
  }

  public int hashCode() {
    int result = myBaseDirectory.hashCode();
    result = 29 * result + myBaseName.hashCode();
    return result;
  }

  @Nullable
  public static ResourceBundle createByUrl(String url) {
    if (!url.startsWith(RESOURCE_BUNDLE_PREFIX)) return null;

    String defaultPropertiesUrl = url.substring(RESOURCE_BUNDLE_PREFIX.length());
    final int idx = defaultPropertiesUrl.lastIndexOf('/');
    if (idx == -1) return null;
    String baseDir = defaultPropertiesUrl.substring(0, idx);
    String baseName = defaultPropertiesUrl.substring(idx + 1);
    VirtualFile baseDirectory = VirtualFileManager.getInstance().findFileByUrl(baseDir);
    if (baseDirectory != null) {
      return new ResourceBundleImpl(baseDirectory, baseName);
    }
    return null;
  }

  public String getUrl() {
    return RESOURCE_BUNDLE_PREFIX +getBaseDirectory() + "/" + getBaseName();
  }

  @NotNull
  public VirtualFile getBaseDirectory() {
    return myBaseDirectory;
  }

}