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

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

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

86.15
/exist-core/src/main/java/org/exist/xquery/functions/fn/transform/TreeUtils.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.xquery.functions.fn.transform;
25

26
import it.unimi.dsi.fastutil.ints.IntArrayList;
27
import it.unimi.dsi.fastutil.ints.IntList;
28
import net.sf.saxon.s9api.XdmNode;
29
import org.exist.xquery.value.NodeValue;
30
import org.w3c.dom.Attr;
31
import org.w3c.dom.Document;
32
import org.w3c.dom.Node;
33

34
import javax.annotation.Nullable;
35
import java.util.ArrayList;
36
import java.util.List;
37

38
public class TreeUtils {
39

40
    private TreeUtils() {
41
        super();
42
    }
43

44
    static StringBuilder pathTo(final Node node) {
45
        if (node instanceof Document) {
1✔
46
            final Document document = (Document) node;
1✔
47
            return new StringBuilder().append(document.getDocumentURI());
1✔
48
        }
49
        final List<Node> priors = new ArrayList<>();
1✔
50
        Node prev = node;
1✔
51
        while (prev != null) {
1✔
52
            priors.add(prev);
1✔
53
            prev = prev.getPreviousSibling();
1✔
54
        }
55
        final Node parent = priors.get(0).getParentNode();
1✔
56
        final StringBuilder sb;
57
        if (parent == null || parent instanceof Document) {
1!
58
            sb = new StringBuilder();
1✔
59
        } else {
1✔
60
            sb = pathTo(parent).append('/');
×
61
        }
62
        for (final Node prior : priors) {
1✔
63
            sb.append(((NodeValue)prior).getQName()).append(';');
1✔
64
        }
65

66
        return sb;
1✔
67
    }
68

69
    static IntList treeIndex(Node node, final boolean implicitDocument) {
70
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
1✔
71
            node = ((Attr) node).getOwnerElement();
1✔
72
        }
73

74
        Node parent = node.getParentNode();
1✔
75
        if (parent == null) {
1✔
76
            if (node.getNodeType() != Node.DOCUMENT_NODE && implicitDocument) {
1!
77
                return IntArrayList.of(0);
1✔
78
            } else {
79
                return new IntArrayList();
1✔
80
            }
81
        }
82

83
        final IntList index = treeIndex(parent, implicitDocument);
1✔
84
        Node sibling = node.getPreviousSibling();
1✔
85
        int position = 0;
1✔
86
        while (sibling != null) {
1!
87
            position += 1;
×
88
            sibling = sibling.getPreviousSibling();
×
89
        }
90
        index.add(position);
1✔
91

92
        return index;
1✔
93
    }
94

95
    static @Nullable XdmNode xdmNodeAtIndex(final XdmNode xdmNode, final IntList index) {
96
        if (index.isEmpty()) {
1✔
97
            return xdmNode;
1✔
98
        } else {
99
            final int firstIndex = index.getInt(0);
1✔
100
            int i = 0;
1✔
101
            for (final XdmNode child : xdmNode.children()) {
1!
102
                if (i++ == firstIndex) {
1!
103
                    return xdmNodeAtIndex(child, index.subList(1, index.size()));
1✔
104
                }
105
            }
106
        }
107
        return null;
×
108
    }
109
}
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

© 2025 Coveralls, Inc