• 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

78.82
/extensions/indexes/lucene/src/main/java/org/exist/indexing/lucene/AbstractFieldConfig.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.indexing.lucene;
50

51
import org.apache.logging.log4j.LogManager;
52
import org.apache.logging.log4j.Logger;
53
import org.apache.lucene.analysis.Analyzer;
54
import org.apache.lucene.document.Document;
55
import org.exist.collections.CollectionConfigurationManager;
56
import org.exist.dom.persistent.DocumentImpl;
57
import org.exist.dom.persistent.NodeProxy;
58
import org.exist.numbering.NodeId;
59
import org.exist.security.PermissionDeniedException;
60
import org.exist.storage.DBBroker;
61
import org.exist.xmldb.XmldbURI;
62
import org.exist.xquery.CompiledXQuery;
63
import org.exist.xquery.XPathException;
64
import org.exist.xquery.XQuery;
65
import org.exist.xquery.XQueryContext;
66
import org.exist.xquery.value.Sequence;
67
import org.w3c.dom.Element;
68

69
import javax.annotation.Nullable;
70
import java.net.URI;
71
import java.net.URISyntaxException;
72
import java.util.Map;
73
import java.util.Optional;
74

75
/**
76
 * Abstract configuration corresponding to either a field or facet element nested inside
77
 * a index definition 'text' element. Adds the possibility to create index content based
78
 * on an arbitrary XQuery expression.
79
 *
80
 * @author Wolfgang Meier
81
 */
82
public abstract class AbstractFieldConfig {
83

84
    public final static String XPATH_ATTR = "expression";
85

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

88
    protected Optional<String> expression = Optional.empty();
1✔
89
    protected boolean isValid = true;
1✔
90
    private CompiledXQuery compiled = null;
1✔
91

92
    public AbstractFieldConfig(LuceneConfig config, Element configElement, Map<String, String> namespaces) {
1✔
93
        final String xpath = configElement.getAttribute(XPATH_ATTR);
1✔
94
        if (!xpath.isEmpty()) {
1✔
95

96
            final StringBuilder sb = new StringBuilder();
1✔
97
            namespaces.forEach((prefix, uri) -> {
1✔
98
                if (!"xml".equals(prefix)) {
1✔
99
                    sb.append("declare namespace ").append(prefix);
1✔
100
                    sb.append("=\"").append(uri).append("\";\n");
1✔
101
                }
102
            });
1✔
103
            config.getImports().ifPresent(moduleImports -> moduleImports.forEach((moduleImport -> {
1✔
104
                sb.append("import module namespace ");
1✔
105
                sb.append(moduleImport.prefix);
1✔
106
                sb.append("=\"");
1✔
107
                sb.append(moduleImport.uri);
1✔
108
                sb.append("\" at \"");
1✔
109
                sb.append(resolveURI(configElement.getBaseURI(), moduleImport.at));
1✔
110
                sb.append("\";\n");
1✔
111
            })));
1✔
112
            sb.append(xpath);
1✔
113

114
            this.expression = Optional.of(sb.toString());
1✔
115
        }
116
    }
1✔
117

118
    @Nullable
119
    public Analyzer getAnalyzer() {
120
        return null;
×
121
    }
122

123
    protected abstract void processResult(Sequence result, Document luceneDoc) throws XPathException;
124

125
    protected abstract void processText(CharSequence text, Document luceneDoc);
126

127
    protected abstract void build(DBBroker broker, DocumentImpl document, NodeId nodeId, Document luceneDoc, CharSequence text);
128

129
    protected void doBuild(DBBroker broker, DocumentImpl document, NodeId nodeId, Document luceneDoc, CharSequence text)
130
            throws PermissionDeniedException, XPathException {
131
        if (!expression.isPresent()) {
1✔
132
            processText(text, luceneDoc);
1✔
133
            return;
1✔
134
        }
135

136
        compile(broker);
1✔
137

138
        if (!isValid) {
1!
139
            return;
×
140
        }
141

142
        final XQuery xquery = broker.getBrokerPool().getXQueryService();
1✔
143
        final NodeProxy currentNode = new NodeProxy(null, document, nodeId);
1✔
144
        try {
145
            Sequence result = xquery.execute(broker, compiled, currentNode);
1✔
146

147
            if (!result.isEmpty()) {
1✔
148
                processResult(result, luceneDoc);
1✔
149
            }
150
        } catch (PermissionDeniedException | XPathException e) {
×
151
            isValid = false;
×
152
            throw e;
×
153
        } finally {
154
            compiled.reset();
1✔
155
            compiled.getContext().reset();
1✔
156
        }
157
    }
1✔
158

159
    private void compile(DBBroker broker) {
160
        if (compiled == null && isValid) {
1!
161
            expression.ifPresent((code) -> compiled = compile(broker, code));
1✔
162
        }
163
    }
1✔
164

165
    protected CompiledXQuery compile(DBBroker broker, String code) {
166
        final XQuery xquery = broker.getBrokerPool().getXQueryService();
1✔
167
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
1✔
168
        try {
169
            return xquery.compile(context, code);
1✔
170
        } catch (XPathException | PermissionDeniedException e) {
×
171
            LOG.error("Failed to compile expression: {}: {}", code, e.getMessage(), e);
×
172
            isValid = false;
×
173
            return null;
×
174
        }
175
    }
176

177
    private String resolveURI(String baseURI, String location) {
178
        try {
179
            final URI uri = new URI(location);
1✔
180
            if (!uri.isAbsolute() && baseURI != null && baseURI.startsWith(CollectionConfigurationManager.CONFIG_COLLECTION)) {
1!
181
                String base = baseURI.substring(CollectionConfigurationManager.CONFIG_COLLECTION.length());
1✔
182
                final int lastSlash = base.lastIndexOf('/');
1✔
183
                if (lastSlash > -1) {
1!
184
                    base = base.substring(0, lastSlash);
1✔
185
                }
186
                return XmldbURI.EMBEDDED_SERVER_URI_PREFIX + base + '/' + location;
1✔
187
            }
188
        } catch (URISyntaxException e) {
×
189
            // ignore and return location
190
        }
×
191
        return location;
×
192
    }
193
}
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

© 2026 Coveralls, Inc