1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.ontotext.ordi.compatibilitytests;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import org.openrdf.model.Statement;
28 import org.openrdf.model.ValueFactory;
29 import org.openrdf.model.impl.ValueFactoryImpl;
30
31 /**
32 * This class is helper parser to process the owl file in the resource
33 * directory. It'll not process arbitrary .nt files! Use a generic .nt parser
34 * instead!
35 *
36 * @author vassil
37 *
38 */
39 class NTParser {
40
41 private BufferedReader reader;
42 private ValueFactory factory;
43 private Pattern pattern;
44
45 public NTParser(InputStream stream) {
46 if (stream == null) {
47 throw new IllegalArgumentException();
48 }
49 this.reader = new BufferedReader(new InputStreamReader(stream));
50 this.factory = new ValueFactoryImpl();
51 this.pattern = Pattern
52 .compile("\\s*<(.+)>\\s*<(.+)>\\s*(?:<|\")(.+)(?:>|\")\\s*\\.");
53 }
54
55 /**
56 * Reads the next available statement in NT file format.
57 *
58 * @return next statement or null
59 */
60 Statement next() {
61 try {
62 String line = reader.readLine();
63 if (line == null) {
64 reader.close();
65 return null;
66 }
67 Matcher match = pattern.matcher(line);
68 if (match.matches() == false || match.groupCount() != 3) {
69 throw new RuntimeException(
70 "The input file format is not supported!");
71 }
72 return factory.createStatement(factory.createURI(match.group(1)),
73 factory.createURI(match.group(2)), match.group(3)
74 .startsWith("http://") ? factory.createURI(match
75 .group(3)) : factory.createLiteral(match.group(3)));
76 } catch (IOException e) {
77 throw new RuntimeException("Could not read input stream!", e);
78 }
79 }
80
81 }