• 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

71.93
/exist-core/src/main/java/org/exist/xquery/functions/fn/transform/URIResolution.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 org.exist.dom.persistent.NodeProxy;
27
import org.exist.security.PermissionDeniedException;
28
import org.exist.xmldb.XmldbURI;
29
import org.exist.xquery.ErrorCodes;
30
import org.exist.xquery.Expression;
31
import org.exist.xquery.XPathException;
32
import org.exist.xquery.XQueryContext;
33
import org.exist.xquery.util.DocUtils;
34
import org.exist.xquery.value.AnyURIValue;
35
import org.exist.xquery.value.Sequence;
36
import org.exist.xquery.value.Type;
37
import org.w3c.dom.Node;
38

39
import javax.xml.transform.Source;
40
import javax.xml.transform.TransformerException;
41
import javax.xml.transform.URIResolver;
42
import javax.xml.transform.dom.DOMSource;
43
import java.net.URI;
44
import java.net.URISyntaxException;
45

46
public class URIResolution {
×
47

48
    /**
49
     * URI resolution, the core should be the same as for fn:resolve-uri
50
     * @param relative URI to resolve
51
     * @param base to resolve against
52
     * @return resolved URI
53
     * @throws URISyntaxException if resolution is not possible
54
     */
55
    static AnyURIValue resolveURI(final AnyURIValue relative, final AnyURIValue base) throws URISyntaxException, XPathException {
56
        var relativeURI = new URI(relative.getStringValue());
1✔
57
        if (relativeURI.isAbsolute()) {
1✔
58
            return relative;
1✔
59
        }
60
        var baseURI = new URI(base.getStringValue() );
1✔
61
        if (!baseURI.isAbsolute()) {
1✔
62
            return relative;
1✔
63
        }
64
        try {
65
            var xBase = XmldbURI.xmldbUriFor(baseURI);
1✔
66
            var resolved = xBase.getURI().resolve(relativeURI);
1✔
67
            return new AnyURIValue(XmldbURI.XMLDB_URI_PREFIX + resolved);
1✔
68
        } catch (URISyntaxException e) {
1✔
69
            return new AnyURIValue(baseURI.resolve(relativeURI));
1✔
70
        }
71
    }
72

73
    public static class CompileTimeURIResolver implements URIResolver {
74

75
        private final XQueryContext xQueryContext;
76
        private final Expression containingExpression;
77

78
        public CompileTimeURIResolver(XQueryContext xQueryContext, Expression containingExpression) {
1✔
79
            this.xQueryContext = xQueryContext;
1✔
80
            this.containingExpression = containingExpression;
1✔
81
        }
1✔
82

83
        @Override
84
        public Source resolve(final String href, final String base) throws TransformerException {
85

86
            try {
87
                final AnyURIValue baseURI = new AnyURIValue(base);
1✔
88
                final AnyURIValue hrefURI = new AnyURIValue(href);
1✔
89
                var resolved = resolveURI(hrefURI, baseURI);
1✔
90
                return resolveDocument(resolved.getStringValue());
1✔
91
            } catch (URISyntaxException e) {
×
92
                throw new TransformerException(
×
93
                    "Failed to resolve " + href + " against " + base, e);
×
94
            } catch (XPathException e) {
×
95
                throw new TransformerException(
×
96
                    "Failed to find document as result of resolving " + href + " against " + base, e);
×
97
            }
98
        }
99

100
        protected Source resolveDocument(final String location) throws XPathException {
101
            return URIResolution.resolveDocument(location, xQueryContext, containingExpression);
×
102
        }
103
    }
104

105
    /**
106
     * Resolve an absolute document location, stylesheet or included source
107
     *
108
     * @param location of the stylesheet
109
     * @return the resolved stylesheet as a source
110
     * @throws org.exist.xquery.XPathException if the item does not exist, or is not a document
111
     */
112
    static Source resolveDocument(final String location, final XQueryContext xQueryContext, Expression containingExpression) throws XPathException {
113

114
        final Sequence document;
115
        try {
116
            document = DocUtils.getDocument(xQueryContext, location);
1✔
117
        } catch (final PermissionDeniedException e) {
1✔
118
            throw new XPathException(containingExpression, ErrorCodes.FODC0002,
1✔
119
                "Can not access '" + location + "'" + e.getMessage());
1✔
120
        }
121
        if (document == null || document.isEmpty()) {
1!
122
            throw new XPathException(containingExpression, ErrorCodes.FODC0002,
1✔
123
                "No document found at location '"+ location);
1✔
124
        }
125
        if (document.hasOne() && Type.subTypeOf(document.getItemType(), Type.NODE)) {
1!
126
            if (document instanceof NodeProxy proxy) {
1!
127
                return new DOMSource(proxy.getNode());
×
128
            }
129
            else if (document.itemAt(0) instanceof Node node) {
1!
130
                return new DOMSource(node);
1✔
131
            }
132
        }
133
        throw new XPathException(containingExpression, ErrorCodes.FODC0002,
×
134
            "Location '"+ location + "' returns an item which is not a document node");
×
135
    }
136
}
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