Package: StatementHolder
StatementHolder
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
StatementHolder(String) |
|
|
|
|
|
||||||||||||||||||||
analyzeStatement() |
|
|
|
|
|
||||||||||||||||||||
assembleStatement() |
|
|
|
|
|
||||||||||||||||||||
clearParameters() |
|
|
|
|
|
||||||||||||||||||||
ensureState() |
|
|
|
|
|
||||||||||||||||||||
getStatement() |
|
|
|
|
|
||||||||||||||||||||
isNotParametrized() |
|
|
|
|
|
||||||||||||||||||||
lambda$clearParameters$0(String, String) |
|
|
|
|
|
||||||||||||||||||||
setParameter(String, String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /**
2: * Copyright (C) 2022 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.ontodriver.util;
16:
17: import java.util.*;
18:
19: /**
20: * Used for parsing and managing SPARQL statements.
21: */
22: public class StatementHolder {
23:
24: private Map<String, String> paramValues;
25: private List<String> paramNames;
26: private List<String> statementParts;
27: private final String statement;
28:
29:
30: public StatementHolder(String statement) {
31: Objects.requireNonNull(statement);
32:
33: this.statement = statement.trim();
34: }
35:
36: public void analyzeStatement() {
37: this.paramNames = new ArrayList<>();
38: this.statementParts = new ArrayList<>();
39: // In single-quoted string
40: boolean inSQString = false;
41: // In double-quoted string
42: boolean inDQString = false;
43: boolean inParam = false;
44: int lastParamEndIndex = 0;
45: int paramStartIndex = 0;
46:• for (int i = 0; i < statement.length(); i++) {
47: final char c = statement.charAt(i);
48:• switch (c) {
49: case '\'':
50:• inSQString = !inSQString;
51: break;
52: case '"':
53:• inDQString = !inDQString;
54: break;
55: case '?':
56:• if (!inSQString && !inDQString) {
57: statementParts.add(statement.substring(lastParamEndIndex, i));
58: paramStartIndex = i + 1;
59: inParam = true;
60: }
61: break;
62: case '<':
63: case '>':
64: case ',':
65: case '\n':
66: case ')':
67: case ' ':
68: case '.':
69: case ';':
70: case '{':
71: case '}':
72: case '[':
73: case ']':
74:• if (inParam) {
75: lastParamEndIndex = i;
76: final String param = statement.substring(paramStartIndex, i);
77: paramNames.add(param);
78: inParam = false;
79: }
80: break;
81: default:
82: break;
83: }
84: }
85: statementParts.add(statement.substring(lastParamEndIndex));
86: this.paramValues = new HashMap<>(paramNames.size());
87:
88:• assert statementParts.size() == paramNames.size() + 1;
89: }
90:
91: public void setParameter(String parameterName, String value) {
92: ensureState();
93:• if (!paramNames.contains(parameterName)) {
94: throw new IllegalArgumentException("Unknown binding name " + parameterName);
95: }
96: paramValues.put(parameterName, value);
97: }
98:
99: private void ensureState() {
100:• if (paramNames == null) {
101: throw new IllegalStateException("Statement has to be analyzed before working with parameters.");
102: }
103: }
104:
105: public String assembleStatement() {
106:• if (isNotParametrized()) {
107: return statement;
108: }
109: final StringBuilder sb = new StringBuilder(statement.length());
110:• for (int i = 0; i < paramNames.size(); i++) {
111: sb.append(statementParts.get(i));
112: final String paramValue = paramValues.get(paramNames.get(i));
113:• if (paramValue == null) {
114: sb.append("?").append(paramNames.get(i));
115: } else {
116: sb.append(paramValue);
117: }
118: }
119: sb.append(statementParts.get(paramNames.size()));
120: return sb.toString();
121: }
122:
123: public String getStatement() {
124: return statement;
125: }
126:
127: private boolean isNotParametrized() {
128:• return paramNames == null || paramNames.isEmpty();
129: }
130:
131: public void clearParameters() {
132: ensureState();
133: paramValues.replaceAll((p, v) -> null);
134: }
135: }