• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 9905881806

12 Jul 2024 09:46AM UTC coverage: 61.162% (-0.2%) from 61.387%
9905881806

push

github

web-flow
#403: Improve XML merger (#456)

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8329 relevant lines covered (63.59%)

2.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.02
cli/src/main/java/com/devonfw/tools/ide/merge/xmlmerger/model/MergeElement.java
1
package com.devonfw.tools.ide.merge.xmlmerger.model;
2

3
import com.devonfw.tools.ide.merge.xmlmerger.MergeStrategy;
4
import com.devonfw.tools.ide.merge.xmlmerger.XmlMerger;
5
import org.w3c.dom.Attr;
6
import org.w3c.dom.Element;
7
import org.w3c.dom.NamedNodeMap;
8
import org.w3c.dom.Node;
9
import org.w3c.dom.NodeList;
10

11
import javax.xml.namespace.QName;
12
import java.nio.file.Path;
13
import java.util.ArrayList;
14
import java.util.List;
15

16
/**
17
 * Represents an XML element during the merge process.
18
 */
19
public class MergeElement {
20

21
  /**
22
   * The XML element represented by this MergeElement.
23
   */
24
  private final Element element;
25

26
  /**
27
   * The path of the document where this element resides.
28
   */
29
  private final Path documentPath;
30

31
  /**
32
   * @param element the {@link Element} to be represented.
33
   * @param documentPath the {@link Path} to the document this element belongs to.
34
   */
35
  public MergeElement(Element element, Path documentPath) {
2✔
36

37
    this.element = element;
3✔
38
    this.documentPath = documentPath;
3✔
39
  }
1✔
40

41
  public Element getElement() {
42

43
    return element;
3✔
44
  }
45

46
  /**
47
   * @return the path to the document this element belongs to.
48
   */
49
  public Path getDocumentPath() {
50

51
    return documentPath;
3✔
52
  }
53

54
  /**
55
   * Retrieves the merge strategy associated with this MergeElement.
56
   *
57
   * @return the merge strategy
58
   */
59
  public String getMergingStrategy() {
60

61
    String strategy = this.element.getAttributeNS(XmlMerger.MERGE_NS_URI, "strategy").toLowerCase();
7✔
62
    if (!strategy.isEmpty()) {
3✔
63
      return strategy;
2✔
64
    }
65

66
    // Inherit merging strategy from parent
67
    Element parent = getParentElement();
3✔
68
    if (parent != null) {
2✔
69
      return new MergeElement(parent, this.documentPath).getMergingStrategy();
8✔
70
    }
71
    return MergeStrategy.KEEP.name(); // should the default be keep?
3✔
72
  }
73

74
  /**
75
   * Retrieves the value of the merge:id attribute of this MergeElement.
76
   *
77
   * @return the ID attribute value
78
   */
79
  public String getId() {
80

81
    return this.element.getAttributeNS(XmlMerger.MERGE_NS_URI, "id");
6✔
82
  }
83

84
  /**
85
   * Retrieves the qualified name (URI + local name) of this MergeElement.
86
   *
87
   * @return the QName
88
   */
89
  public QName getQName() {
90

91
    String namespaceURI = this.element.getNamespaceURI();
4✔
92
    String localName = this.element.getLocalName();
4✔
93
    return new QName(namespaceURI, localName);
6✔
94
  }
95

96
  /**
97
   * Retrieves the parent element of this MergeElement.
98
   *
99
   * @return the parent element, or {@code null} if there is no parent
100
   */
101
  private Element getParentElement() {
102

103
    Node parentNode = element.getParentNode();
4✔
104
    if (parentNode != null && parentNode.getNodeType() == Node.ELEMENT_NODE) {
6!
105
      return (Element) parentNode;
3✔
106
    }
107
    return null;
2✔
108
  }
109

110
  /**
111
   * Retrieves the attributes of this MergeElement.
112
   *
113
   * @return a list of {@link MergeAttribute} objects representing the attributes, if there are no attributes, the list is empty.
114
   */
115
  public List<MergeAttribute> getElementAttributes() {
116

117
    NamedNodeMap attributes = element.getAttributes();
4✔
118
    List<MergeAttribute> attributeList = new ArrayList<>();
4✔
119
    for (int i = 0; i < attributes.getLength(); i++) {
8✔
120
      attributeList.add(new MergeAttribute((Attr) attributes.item(i)));
10✔
121
    }
122
    return attributeList;
2✔
123
  }
124

125
  /**
126
   * Checks if this MergeElement is a root element.
127
   *
128
   * @return {@code true} if this element is a root element, {@code false} otherwise
129
   */
130
  public boolean isRootElement() {
131

132
    return element.getParentNode().getNodeType() == Node.DOCUMENT_NODE;
×
133
  }
134

135
  /**
136
   * Retrieves the XPath of this MergeElement with no criterion. E.g. /root/.../element
137
   *
138
   * @return the XPath
139
   */
140
  public String getXPath() {
141

142
    StringBuilder xpath = new StringBuilder();
×
143
    Node current = element;
×
144
    while (current != null && current.getNodeType() == Node.ELEMENT_NODE) {
×
145
      Element currentElement = (Element) current;
×
146
      String tagName = currentElement.getTagName();
×
147
      xpath.insert(0, "/" + tagName);
×
148
      current = current.getParentNode();
×
149
    }
×
150
    return xpath.toString();
×
151
  }
152

153
  /**
154
   * Retrieves the child elements of this MergeElement.
155
   *
156
   * @return a list of {@link MergeElement} objects representing the child elements
157
   */
158
  public List<MergeElement> getChildElements() {
159

160
    List<MergeElement> childElements = new ArrayList<>();
4✔
161
    NodeList nodeList = element.getChildNodes();
4✔
162

163
    for (int i = 0; i < nodeList.getLength(); i++) {
8✔
164
      Node node = nodeList.item(i);
4✔
165
      if (node.getNodeType() == Node.ELEMENT_NODE) {
4✔
166
        childElements.add(new MergeElement((Element) node, this.documentPath));
10✔
167
      }
168
    }
169
    return childElements;
2✔
170
  }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc