• 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

79.79
/exist-core/src/main/java/org/exist/xquery/functions/fn/ParsingFunctions.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
 * NOTE: Parts of this file contain code from 'The eXist-db Authors'.
25
 *       The original license header is included below.
26
 *
27
 * =====================================================================
28
 *
29
 * eXist-db Open Source Native XML Database
30
 * Copyright (C) 2001 The eXist-db Authors
31
 *
32
 * info@exist-db.org
33
 * http://www.exist-db.org
34
 *
35
 * This library is free software; you can redistribute it and/or
36
 * modify it under the terms of the GNU Lesser General Public
37
 * License as published by the Free Software Foundation; either
38
 * version 2.1 of the License, or (at your option) any later version.
39
 *
40
 * This library is distributed in the hope that it will be useful,
41
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43
 * Lesser General Public License for more details.
44
 *
45
 * You should have received a copy of the GNU Lesser General Public
46
 * License along with this library; if not, write to the Free Software
47
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
48
 */
49
package org.exist.xquery.functions.fn;
50

51
import org.apache.logging.log4j.LogManager;
52
import org.apache.logging.log4j.Logger;
53
import org.exist.Namespaces;
54
import org.exist.dom.QName;
55
import org.exist.dom.memtree.MemTreeBuilder;
56
import org.exist.dom.memtree.NodeImpl;
57
import org.exist.dom.memtree.SAXAdapter;
58
import org.exist.util.XMLReaderPool;
59
import org.exist.validation.ValidationReport;
60
import org.exist.xquery.*;
61
import org.exist.xquery.functions.validation.Shared;
62
import org.exist.xquery.value.*;
63
import org.xml.sax.Attributes;
64
import org.xml.sax.InputSource;
65
import org.xml.sax.SAXException;
66
import org.xml.sax.XMLReader;
67

68
import java.io.IOException;
69
import java.io.StringReader;
70
import java.nio.charset.StandardCharsets;
71

72
import static org.exist.util.ByteOrderMark.stripXmlBom;
73

74
public class ParsingFunctions extends BasicFunction {
75

76
    private static final String FRAGMENT_WRAPPER_NAME = "__parse-xml-fragment__";
77

78
        protected static final FunctionReturnSequenceType RESULT_TYPE_FOR_PARSE_XML = new FunctionReturnSequenceType(Type.DOCUMENT,
1✔
79
                        Cardinality.ZERO_OR_ONE, "the parsed document");
1✔
80
        protected static final FunctionReturnSequenceType RESULT_TYPE_FOR_PARSE_XML_FRAGMENT = new FunctionReturnSequenceType(Type.DOCUMENT,
1✔
81
                        Cardinality.ZERO_OR_ONE, "the parsed document fragment");
1✔
82

83
        protected static final FunctionParameterSequenceType TO_BE_PARSED_PARAMETER = new FunctionParameterSequenceType(
1✔
84
                        "arg", Type.STRING, Cardinality.ZERO_OR_ONE, "The string to be parsed");
1✔
85

86
        protected static final Logger logger = LogManager.getLogger(ParsingFunctions.class);
1✔
87

88
        public final static FunctionSignature[] signatures = {
1✔
89
                        new FunctionSignature(
1✔
90
                                        new QName("parse-xml", Function.BUILTIN_FUNCTION_NS),
1✔
91
                                        "This function takes as input an XML document represented as a string,"
1✔
92
                                                        + " and returns the document node at the root of an XDM tree representing the parsed document.",
93
                                        new SequenceType[] { TO_BE_PARSED_PARAMETER }, RESULT_TYPE_FOR_PARSE_XML),
1✔
94
                        new FunctionSignature(
1✔
95
                                        new QName("parse-xml-fragment", Function.BUILTIN_FUNCTION_NS),
1✔
96
                                        "This function takes as input an XML external entity represented as a string," +
1✔
97
                                        "and returns the document node at the root of an XDM tree representing the parsed document fragment.",
98
                                        new SequenceType[] { TO_BE_PARSED_PARAMETER }, RESULT_TYPE_FOR_PARSE_XML_FRAGMENT) };
1✔
99

100
        public ParsingFunctions(final XQueryContext context, final FunctionSignature signature) {
101
                super(context, signature);
1✔
102
        }
1✔
103

104
        @Override
105
        public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
106
                if (args[0].getItemCount() == 0) {
1!
107
                        return Sequence.EMPTY_SEQUENCE;
×
108
                }
109
                final String xmlContent = args[0].itemAt(0).getStringValue();
1✔
110
                if (xmlContent.isEmpty()) {
1✔
111
                        return Sequence.EMPTY_SEQUENCE;
1✔
112
                }
113
        
114
        return parse(xmlContent, args);
1✔
115
        }
116

117

118
    private Sequence parse(final String xmlContent, final Sequence[] args) throws XPathException {
119
        final SAXAdapter adapter = new FragmentSAXAdapter(this, context, isCalledAs("parse-xml-fragment"));
1✔
120
            final ValidationReport report = validate(xmlContent, adapter);
1✔
121

122
        if (report.isValid()) {
1!
123
            return adapter.getDocument();
1✔
124
        } else {
125
            try {
126
                context.pushDocumentContext();
×
127
                final MemTreeBuilder builder = context.getDocumentBuilder();
×
128
                final NodeImpl result = Shared.writeReport(report, builder);
×
129
                throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + report.toString(), result);
×
130
            } finally {
×
131
                context.popDocumentContext();
×
132
            }
×
133
        }
134
    }
135

136
    private ValidationReport validate(String xmlContent, final SAXAdapter saxAdapter) throws XPathException {
137
        xmlContent = stripXmlBom(xmlContent);
1✔
138
        final String xml;
139
            if (isCalledAs("parse-xml-fragment")) {
1✔
140
            xml = "<" + FRAGMENT_WRAPPER_NAME + ">" + xmlContent + "</" + FRAGMENT_WRAPPER_NAME + ">";
1✔
141
        } else {
1✔
142
                xml = xmlContent;
1✔
143
        }
144

145
        final ValidationReport report = new ValidationReport();
1✔
146

147
        try (final StringReader reader = new StringReader(xml)) {
1✔
148
            final InputSource src = new InputSource(reader);
1✔
149

150
            final XMLReaderPool parserPool = context.getBroker().getBrokerPool().getParserPool();
1✔
151
            XMLReader xr = null;
1✔
152
            try {
153
                xr = parserPool.borrowXMLReader();
1✔
154
                xr.setErrorHandler(report);
1✔
155
                xr.setContentHandler(saxAdapter);
1✔
156
                xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, saxAdapter);
1✔
157
                xr.parse(src);
1✔
158
            } catch (final SAXException e) {
1✔
159
                logger.debug("Error while parsing XML: {}", e.getMessage(), e);
×
160
            } catch (final IOException e) {
×
161
                throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + e.getMessage(),
×
162
                        new StringValue(this, xml), e);
×
163
            } finally {
164
                if (xr != null) {
1!
165
                    parserPool.returnXMLReader(xr);
1✔
166
                }
167
            }
168
        }
169

170
        return report;
1✔
171
    }
172

173
    private static class FragmentSAXAdapter extends SAXAdapter {
174
        private final boolean hasFragmentWrapper;
175
        private boolean strippedFramentWrapper = false;
1✔
176

177
        public FragmentSAXAdapter(final XQueryContext context, final boolean hasFragmentWrapper) {
178
            this(null, context, hasFragmentWrapper);
×
179
        }
×
180

181
        public FragmentSAXAdapter(final Expression expression, final XQueryContext context, final boolean hasFragmentWrapper) {
182
            super(expression, context);
1✔
183
            this.hasFragmentWrapper = hasFragmentWrapper;
1✔
184
        }
1✔
185

186
        @Override
187
        public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
188
            if (hasFragmentWrapper && !strippedFramentWrapper && localName.equals(FRAGMENT_WRAPPER_NAME)) {
1!
189
                // no-op
190
            } else {
191
                super.startElement(namespaceURI, localName, qName, atts);
1✔
192
            }
193
        }
1✔
194

195
        @Override
196
        public void endElement(final String namespaceURI, final String localName, final String qName) throws SAXException {
197
            if (hasFragmentWrapper && !strippedFramentWrapper && localName.equals(FRAGMENT_WRAPPER_NAME)) {
1!
198
                strippedFramentWrapper = true;
1✔
199
            } else {
1✔
200
                super.endElement(namespaceURI, localName, qName);
1✔
201
            }
202
        }
1✔
203
    }
204
}
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