• 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

39.82
/exist-core/src/main/java/org/exist/util/ParametersExtractor.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.util;
50

51
import java.util.ArrayList;
52
import java.util.HashMap;
53
import java.util.List;
54
import java.util.Map;
55
import java.util.Properties;
56
import org.w3c.dom.Element;
57
import org.w3c.dom.NamedNodeMap;
58
import org.w3c.dom.Node;
59
import org.w3c.dom.NodeList;
60

61
/**
62
 * Utility class for extracting parameters from 
63
 * DOM representation into a Map.
64
 *
65
 * @author <a href="mailto:adam@exist-db.org">Adam Retter</a>
66
 */
67
public class ParametersExtractor {
×
68

69
    public final static String PARAMETERS_ELEMENT_NAME = "parameters";
70
    public final static String PARAMETER_ELEMENT_NAME = "parameter";
71
    private final static String PARAMETER_NAME_ATTRIBUTE = "name";
72
    private final static String PARAMETER_VALUE_ATTRIBUTE = "value";
73

74
    /**
75
     * Extract the parameters.
76
     *
77
     * @param parameters A "parameters" element, which may contain "parameter" child elements
78
     *
79
     * @return the parameters
80
     */
81
    public static Map<String, List<? extends Object>> extract(final Element parameters) {
82

83
        final Map<String, List<? extends Object>> result;
84

85
        if(parameters == null || !parameters.getLocalName().equals(PARAMETERS_ELEMENT_NAME)) {
×
86
            result = new HashMap<>(0);
×
87
        } else {
×
88

89
            final String namespace = parameters.getNamespaceURI();
×
90
            final NodeList nlParameter = parameters.getElementsByTagNameNS(namespace, PARAMETER_ELEMENT_NAME);
×
91

92
            result = extract(nlParameter);
×
93
        }
94

95
        return result;
×
96
    }
97

98
    /**
99
     * Extract the parameters.
100
     *
101
     * @param nlParameter A NodeList of "parameter" elements
102
     *
103
     * @return the parameters
104
     */
105
    public static Map<String, List<? extends Object>> extract(final NodeList nlParameter) {
106

107
        final Map<String, List<? extends Object>> result;
108

109
        if(nlParameter == null || nlParameter.getLength() == 0) {
1!
110
            result = new HashMap<>(0);
1✔
111
        } else {
1✔
112
            result = extractParameters(nlParameter);
1✔
113
        }
114

115

116
        return result;
1✔
117
    }
118

119
    private static Map<String, List<? extends Object>> extractParameters(final NodeList nlParameter) {
120

121
        final Map<String, List<?>> parameters = new HashMap<>(nlParameter.getLength());
1✔
122

123
        for (int i = 0 ; i < nlParameter.getLength();  i++) {
1✔
124
            final Element param = (Element)nlParameter.item(i);
1✔
125
            //TODO : rely on schema-driven validation -pb
126
            final String name = param.getAttribute(PARAMETER_NAME_ATTRIBUTE);
1✔
127
            /*if(name == null) {
128
                throwOrLog("Expected attribute '" + PARAMETER_NAME_ATTRIBUTE + "' for element '" + PARAMETER_ELEMENT_NAME + "' in trigger's configuration.", testOnly);
129
            }*/
130

131
            List values = parameters.get(name);
1✔
132

133
            final String value = param.getAttribute(PARAMETER_VALUE_ATTRIBUTE);
1✔
134
            if (!value.isEmpty()) {
1!
135
                if(values == null) {
1✔
136
                    values = new ArrayList<String>();
1✔
137
                }
138
                values.add(value);
1✔
139
            } else {
1✔
140
                //are there child nodes?
141
                if(param.getChildNodes().getLength() > 0) {
×
142

143
                    if(values == null) {
×
144
                        values = new ArrayList<Map<String, List>>();
×
145
                    }
146

147
                    values.add(getParameterChildParameters(param));
×
148
                }
149
            }
150

151
            parameters.put(name, values);
1✔
152
        }
153

154
        return parameters;
1✔
155
    }
156

157
    private static Map<String, List> getParameterChildParameters(final Element parameter) {
158

159
        final Map<String, List> results = new HashMap<>();
×
160

161
        final NodeList childParameters = parameter.getChildNodes();
×
162
        for(int i = 0; i < childParameters.getLength(); i++) {
×
163
            final Node nChildParameter = childParameters.item(i);
×
164
            if(nChildParameter instanceof Element childParameter) {
×
165
                final String name = childParameter.getLocalName();
×
166

167
                if(childParameter.getAttributes().getLength() > 0){
×
168
                    List<Properties> childParameterProperties = (List<Properties>)results.get(name);
×
169
                    if(childParameterProperties == null) {
×
170
                        childParameterProperties = new ArrayList<>();
×
171
                    }
172

173
                    final NamedNodeMap attrs = childParameter.getAttributes();
×
174
                    final Properties props = new Properties();
×
175
                    for(int a = 0; a < attrs.getLength(); a++) {
×
176
                        final Node attr = attrs.item(a);
×
177
                        props.put(attr.getLocalName(), attr.getNodeValue());
×
178
                    }
179
                    childParameterProperties.add(props);
×
180

181
                    results.put(name, childParameterProperties);
×
182
                } else {
×
183
                    List<String> strings = (List<String>)results.get(name);
×
184
                    if(strings == null) {
×
185
                        strings = new ArrayList<>();
×
186
                    }
187
                    strings.add(childParameter.getTextContent());
×
188
                    results.put(name, strings);
×
189
                }
190
            }
191
        }
192

193
        return results;
×
194
    }
195

196
    /**
197
     * Parses a structure like:
198
     * <pre>
199
     * {@code
200
     *  <parameters>
201
     *    <param name="a" value="1"/><param name="b" value="2"/>
202
     *  </parameters>
203
     * }
204
     * </pre>
205
     * into a set of Properties.
206
     *
207
     * @param nParameters
208
     *            The parameters Node
209
     * @return a set of name value properties for representing the XML
210
     *         parameters
211
     */
212
    public static Properties parseParameters(final Node nParameters){
213
        return parseProperties(nParameters, "param");
×
214
    }
215

216
    /**
217
     * Parses a structure like:
218
     * <pre>
219
     * {@code
220
     *   <properties>
221
     *     <property name="a" value="1"/>
222
     *     <property name="b" value="2"/>
223
     *   </properties>
224
     * }
225
     * </pre>
226
     * into a set of Properties
227
     *
228
     * @param nProperties
229
     *            The properties Node
230
     * @return a set of name value properties for representing the XML
231
     *         properties
232
     */
233
    public static Properties parseProperties(final Node nProperties) {
234
        return parseProperties(nProperties, "property");
1✔
235
    }
236

237
    /**
238
     * Parses a structure like:
239
     * <pre>
240
     * {@code
241
     *   <features>
242
     *     <feature name="a" value="1"/>
243
     *     <feature name="b" value="2"/>
244
     *   </features>
245
     * }
246
     * </pre>
247
     * into a set of Properties
248
     *
249
     * @param nFeatures
250
     *            The features Node
251
     * @return a set of name value properties for representing the XML
252
     *         features
253
     */
254
    public static Properties parseFeatures(final Node nFeatures) {
255
        return parseProperties(nFeatures, "feature");
1✔
256
    }
257

258
    /**
259
     * Parses a structure like:
260
     * <pre>
261
     * {@code
262
     *   <properties>
263
     *     <property name="a" value="1"/>
264
     *     <property name="b" value="2"/>
265
     *   </properties>
266
     * }
267
     * </pre>
268
     * into a set of Properties
269
     *
270
     * @param container
271
     *            The container of the properties
272
     * @param elementName
273
     *            The name of the property element
274
     * @return a set of name value properties for representing the XML
275
     *         properties
276
     */
277
    private static Properties parseProperties(final Node container, final String elementName) throws IllegalArgumentException {
278
        final Properties properties = new Properties();
1✔
279

280
        if(container != null && container.getNodeType() == Node.ELEMENT_NODE) {
1!
281
            final NodeList params = ((Element) container).getElementsByTagName(elementName);
1✔
282
            for(int i = 0; i < params.getLength(); i++) {
1✔
283
                final Element param = ((Element) params.item(i));
1✔
284

285
                final String name = param.getAttribute("name");
1✔
286
                final String value = param.getAttribute("value");
1✔
287

288
                if ((!name.isEmpty()) && (!value.isEmpty())) {
1!
289
                    properties.setProperty(name, value);
1✔
290
                } else {
1✔
291
                    if (name.isEmpty()) {
×
292
                        throw new IllegalArgumentException("'name' attribute missing for " + elementName);
×
293
                    } else {
294
                        throw new IllegalArgumentException("'value' attribute missing for " + elementName);
×
295
                    }
296
                }
297
            }
298
        }
299

300
        return properties;
1✔
301
    }
302
}
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