Skip to contentMethod: getSparqlFunction(String)
1: /**
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.soql;
16:
17: import cz.cvut.kbss.jopa.exception.QueryParserException;
18:
19: import java.util.HashMap;
20: import java.util.Locale;
21: import java.util.Map;
22:
23: /**
24: * Translates SOQL functions to SPARQL functions.
25: */
26: class SoqlFunctionTranslator {
27:
28: private static final Map<String, String> FUNCTION_MAP = initFunctions();
29:
30: private static Map<String, String> initFunctions() {
31: final Map<String, String> map = new HashMap<>();
32: map.put(SoqlConstants.Functions.UPPER, "UCASE");
33: map.put(SoqlConstants.Functions.LOWER, "LCASE");
34: map.put(SoqlConstants.Functions.LENGTH, "STRLEN");
35: map.put(SoqlConstants.Functions.ABS, "ABS");
36: map.put(SoqlConstants.Functions.CEIL, "CEIL");
37: map.put(SoqlConstants.Functions.FLOOR, "FLOOR");
38: return map;
39: }
40:
41: private SoqlFunctionTranslator() {
42: throw new AssertionError();
43: }
44:
45: /**
46: * Gets a SPARQL function equivalent to the specified SOQL function.
47: *
48: * @param soqlFunction SOQL function name
49: * @return Matching SPARQL function name
50: * @throws QueryParserException If the specified function has no SPARQL equivalent here
51: */
52: static String getSparqlFunction(String soqlFunction) {
53: final String fName = soqlFunction.toUpperCase(Locale.ROOT);
54:• if (!FUNCTION_MAP.containsKey(fName)) {
55: throw new QueryParserException("Unsupported SOQL function '" + soqlFunction + "'.");
56: }
57: return FUNCTION_MAP.get(fName);
58: }
59: }