• 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

81.63
/exist-core/src/main/java/org/exist/resolver/XercesXmlResolverAdapter.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 org.apache.xerces.impl.dtd.XMLDTDDescription;
27
import org.apache.xerces.impl.xs.XSDDescription;
28
import org.apache.xerces.util.SAXInputSource;
29
import org.apache.xerces.util.XMLEntityDescriptionImpl;
30
import org.apache.xerces.xni.QName;
31
import org.apache.xerces.xni.XMLResourceIdentifier;
32
import org.apache.xerces.xni.XNIException;
33
import org.apache.xerces.xni.grammars.XMLSchemaDescription;
34
import org.apache.xerces.xni.parser.XMLEntityResolver;
35
import org.apache.xerces.xni.parser.XMLInputSource;
36
import org.exist.util.XMLReaderObjectFactory;
37
import org.xml.sax.*;
38
import org.xmlresolver.Resolver;
39

40
import javax.annotation.Nullable;
41
import java.io.IOException;
42

43
/**
44
 * Adapts an {@link org.xmlresolver.Resolver} for use
45
 * with Xerces SAX Parser by implementing {@link org.apache.xerces.xni.parser.XMLEntityResolver}.
46
 *
47
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
48
 */
49
public class XercesXmlResolverAdapter implements XMLEntityResolver {
50
    private final Resolver resolver;
51

52
    public XercesXmlResolverAdapter(final Resolver resolver) {
1✔
53
        this.resolver = resolver;
1✔
54
    }
1✔
55

56
    @Override
57
    public XMLInputSource resolveEntity(final XMLResourceIdentifier xmlResourceIdentifier) throws XNIException, IOException {
58

59
        try {
60
            // get the name
61
            final String name;
62
            if (xmlResourceIdentifier instanceof XSDDescription) {
1✔
63
                final QName triggeringComponent = ((XSDDescription) xmlResourceIdentifier).getTriggeringComponent();
1✔
64
                name = triggeringComponent != null ? triggeringComponent.localpart : null;
1✔
65
            } else if (xmlResourceIdentifier instanceof XMLSchemaDescription) {
1!
66
                final QName triggeringComponent = ((XMLSchemaDescription) xmlResourceIdentifier).getTriggeringComponent();
×
67
                name = triggeringComponent != null ? triggeringComponent.localpart : null;
×
68
            } else if (xmlResourceIdentifier instanceof XMLEntityDescriptionImpl) {
1✔
69
                name = ((XMLEntityDescriptionImpl)xmlResourceIdentifier).getEntityName();
1✔
70
            } else if (xmlResourceIdentifier instanceof XMLDTDDescription) {
1!
71
                name = ((XMLDTDDescription)xmlResourceIdentifier).getRootName();
1✔
72
            } else {
1✔
73
                name = null;
×
74
            }
75

76
            // get the systemId
77
            final String systemId;
78
            if (xmlResourceIdentifier.getExpandedSystemId() !=  null) {
1✔
79
                systemId = xmlResourceIdentifier.getExpandedSystemId();
1✔
80
            } else {
1✔
81
                systemId = xmlResourceIdentifier.getNamespace();
1✔
82
            }
83

84
//            System.out.println(String.format("xri=(name=%s publicId=%s baseSystemId=%s systemId=%s)", name, xmlResourceIdentifier.getPublicId(), xmlResourceIdentifier.getBaseSystemId(), systemId));
85

86
            // resolve the entity via an org.xmlresolver.Resolver
87
            final InputSource src = resolver.resolveEntity(name, xmlResourceIdentifier.getPublicId(), xmlResourceIdentifier.getBaseSystemId(), systemId);
1✔
88
            if (src == null) {
1✔
89
                return null;
1✔
90
            }
91

92
            return new SAXInputSource(src);
1✔
93

94
        } catch (final SAXException e) {
×
95
            throw new XNIException(e);
×
96
        }
97
    }
98

99
    /**
100
     * Wraps the {@code resolver} in a XercesXMLResolverAdapter
101
     * and then sets it as the property {@code http://apache.org/xml/properties/internal/entity-resolver}
102
     * on the {@code xmlReader}.
103
     *
104
     * @param xmlReader the Xerces XML Reader
105
     * @param resolver the resolver, or null to unset the property
106
     *
107
     * @throws SAXNotSupportedException if the property is not supported by the XMLReader
108
     * @throws SAXNotRecognizedException if the property is not recognised by the XMLReader
109
     */
110
    public static void setXmlReaderEntityResolver(final XMLReader xmlReader, @Nullable final Resolver resolver) throws SAXNotSupportedException, SAXNotRecognizedException {
111
        final XMLEntityResolver xmlEntityResolver = resolver != null ? new XercesXmlResolverAdapter(resolver) : null;
1✔
112
        setXmlReaderEntityResolver(xmlReader, xmlEntityResolver);
1✔
113
    }
1✔
114

115
    /**
116
     * Sets the {@code xmlEntityResolver} as the property {@code http://apache.org/xml/properties/internal/entity-resolver}
117
     * on the {@code xmlReader}.
118
     *
119
     * @param xmlReader the Xerces XML Reader
120
     * @param xmlEntityResolver the resolver, or null to unset the resolver
121
     *
122
     * @throws SAXNotSupportedException if the property is not supported by the XMLReader
123
     * @throws SAXNotRecognizedException if the property is not recognised by the XMLReader
124
     */
125
    public static void setXmlReaderEntityResolver(final XMLReader xmlReader, @Nullable final XMLEntityResolver xmlEntityResolver) throws SAXNotSupportedException, SAXNotRecognizedException {
126
        xmlReader.setProperty(XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_ENTITYRESOLVER, xmlEntityResolver);
1✔
127
    }
1✔
128

129
    /**
130
     * Get the underlying resolver.
131
     *
132
     * @return the underlying resolver.
133
     */
134
    public Resolver getResolver() {
135
        return resolver;
1✔
136
    }
137
}
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