-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathUtils.java
More file actions
572 lines (492 loc) · 20 KB
/
Utils.java
File metadata and controls
572 lines (492 loc) · 20 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package be.ugent.rml;
import be.ugent.rml.extractor.ConstantExtractor;
import be.ugent.rml.extractor.Extractor;
import be.ugent.rml.extractor.ReferenceExtractor;
import be.ugent.rml.store.Quad;
import be.ugent.rml.store.QuadStore;
import be.ugent.rml.store.RDF4JStore;
import be.ugent.rml.term.Literal;
import be.ugent.rml.term.NamedNode;
import be.ugent.rml.term.Term;
import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;
import org.apache.commons.io.IOUtils;
import org.apache.commons.validator.routines.UrlValidator;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.rio.ParserConfig;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFParseException;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.helpers.BasicParserSettings;
import org.rdfhdt.hdt.enums.RDFNotation;
import org.rdfhdt.hdt.hdt.HDT;
import org.rdfhdt.hdt.hdt.HDTManager;
import org.rdfhdt.hdt.options.HDTSpecification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
public class Utils {
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
// Without support for custom registered languages of length 5-8 of the IANA language-subtag-registry
private static final Pattern regexPatternLanguageTag = Pattern.compile("^((?:(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?:([A-Za-z]{2,3}(-(?:[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4})(-(?:[A-Za-z]{4}))?(-(?:[A-Za-z]{2}|[0-9]{3}))?(-(?:[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-(?:[0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(?:x(-[A-Za-z0-9]{1,8})+))?)|(?:x(-[A-Za-z0-9]{1,8})+))$");
public static Reader getReaderFromLocation(String location) throws IOException {
return getReaderFromLocation(location, null, "");
}
public static Reader getReaderFromLocation(String location, File basePath, String contentType) throws IOException {
if (isRemoteFile(location)) {
try {
return getReaderFromURL(new URL(location), contentType);
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else {
return getReaderFromFile(getFile(location, basePath));
}
}
public static InputStream getInputStreamFromLocation(String location) throws IOException {
return getInputStreamFromLocation(location, null, "");
}
public static InputStream getInputStreamFromLocation(String location, File basePath, String contentType) throws IOException {
if (isRemoteFile(location)) {
return getInputStreamFromURL(new URL(location), contentType);
} else {
return getInputStreamFromFile(getFile(location, basePath));
}
}
public static InputStream getInputStreamFromMOptionValue(String mOptionValue) {
InputStream out;
try {
out = getInputStreamFromLocation(mOptionValue, null, "application/rdf+xml");
} catch (IOException e) {
try {
// raw mapping input string
out = IOUtils.toInputStream(mOptionValue, "UTF-8");
} catch (IOException e2) {
logger.error("Cannot read mapping option {}", mOptionValue);
out = new ByteArrayInputStream(new byte[0]);
}
}
return out;
}
/**
* @param path
* @return
* @throws IOException
*/
public static File getFile(String path) throws IOException {
return Utils.getFile(path, null);
}
/**
* @param path
* @param basePath
* @return
* @throws IOException
*/
public static File getFile(String path, File basePath) throws IOException {
// Absolute path?
File f = new File(path);
if (f.isAbsolute()) {
if (f.exists()) {
return f;
} else {
throw new FileNotFoundException();
}
}
if (basePath == null) {
try {
basePath = new File(System.getProperty("user.dir"));
} catch (Exception e) {
throw new FileNotFoundException();
}
}
logger.debug("Looking for file " + path + " in basePath " + basePath);
// Relative from user dir?
f = new File(basePath, path);
if (f.exists()) {
return f;
}
// Relative from parent of user dir?
f = new File(basePath, "../" + path);
if (f.exists()) {
return f;
}
// Resource path?
try {
return MyFileUtils.getResourceAsFile(path);
} catch (IOException e) {
// Too bad
}
throw new FileNotFoundException(path);
}
public static Reader getReaderFromURL(URL url) throws IOException {
return new BufferedReader(new InputStreamReader(url.openStream()));
}
public static Reader getReaderFromURL(URL url, String contentType) throws IOException {
return new BufferedReader(new InputStreamReader(getInputStreamFromURL(url, contentType)));
}
public static Reader getReaderFromFile(File file) throws FileNotFoundException {
return new FileReader(file);
}
public static InputStream getInputStreamFromURL(URL url) throws IOException {
return url.openStream();
}
public static InputStream getInputStreamFromURL(URL url, String contentType) {
InputStream inputStream = null;
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", contentType);
connection.setRequestProperty("charset", "utf-8");
connection.connect();
inputStream = connection.getInputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
return inputStream;
}
public static InputStream getInputStreamFromFile(File file) throws FileNotFoundException {
return new FileInputStream(file);
}
public static boolean isRemoteFile(String location) {
return location.startsWith("https://") || location.startsWith("http://");
}
public static boolean isLabeledFile(String location) {
return location.startsWith("label://");
}
public static List<Term> getSubjectsFromQuads(List<Quad> quads) {
ArrayList<Term> subjects = new ArrayList<>();
for (Quad quad : quads) {
subjects.add(quad.getSubject());
}
return subjects;
}
public static List<Term> getObjectsFromQuads(List<Quad> quads) {
ArrayList<Term> objects = new ArrayList<>();
for (Quad quad : quads) {
objects.add(quad.getObject());
}
return objects;
}
public static List<String> getLiteralObjectsFromQuads(List<Quad> quads) {
ArrayList<String> objects = new ArrayList<>();
for (Quad quad : quads) {
objects.add(((Literal) quad.getObject()).getValue());
}
return objects;
}
public static List<Term> getList(QuadStore store, Term first) {
List<Term> list = new ArrayList<>();
return getList(store, first, list);
}
public static List<Term> getList(QuadStore store, Term first, List<Term> list) {
if (first.equals(new NamedNode(NAMESPACES.RDF + "nil"))) {
return list;
}
Term value = Utils.getObjectsFromQuads(store.getQuads(first, new NamedNode(NAMESPACES.RDF + "first"), null)).get(0);
Term next = Utils.getObjectsFromQuads(store.getQuads(first, new NamedNode(NAMESPACES.RDF + "rest"), null)).get(0);
list.add(value);
if (next.equals(new NamedNode(NAMESPACES.RDF + "nil"))) {
return list;
} else {
list = getList(store, next, list);
}
return list;
}
public static RDF4JStore readTurtle(InputStream is, RDFFormat format) {
Model model = null;
try {
//model = Rio.parse(mappingStream, "", format);
ParserConfig config = new ParserConfig();
config.set(BasicParserSettings.PRESERVE_BNODE_IDS, true);
model = Rio.parse(is, "", format, config, SimpleValueFactory.getInstance(), null);
is.close();
} catch (IOException | RDFParseException e) {
e.printStackTrace();
}
return new RDF4JStore(model);
}
/**
* Check if conforming to https://tools.ietf.org/html/bcp47#section-2.2.9
*
* @param s language tag
* @return True if valid language tag according to BCP 47
*/
public static boolean isValidrrLanguage(String s) {
return regexPatternLanguageTag.matcher(s).matches();
}
public static RDF4JStore readTurtle(File file, RDFFormat format) {
try {
logger.debug("Reading from " + file.getAbsolutePath());
return readTurtle(getInputStreamFromFile(file), format);
} catch (FileNotFoundException e) {
e.printStackTrace();
return new RDF4JStore(null);
}
}
public static RDF4JStore readTurtle(File mappingFile) {
return Utils.readTurtle(mappingFile, RDFFormat.TURTLE);
}
public static String encodeURI(String url) {
Escaper escaper = UrlEscapers.urlFragmentEscaper();
String result = escaper.escape(url);
result = result.replaceAll("!", "%21");
result = result.replaceAll("#", "%23");
result = result.replaceAll("\\$", "%24");
result = result.replaceAll("&", "%26");
result = result.replaceAll("'", "%27");
result = result.replaceAll("\\(", "%28");
result = result.replaceAll("\\)", "%29");
result = result.replaceAll("\\*", "%2A");
result = result.replaceAll("\\+", "%2B");
result = result.replaceAll(",", "%2C");
result = result.replaceAll("/", "%2F");
result = result.replaceAll(":", "%3A");
result = result.replaceAll(";", "%3B");
result = result.replaceAll("=", "%3D");
result = result.replaceAll("\\?", "%3F");
result = result.replaceAll("@", "%40");
result = result.replaceAll("\\[", "%5B");
result = result.replaceAll("]", "%5D");
return result;
}
public static String fileToString(File file) throws IOException {
Reader reader = getReaderFromFile(file);
int intValueOfChar;
String targetString = "";
while ((intValueOfChar = reader.read()) != -1) {
targetString += (char) intValueOfChar;
}
reader.close();
return targetString;
}
/*
Extracts the selected columns from the SQL query
Orders them alphabetically
Returns hash of concatenated string
*/
// todo: Take subquerries into account
public static int selectedColumnHash(String query) {
Pattern p = Pattern.compile("^SELECT(.*)FROM");
Matcher m = p.matcher(query.replace("\n", " ").replace("\r", " ").trim());
if (m.find()) {
String columns = m.group(1);
String[] columnNames = columns.replace("DISTINCT", "").replace(" ", "").split(",");
Arrays.sort(columnNames);
return String.join("", columnNames).hashCode();
}
throw new Error("Invalid query: " + query);
}
// Simpler version of above method. Hashes the whole query.
public static int getHash(String query) {
return query.hashCode();
}
public static String readFile(String path, Charset encoding) throws IOException {
if (encoding == null) {
encoding = StandardCharsets.UTF_8;
}
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
public static String getURLParamsString(Map<String, String> params)
throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
result.append("&");
}
String resultString = result.toString();
return resultString.length() > 0
? resultString.substring(0, resultString.length() - 1) // remove final '&'
: resultString;
}
public static int getFreePortNumber() throws IOException {
ServerSocket temp = new ServerSocket(0);
temp.setReuseAddress(true);
int portNumber = temp.getLocalPort();
temp.close();
return portNumber;
}
/**
* This method parse the generic template and returns a list of Extractors
* that can later be used by the executor
* to get the data values from the records.
**/
public static List<Extractor> parseTemplate(String template) {
ArrayList<Extractor> extractors = new ArrayList<>();
String current = "";
boolean previousWasBackslash = false;
boolean variableBusy = false;
if (template != null) {
for (Character c : template.toCharArray()) {
if (c == '{') {
if (previousWasBackslash) {
current += c;
previousWasBackslash = false;
} else if (variableBusy) {
throw new Error("Parsing of template failed. Probably a { was followed by a second { without first closing the first {. Make sure that you use { and } correctly.");
} else {
variableBusy = true;
if (!current.equals("")) {
extractors.add(new ConstantExtractor(current));
}
current = "";
}
} else if (c == '}') {
if (previousWasBackslash) {
current += c;
previousWasBackslash = false;
} else if (variableBusy) {
extractors.add(new ReferenceExtractor(current));
current = "";
variableBusy = false;
} else {
throw new Error("Parsing of template failed. Probably a } as used before a { was used. Make sure that you use { and } correctly.");
}
} else if (c == '\\') {
if (previousWasBackslash) {
previousWasBackslash = false;
current += c;
} else {
previousWasBackslash = true;
}
} else {
current += c;
}
}
if (!current.equals("")) {
extractors.add(new ConstantExtractor(current));
}
}
return extractors;
}
public static String randomString(int len) {
String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
SecureRandom rnd = new SecureRandom();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++)
sb.append(AB.charAt(rnd.nextInt(AB.length())));
return sb.toString();
}
public static String hashCode(String s) {
int hash = 0;
for (int i = 0; i < s.toCharArray().length; i++) {
hash += s.toCharArray()[i] * 31 ^ (s.toCharArray().length - 1 - i);
}
return Integer.toString(Math.abs(hash));
}
public static void ntriples2hdt(String rdfInputPath, String hdtOutputPath) {
// Configuration variables
String baseURI = "http://example.com/mydataset";
String inputType = "ntriples";
try {
// Create HDT from RDF file
HDT hdt = HDTManager.generateHDT(rdfInputPath, baseURI, RDFNotation.parse(inputType), new HDTSpecification(), null);
// Save generated HDT to a file
hdt.saveToHDT(hdtOutputPath, null);
// IMPORTANT: Free resources
hdt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean isValidIRI(String iri) {
UrlValidator urlValidator = new UrlValidator();
return urlValidator.isValid(iri);
}
public static String getBaseDirectiveTurtle(File file) {
StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get(file.getAbsolutePath()), StandardCharsets.UTF_8)) {
stream.forEach(s -> contentBuilder.append(s).append("\n"));
} catch (IOException e) {
e.printStackTrace();
}
String turtle = contentBuilder.toString();
return Utils.getBaseDirectiveTurtle(turtle);
}
public static String getBaseDirectiveTurtle(InputStream is) {
String turtle = null;
try {
turtle = IOUtils.toString(is, StandardCharsets.UTF_8);
} catch (IOException e) {
turtle = "";
}
return Utils.getBaseDirectiveTurtle(turtle);
}
public static String getBaseDirectiveTurtle(String turtle) {
Pattern p = Pattern.compile("@base <([^<>]*)>");
Matcher m = p.matcher(turtle);
if (m.find()) {
return m.group(1);
} else {
return null;
}
}
public static String transformDatatypeString(String input, String datatype) {
switch (datatype) {
case "http://www.w3.org/2001/XMLSchema#hexBinary":
// TODO
return input;
case "http://www.w3.org/2001/XMLSchema#decimal":
return "" + Double.parseDouble(input);
case "http://www.w3.org/2001/XMLSchema#integer":
return "" + Integer.parseInt(input);
case "http://www.w3.org/2001/XMLSchema#double":
return formatToScientific(Double.parseDouble(input));
case "http://www.w3.org/2001/XMLSchema#boolean":
switch (input) {
case "t":
case "true":
case "TRUE":
case "1":
return "true";
default:
return "false";
}
case "http://www.w3.org/2001/XMLSchema#date":
return input;
case "http://www.w3.org/2001/XMLSchema#time":
return input;
case "http://www.w3.org/2001/XMLSchema#dateTime":
return input.replace(" ", "T");
default:
return input;
}
}
private static String formatToScientific(Double d) {
BigDecimal input = BigDecimal.valueOf(d).stripTrailingZeros();
int precision = input.scale() < 0
? input.precision() - input.scale()
: input.precision();
StringBuilder s = new StringBuilder("0.0");
for (int i = 2; i < precision; i++) {
s.append("#");
}
s.append("E0");
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat df = (DecimalFormat) nf;
df.applyPattern(s.toString());
return df.format(d);
}
}