|
16 | 16 | import java.util.ArrayList; |
17 | 17 | import java.util.Arrays; |
18 | 18 | import java.util.Comparator; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.HashSet; |
19 | 21 | import java.util.List; |
| 22 | +import java.util.Map; |
20 | 23 |
|
21 | 24 | import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
22 | 25 | import org.eclipse.core.runtime.preferences.InstanceScope; |
@@ -85,6 +88,33 @@ public static void addChildWithIndent(Node parent, Node child) { |
85 | 88 | appendTextNode(parent, false); |
86 | 89 | } |
87 | 90 |
|
| 91 | + public static void updateAttributes(Element oldElement, Element newElement) { |
| 92 | + Map<String, String> previous = asMap(oldElement.getAttributes()); |
| 93 | + Map<String, String> current = asMap(newElement.getAttributes()); |
| 94 | + HashSet<String> all = new HashSet<>(); |
| 95 | + all.addAll(previous.keySet()); |
| 96 | + all.addAll(current.keySet()); |
| 97 | + for (String attr : all) { |
| 98 | + if (current.containsKey(attr)) { |
| 99 | + // if the current element contains the attribute then set it to |
| 100 | + // the current value |
| 101 | + oldElement.setAttribute(attr, current.get(attr)); |
| 102 | + } else { |
| 103 | + // otherwise remove the attribute |
| 104 | + oldElement.removeAttribute(attr); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static Map<String,String> asMap(NamedNodeMap attributes) { |
| 110 | + HashMap<String, String> map = new HashMap<>(); |
| 111 | + for (int i = 0; i < attributes.getLength(); i++) { |
| 112 | + Node item = attributes.item(i); |
| 113 | + map.put(item.getNodeName(), item.getNodeValue()); |
| 114 | + } |
| 115 | + return map; |
| 116 | + } |
| 117 | + |
88 | 118 | /** |
89 | 119 | * For all the children of the parentElement, if any of the oldElements are not |
90 | 120 | * found in the newElements list then they are removed. All newElements that are |
|
0 commit comments