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

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

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 java.nio.file.Path;
4
import java.util.ArrayList;
5
import java.util.List;
6
import javax.xml.namespace.QName;
7

8
import org.w3c.dom.Attr;
9
import org.w3c.dom.Element;
10
import org.w3c.dom.NamedNodeMap;
11
import org.w3c.dom.Node;
12
import org.w3c.dom.NodeList;
13

14
import com.devonfw.tools.ide.merge.xmlmerger.MergeStrategy;
15
import com.devonfw.tools.ide.merge.xmlmerger.XmlMerger;
16

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

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

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

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

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

42
  public Element getElement() {
43

44
    return this.element;
3✔
45
  }
46

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

52
    return this.documentPath;
3✔
53
  }
54

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

164
    for (int i = 0; i < nodeList.getLength(); i++) {
8✔
165
      Node node = nodeList.item(i);
4✔
166
      if (node.getNodeType() == Node.ELEMENT_NODE) {
4✔
167
        childElements.add(new MergeElement((Element) node, this.documentPath));
10✔
168
      }
169
    }
170
    return childElements;
2✔
171
  }
172
}
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