summaryrefslogtreecommitdiff
path: root/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/xml/XmlPropertiesFileImpl.java
blob: 4b8953f47e9c83ad99fc4834081c30ca223d8db1 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright 2000-2012 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.
 */
package com.intellij.lang.properties.xml;

import com.intellij.lang.properties.IProperty;
import com.intellij.lang.properties.PropertiesImplUtil;
import com.intellij.lang.properties.PropertiesUtil;
import com.intellij.lang.properties.ResourceBundle;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.psi.Property;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.reference.SoftLazyValue;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.MultiMap;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author Dmitry Avdeev
 *         Date: 7/26/11
 */
public class XmlPropertiesFileImpl extends XmlPropertiesFile {

  private static final Key<CachedValue<PropertiesFile>> KEY = Key.create("xml properties file");
  private final XmlFile myFile;
  private final SoftLazyValue<MultiMap<String, IProperty>> myPropertiesMap = new SoftLazyValue<MultiMap<String, IProperty>>() {
    @NotNull
    @Override
    protected MultiMap<String, IProperty> compute() {
      XmlTag rootTag = myFile.getRootTag();
      if (rootTag == null) {
        return MultiMap.emptyInstance();
      }

      XmlTag[] entries = rootTag.findSubTags("entry");
      MultiMap<String, IProperty> map = new MultiMap<String, IProperty>();

      for (XmlTag entry : entries) {
        XmlProperty property = new XmlProperty(entry, XmlPropertiesFileImpl.this);
        map.putValue(property.getKey(), property);
      }
      return map;
    }
  };

  private XmlPropertiesFileImpl(XmlFile file) {
    myFile = file;
  }

  @NotNull
  @Override
  public PsiFile getContainingFile() {
    return myFile;
  }

  @NotNull
  @Override
  public List<IProperty> getProperties() {
    return new ArrayList<IProperty>(myPropertiesMap.getValue().values());
  }

  @Override
  public IProperty findPropertyByKey(@NotNull @NonNls String key) {
    Collection<IProperty> properties = myPropertiesMap.getValue().get(key);
    return properties.isEmpty() ? null : properties.iterator().next();
  }

  @NotNull
  @Override
  public List<IProperty> findPropertiesByKey(@NotNull @NonNls String key) {
    return new ArrayList<IProperty>(myPropertiesMap.getValue().get(key));
  }

  @NotNull
  @Override
  public ResourceBundle getResourceBundle() {
    return PropertiesImplUtil.getResourceBundle(this);
  }

  @NotNull
  @Override
  public Locale getLocale() {
    return PropertiesUtil.getLocale(getVirtualFile());
  }

  @NotNull
  @Override
  public PsiElement addProperty(@NotNull IProperty property) throws IncorrectOperationException {
    return null;
  }

  @NotNull
  @Override
  public PsiElement addPropertyAfter(@NotNull Property property, @Nullable Property anchor) throws IncorrectOperationException {
    return null;
  }

  @Override
  public IProperty addProperty(String key, String value) {
    XmlTag rootTag = myFile.getRootTag();
    XmlTag entry = rootTag.createChildTag("entry", "", value, false);
    entry.setAttribute("key", key);
    rootTag.addSubTag(entry, false);
    return new XmlProperty(entry, this);
  }


  public static PropertiesFile getPropertiesFile(final PsiFile file) {
    CachedValuesManager manager = CachedValuesManager.getManager(file.getProject());
    if (file instanceof XmlFile) {
      return manager.getCachedValue(file, KEY,
                                    new CachedValueProvider<PropertiesFile>() {
                                      @Override
                                      public Result<PropertiesFile> compute() {
                                        PropertiesFile value =
                                          XmlPropertiesIndex.isPropertiesFile((XmlFile)file)
                                          ? new XmlPropertiesFileImpl((XmlFile)file)
                                          : null;
                                        return Result.create(value, file);
                                      }
                                    }, false
      );
    }
    return null;
  }

  @NotNull
  @Override
  public Map<String, String> getNamesMap() {
    Map<String, String> result = new THashMap<String, String>();
    for (IProperty property : getProperties()) {
      result.put(property.getUnescapedKey(), property.getValue());
    }
    return result;
  }

  @Override
  public String getName() {
    return getContainingFile().getName();
  }

  @Override
  public VirtualFile getVirtualFile() {
    return getContainingFile().getVirtualFile();
  }

  @Override
  public PsiDirectory getParent() {
    return getContainingFile().getParent();
  }

  @Override
  public Project getProject() {
    return getContainingFile().getProject();
  }

  @Override
  public String getText() {
    return getContainingFile().getText();
  }

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

    XmlPropertiesFileImpl that = (XmlPropertiesFileImpl)o;

    if (!myFile.equals(that.myFile)) return false;

    return true;
  }

  @Override
  public int hashCode() {
    return myFile.hashCode();
  }
}