-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPARQLBuilder.java
More file actions
390 lines (320 loc) · 12.7 KB
/
SPARQLBuilder.java
File metadata and controls
390 lines (320 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package rs.ftn.pma.tourismobile.util;
import java.util.ArrayList;
import java.util.List;
/**
* Utility class for building SPARQL queries.
* Created by Daniel Kupčo on 07.06.2016.
*/
public class SPARQLBuilder {
private SPARQLSelectPart query;
public SPARQLSelectPart startQuery() {
query = new SPARQLSelectPart();
return query;
}
public String getQuery() {
return query.build();
}
public String prettify() {
StringBuilder prettified = new StringBuilder(getQuery());
// keywords
List<String> keywords = new ArrayList<>();
keywords.add("SELECT");
keywords.add("FROM");
keywords.add("WHERE");
keywords.add("FILTER");
keywords.add("GROUP BY");
keywords.add("ORDER BY");
keywords.add("LIMIT");
keywords.add("OFFSET");
for(String keyword : keywords) {
breakLineOnKeyword(prettified, keyword);
}
// separators and indentation
String indentation = "";
int firstOpenCurly = prettified.indexOf("{");
// must be updated because query length is getting bigger
int lastClosedCurly = prettified.lastIndexOf("}") + 1;
boolean afterTriplet = false;
for (int i = firstOpenCurly; i < lastClosedCurly; i++) {
char c = prettified.charAt(i);
if(c == '{') {
prettified.replace(i - 1, i, "\n" + indentation); // insert new line and indentation
lastClosedCurly += indentation.length() + 1; // update last closed curly bracket index
i += indentation.length(); // skip indentation, new line is already skipped with i++ in the loop
indentation += " "; // increase indentation
if(prettified.indexOf("{ ?", i) == i) { // if there is triplet break it to the next line
prettified.replace(i + 1, i + 2, "\n" + indentation);
lastClosedCurly += indentation.length() + 1;
i += indentation.length();
}
}
else if(c == '}') {
if(indentation.length() > 1) {
indentation = indentation.substring(0, indentation.length() - 2); // first decrease the indentation
}
prettified.replace(i - 1, i, "\n" + indentation); // same process as above...
lastClosedCurly += indentation.length() + 1;
i += indentation.length();
if(prettified.indexOf("} ?", i) == i) {
prettified.replace(i + 1, i + 2, "\n" + indentation);
lastClosedCurly += indentation.length() + 1;
i += indentation.length();
}
}
// for every keyword in the process (while we have valid indentation in each moment)
for(String keyword : keywords) {
int index = prettified.indexOf(keyword, i); // get the keyword index form current position
if(i == index) { // if we are at the keyword
prettified.insert(i, indentation); // indent it
lastClosedCurly += indentation.length();
i += indentation.length() + keyword.length(); // skip the indentation and the keyword
}
}
// separators
if (c == ';') {
if (!afterTriplet) { // if after triplet indent a bit more
afterTriplet = true;
indentation += " ";
}
prettified.replace(i + 1, i + 2, "\n" + indentation);
lastClosedCurly += indentation.length() + 1;
}
else if (c == '.') {
if (afterTriplet) { // if another condition and we are in triplet decrease indentation
afterTriplet = false;
indentation = indentation.substring(0, indentation.length() - 2);
}
prettified.replace(i + 1, i + 2, "\n" + indentation);
lastClosedCurly += indentation.length() + 1;
}
}
// in the end clear empty lines
clearEmptyLines(prettified);
return prettified.toString();
}
private void breakLineOnKeyword(StringBuilder stringBuilder, String keyword) {
int index = stringBuilder.indexOf(keyword);
while(index > -1) {
stringBuilder.insert(index, "\n");
index = stringBuilder.indexOf(keyword, index + 2);
}
}
private void clearEmptyLines(StringBuilder stringBuilder) {
int firstNewLine = stringBuilder.indexOf("\n");
int secondNewLine = stringBuilder.indexOf("\n", firstNewLine + 1);
while(secondNewLine > -1 && secondNewLine < stringBuilder.length()) {
// if there is empty line remove it
// first new line index automatically takes the value of the second
if(stringBuilder.substring(firstNewLine, secondNewLine).trim().isEmpty()) {
stringBuilder.replace(firstNewLine, secondNewLine, "");
}
// otherwise go to the next new line character
else{
firstNewLine = secondNewLine;
}
secondNewLine = stringBuilder.indexOf("\n", firstNewLine + 1);
}
}
public static abstract class SPARQLPart {
protected SPARQLPart creator;
protected StringBuilder queryPart;
public SPARQLPart(SPARQLPart creator) {
this.creator = creator;
queryPart = new StringBuilder();
}
public SPARQLPart(SPARQLPart creator, String initialString) {
this.creator = creator;
queryPart = new StringBuilder(initialString);
}
public char lastChar(StringBuilder stringBuilder) {
int i = 1;
int length = stringBuilder.length();
char last = ' ';
while(last == ' ') {
last = stringBuilder.charAt(length - i);
i++;
}
return last;
}
public SPARQLPart appendString(String string) {
queryPart.append(string);
return this;
}
public SPARQLPart appendStringLine(String string) {
queryPart.append("\n").append(string);
return this;
}
public SPARQLPart endPart() {
creator.appendString(queryPart.toString());
return creator;
}
}
public static class SPARQLSelectPart extends SPARQLPart {
public SPARQLSelectPart() {
super(null);
}
public SPARQLSelectPart(SPARQLPart creator) {
super(creator);
}
public SPARQLSelectPart(SPARQLPart creator, String initialString) {
super(creator, initialString);
}
public SPARQLWherePart endSubquery() {
queryPart.append(" }");
return (SPARQLWherePart) super.endPart();
}
public SPARQLSelectPart select() {
queryPart.append("SELECT");
return this;
}
public SPARQLSelectPart selectDistinct() {
queryPart.append("SELECT DISTINCT");
return this;
}
public SPARQLSelectPart variables(String... variables) {
if(variables.length == 0) {
queryPart.append(" *");
}
else {
for(String variable : variables) {
queryPart.append(String.format(" ?%s", variable));
}
}
return this;
}
public SPARQLSelectPart var(String name) {
queryPart.append(String.format(" (?%s) ", name));
return this;
}
public SPARQLSelectPart varAs(String name, String as) {
queryPart.append(String.format(" (?%s as ?%s) ", name, as));
return this;
}
public SPARQLSelectPart aggregateVarAs(String aggregateFunction, String name, String as) {
queryPart.append(String.format(" (%s(?%s) as ?%s) ", aggregateFunction, name, as));
return this;
}
public SPARQLSelectPart from(String url) {
queryPart.append(String.format(" FROM <%s>", url));
return this;
}
public SPARQLWherePart startWhere() {
SPARQLWherePart wherePart = new SPARQLWherePart(this);
return wherePart;
}
public SPARQLSelectPart orderBy(String... variables) {
if(variables.length > 0) {
queryPart.append(" ORDER BY");
for(String variable : variables) {
queryPart.append(String.format(" ?%s", variable));
}
}
return this;
}
public SPARQLSelectPart groupBy(String... variables) {
if(variables.length > 0) {
queryPart.append(" GROUP BY");
for(String variable : variables) {
queryPart.append(String.format(" ?%s", variable));
}
}
return this;
}
public SPARQLSelectPart limit(int number) {
queryPart.append(String.format(" LIMIT %d", number));
return this;
}
public SPARQLSelectPart offset(int number) {
queryPart.append(String.format(" OFFSET %d", number));
return this;
}
public String build() {
return queryPart.toString();
}
}
public static class SPARQLWherePart extends SPARQLPart {
public SPARQLWherePart(SPARQLSelectPart creator) {
super(creator, " WHERE {");
}
public SPARQLSelectPart endWhere() {
queryPart.append(" . }");
return (SPARQLSelectPart) super.endPart();
}
public SPARQLSelectPart startSubquery() {
return new SPARQLSelectPart(this, " { ");
}
public SPARQLWherePart triplet(String subject, String predicate, String object) {
return triplet(subject, predicate, object, false);
}
public SPARQLWherePart triplet(String subject, String predicate, String object, boolean isObjectVariable) {
char lastChar = lastChar(queryPart);
if(!(lastChar == '{' || lastChar == '.' || lastChar == '}')) {
queryPart.append(" .");
}
final String format = isObjectVariable ? " ?%s %s ?%s" : " ?%s %s %s";
queryPart.append(String.format(format, subject, predicate, object));
return this;
}
public SPARQLWherePart property(String property) {
queryPart.append(String.format(" ; %s", property));
return this;
}
public SPARQLWherePart propertyChoice(String... properties) {
queryPart.append(" ; ");
for(String property : properties) {
queryPart.append(String.format("%s|", property));
}
queryPart.deleteCharAt(queryPart.length() - 1);
return this;
}
public SPARQLWherePart as(String variable) {
queryPart.append(String.format(" ?%s", variable));
return this;
}
public SPARQLWherePart and() {
queryPart.append(" .");
return this;
}
public SPARQLFilterPart startFilter() {
SPARQLFilterPart filterPart = new SPARQLFilterPart(this);
return filterPart;
}
}
public static class SPARQLFilterPart extends SPARQLPart {
public SPARQLFilterPart(SPARQLWherePart creator) {
super(creator, " . FILTER(");
}
public SPARQLFilterPart var(String name) {
queryPart.append(String.format("?%s", name));
return this;
}
public SPARQLFilterPart varFunction(String function, String name) {
queryPart.append(String.format("%s(?%s)", function, name));
return this;
}
public SPARQLFilterPart eq(String value) {
queryPart.append(String.format("=%s", value));
return this;
}
public SPARQLFilterPart eqAsString(String value) {
queryPart.append(String.format("=\"%s\"", value));
return this;
}
public SPARQLFilterPart eqAsType(String value, String type) {
queryPart.append(String.format("=%s^^%s", value, type));
return this;
}
public SPARQLFilterPart and() {
queryPart.append(" && ");
return this;
}
public SPARQLFilterPart or() {
queryPart.append(" || ");
return this;
}
public SPARQLWherePart endFilter() {
queryPart.append(")");
return (SPARQLWherePart) super.endPart();
}
}
}