IDEADEV-41647 Path Variables aren't detected for Artifacts Configurations [rvwd by...
[fedora-idea.git] / platform / platform-impl / src / com / intellij / openapi / components / PathMacroMap.java
blob722665519c4e361f7610fb8650caa605adb4b227
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.openapi.components;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.util.NotNullFunction;
20 import org.jdom.Attribute;
21 import org.jdom.Comment;
22 import org.jdom.Element;
23 import org.jdom.Text;
24 import org.jetbrains.annotations.Nullable;
26 import java.io.File;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
32 /**
33 * @author Eugene Zhuravlev
34 * Date: Dec 6, 2004
36 public abstract class PathMacroMap {
37 private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.components.PathMacroMap");
38 protected final Map<String, String> myMacroMap;
40 protected PathMacroMap() {
41 myMacroMap = new LinkedHashMap<String,String>();
44 public void putAll(PathMacroMap pathMacroMap) {
45 putAll(pathMacroMap.myMacroMap);
48 public void putAll(Map<String, String> macroMap) {
49 myMacroMap.putAll(macroMap);
52 public void put(String fromText, String toText) {
53 myMacroMap.put(fromText, toText);
56 public abstract String substitute(String text, boolean caseSensitive);
58 public final void substitute(Element e, boolean caseSensitive) {
59 substitute(e, caseSensitive, false);
62 public final void substitute(Element e, boolean caseSensitive, final boolean recursively, @Nullable final NotNullFunction<Object, Boolean> filter) {
63 List content = e.getContent();
64 for (Object child : content) {
65 if (child instanceof Element) {
66 Element element = (Element)child;
67 substitute(element, caseSensitive, recursively, filter);
69 else if (child instanceof Text) {
70 Text t = (Text)child;
71 if (filter == null || filter.fun(t)) t.setText(recursively ? substituteRecursively(t.getText(), caseSensitive) : substitute(t.getText(), caseSensitive));
73 else if (child instanceof Comment) {
74 /*do not substitute in comments
75 Comment c = (Comment)child;
76 c.setText(substitute(c.getText(), caseSensitive, usedMacros));
79 else {
80 LOG.error("Wrong content: " + child.getClass());
84 List attributes = e.getAttributes();
85 for (final Object attribute1 : attributes) {
86 Attribute attribute = (Attribute)attribute1;
87 if (filter == null || filter.fun(attribute)) {
88 final String value = recursively
89 ? substituteRecursively(attribute.getValue(), caseSensitive)
90 : substitute(attribute.getValue(), caseSensitive);
91 attribute.setValue(value);
96 public final void substitute(Element e, boolean caseSensitive, final boolean recursively) {
97 substitute(e, caseSensitive, recursively, null);
100 public String substituteRecursively(String text, boolean caseSensitive) {
101 return substitute(text, caseSensitive);
104 public int size() {
105 return myMacroMap.size();
108 protected Set<Map.Entry<String, String>> entries() {
109 return myMacroMap.entrySet();
112 protected Set<String> keySet() {
113 return myMacroMap.keySet();
116 public String get(String key) {
117 return myMacroMap.get(key);
120 public static String quotePath(String path) {
121 path = path.replace(File.separatorChar, '/');
122 //path = StringUtil.replace(path, "&", "&amp;");
123 return path;
126 public int hashCode() {
127 return myMacroMap.hashCode();