• 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

8.33
/extensions/expath/src/main/java/org/expath/tools/model/exist/EXistSequence.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.expath.tools.model.exist;
25

26
import java.io.IOException;
27
import java.io.OutputStream;
28
import java.io.OutputStreamWriter;
29
import java.io.Writer;
30
import java.nio.charset.StandardCharsets;
31
import java.util.Properties;
32
import javax.xml.XMLConstants;
33
import javax.xml.namespace.QName;
34
import javax.xml.transform.OutputKeys;
35

36
import org.apache.commons.io.output.CloseShieldOutputStream;
37
import org.exist.storage.serializers.Serializer;
38
import org.exist.util.serializer.XQuerySerializer;
39
import org.exist.xquery.XPathException;
40
import org.exist.xquery.XQueryContext;
41
import org.exist.xquery.value.Item;
42
import org.exist.xquery.value.SequenceIterator;
43
import org.expath.tools.ToolsException;
44
import org.expath.tools.model.Sequence;
45
import org.expath.tools.serial.SerialParameters;
46
import org.xml.sax.SAXException;
47

48
/**
49
 * @author <a href="mailto:adam@existsolutions.com">Adam Retter</a>
50
 */
51
public class EXistSequence implements Sequence {
52
 
53
    private final org.exist.xquery.value.Sequence sequence;
54
    private SequenceIterator sequenceIterator = SequenceIterator.EMPTY_ITERATOR;
1✔
55
    private final XQueryContext context;
56
    
57
    public EXistSequence(final org.exist.xquery.value.Sequence sequence, final XQueryContext context) throws XPathException {
1✔
58
        this.sequence = sequence;
1✔
59
        if(sequence != null) {
1!
60
            this.sequenceIterator = sequence.iterate();
1✔
61
        }
62
        this.context = context;
1✔
63
    }
1✔
64
    
65
    @Override
66
    public boolean isEmpty() throws ToolsException {
67
        return sequence.isEmpty();
×
68
    }
69

70
    @Override
71
    public Sequence next() throws ToolsException {
72
        try {
73
            final Item item = sequenceIterator.nextItem();
×
74
            final org.exist.xquery.value.Sequence singleton = (org.exist.xquery.value.Sequence) item;
×
75
            return new EXistSequence(singleton, context);
×
76
        } catch (final XPathException xpe) {
×
77
            throw new ToolsException(xpe.getMessage(), xpe);
×
78
        }
79
    }
80

81
    @Override
82
    public void serialize(final OutputStream out, final SerialParameters params) throws ToolsException {
83
        final Properties props = params == null ? null : makeOutputProperties(params);
×
84
        props.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
×
85

86
        final String encoding = props.getProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
×
87
        try(final Writer writer = new OutputStreamWriter(new CloseShieldOutputStream(out), encoding)) {
×
88
            final XQuerySerializer xqSerializer = new XQuerySerializer(context.getBroker(), props, writer);
×
89
            xqSerializer.serialize(sequence);
×
90
        } catch(final SAXException | IOException | XPathException e) {
×
91
            throw new ToolsException("A problem occurred while serializing the node set: " + e.getMessage(), e);
×
92
        }
×
93
    }
×
94

95
    /**
96
     * Borrowed from {@link org.expath.tools.saxon.model.SaxonSequence}
97
     */
98
    private Properties makeOutputProperties(final SerialParameters params) throws ToolsException
99
    {
100
        final Properties props = new Properties();
×
101

102
        setOutputKey(props, OutputKeys.METHOD,                 params.getMethod());
×
103
        setOutputKey(props, OutputKeys.MEDIA_TYPE,             params.getMediaType());
×
104
        setOutputKey(props, OutputKeys.ENCODING,               params.getEncoding());
×
105
        setOutputKey(props, OutputKeys.CDATA_SECTION_ELEMENTS, params.getCdataSectionElements());
×
106
        setOutputKey(props, OutputKeys.DOCTYPE_PUBLIC,         params.getDoctypePublic());
×
107
        setOutputKey(props, OutputKeys.DOCTYPE_SYSTEM,         params.getDoctypeSystem());
×
108
        setOutputKey(props, OutputKeys.INDENT,                 params.getIndent());
×
109
        setOutputKey(props, OutputKeys.OMIT_XML_DECLARATION,   params.getOmitXmlDeclaration());
×
110
        setOutputKey(props, OutputKeys.STANDALONE,             params.getStandalone());
×
111
        setOutputKey(props, OutputKeys.VERSION,                params.getVersion());
×
112

113
        return props;
×
114
    }
115

116
    private void setOutputKey(Properties props, String name, String value)
117
            throws ToolsException
118
    {
119
        if ( value != null ) {
×
120
            props.setProperty(name, value);
×
121
        }
122
    }
×
123

124
    private void setOutputKey(Properties props, String name, Boolean value)
125
            throws ToolsException
126
    {
127
        if ( value != null ) {
×
128
            props.setProperty(name, value ? "yes" : "no");
×
129
        }
130
    }
×
131

132
    private void setOutputKey(Properties props, String name, SerialParameters.Standalone value)
133
            throws ToolsException
134
    {
135
        if ( value != null ) {
×
136
            switch ( value ) {
×
137
                case YES:
138
                    props.setProperty(name, "yes");
×
139
                    break;
×
140
                case NO:
141
                    props.setProperty(name, "no");
×
142
                    break;
×
143
                case OMIT:
144
                    props.setProperty(name, "omit");
×
145
                    break;
×
146
                default:
147
                    throw new ToolsException("Invalid Standalone value: " + value);
×
148
            }
149
        }
150
    }
×
151

152
    private void setOutputKey(Properties props, String name, QName value)
153
            throws ToolsException
154
    {
155
        if ( value != null ) {
×
156
            if ( value.getNamespaceURI() != null && !value.getNamespaceURI().equals(XMLConstants.NULL_NS_URI) ) {
×
157
                throw new ToolsException(
×
158
                        "A QName with a non-null namespace not supported as a serialization param: {"
159
                                + value.getNamespaceURI() + "}" + value.getLocalPart());
×
160
            }
161
            props.setProperty(name, value.getLocalPart());
×
162
        }
163
    }
×
164

165
    private void setOutputKey(Properties props, String name, Iterable<QName> value)
166
            throws ToolsException
167
    {
168
        if ( value != null ) {
×
169
            StringBuilder buf = new StringBuilder();
×
170
            for ( QName qname : value ) {
×
171
                if ( qname.getNamespaceURI() != null ) {
×
172
                    throw new ToolsException(
×
173
                            "A QName with a non-null namespace not supported as a serialization param: {"
174
                                    + qname.getNamespaceURI() + "}" + qname.getLocalPart());
×
175
                }
176
                buf.append(qname.getLocalPart());
×
177
                buf.append(" ");
×
178
            }
×
179
            props.setProperty(name, buf.toString());
×
180
        }
181
    }
×
182
}
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