• 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

90.91
/exist-core/src/main/java/org/exist/resolver/ResolverFactory.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.resolver;
25

26
import com.evolvedbinary.j8fu.tuple.Tuple2;
27
import org.xml.sax.InputSource;
28
import org.xmlresolver.Resolver;
29
import org.xmlresolver.ResolverFeature;
30
import org.xmlresolver.XMLResolverConfiguration;
31

32
import java.net.URI;
33
import java.net.URISyntaxException;
34
import java.nio.file.Paths;
35
import java.util.List;
36
import java.util.Optional;
37
import java.util.stream.Collectors;
38

39
import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple;
40

41
/**
42
 * Factory for creating Resolvers.
43
 *
44
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
45
 */
46
public interface ResolverFactory {
47

48
    /**
49
     * Create a Resolver that is configured for specific catalogs.
50
     *
51
     * @param catalogs the list of catalogs, the first entry in the tuple is their URI (and/or location),
52
     *                 and the optional second argument is an InputSource for obtaining them directly.
53
     *
54
     * @return the resolver
55
     *
56
     * @throws URISyntaxException if one of the catalog URI is invalid
57
     */
58
    static Resolver newResolver(final List<Tuple2<String, Optional<InputSource>>> catalogs) throws URISyntaxException {
59
        final XMLResolverConfiguration resolverConfiguration = new XMLResolverConfiguration();
1✔
60
        resolverConfiguration.setFeature(ResolverFeature.RESOLVER_LOGGER_CLASS, "org.xmlresolver.logging.SystemLogger");
1✔
61
        resolverConfiguration.setFeature(ResolverFeature.CATALOG_LOADER_CLASS, "org.xmlresolver.loaders.ValidatingXmlLoader");
1✔
62
        resolverConfiguration.setFeature(ResolverFeature.CLASSPATH_CATALOGS, true);
1✔
63
        resolverConfiguration.setFeature(ResolverFeature.URI_FOR_SYSTEM, true);
1✔
64
        // See: https://xmlresolver.org/ch06.html#xml.catalog.alwaysResolve
65
        resolverConfiguration.setFeature(ResolverFeature.ALWAYS_RESOLVE, false);
1✔
66

67
        for (final Tuple2<String, Optional<InputSource>> catalog : catalogs) {
1✔
68
            String strCatalogUri = catalog._1;
1✔
69
            strCatalogUri = sanitizeCatalogUri(strCatalogUri);
1✔
70
            if (catalog._2.isPresent()) {
1✔
71
                resolverConfiguration.addCatalog(new URI(strCatalogUri), catalog._2.get());
1✔
72
            } else {
1✔
73
                resolverConfiguration.addCatalog(strCatalogUri);
1✔
74
            }
75
        }
76

77
        return new Resolver(resolverConfiguration);
1✔
78
    }
79

80
    /**
81
     * Sanitize the Catalog URI.
82
     *
83
     * Mainly deals with converting Windows file paths to URI.
84
     *
85
     * @param strCatalogUri The Catalog URI string
86
     *
87
     * @return The sanitized Catalog URI string
88
     */
89
    static String sanitizeCatalogUri(String strCatalogUri) {
90
        if (strCatalogUri.indexOf('\\') > -1) {
1!
91
            // convert from Windows file path
92
            strCatalogUri = Paths.get(strCatalogUri).toUri().toString();
×
93
        }
94
        return strCatalogUri;
1✔
95
    }
96

97
    /**
98
     * Catalog URI if stored in database must start with
99
     * URI Scheme xmldb:// (and NOT xmldb:exist://) so that
100
     * the {@link Resolver} can use {@link org.exist.protocolhandler.protocols.xmldb.Handler}
101
     * to resolve any relative URI resources from the database.
102
     *
103
     * @param catalogs the catalog URIs
104
     *
105
     * @return the catalog URIs suitable for use with the {@link Resolver}.
106
     */
107
    static List<Tuple2<String, Optional<InputSource>>> fixupExistCatalogUris(final List<Tuple2<String, Optional<InputSource>>> catalogs) {
108
        return catalogs.stream().map(catalog -> Tuple(fixupExistCatalogUri(catalog._1), catalog._2)).collect(Collectors.toList());
×
109
    }
110

111
    /**
112
     * Catalog URI if stored in database must start with
113
     * URI Scheme xmldb:// (and NOT xmldb:exist://) so that
114
     * the {@link Resolver} can use {@link org.exist.protocolhandler.protocols.xmldb.Handler}
115
     * to resolve any relative URI resources from the database.
116
     *
117
     * @param catalogUri the catalog URI
118
     *
119
     * @return the catalog URI suitable for use with the {@link Resolver}.
120
     */
121
    static String fixupExistCatalogUri(String catalogUri) {
122
        if (catalogUri.startsWith("xmldb:exist://")) {
1✔
123
            catalogUri = catalogUri.replace("xmldb:exist://", "xmldb://");
1✔
124
        } else if (catalogUri.startsWith("/db")) {
1✔
125
            catalogUri = "xmldb://" + catalogUri;
1✔
126
        }
127
        return catalogUri;
1✔
128
    }
129

130
}
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