|
| 1 | +package fr.inria.corese.core.next.impl.io.parser.nquads; |
| 2 | + |
| 3 | +import fr.inria.corese.core.next.api.Model; |
| 4 | +import fr.inria.corese.core.next.api.ValueFactory; |
| 5 | +import fr.inria.corese.core.next.api.base.io.RDFFormat; |
| 6 | +import fr.inria.corese.core.next.api.base.io.parser.AbstractRDFParser; |
| 7 | +import fr.inria.corese.core.next.api.io.IOOptions; |
| 8 | +import fr.inria.corese.core.next.impl.exception.ParsingErrorException; |
| 9 | +import fr.inria.corese.core.next.impl.parser.antlr.NQuadsLexer; |
| 10 | +import fr.inria.corese.core.next.impl.parser.antlr.NQuadsParser; |
| 11 | +import org.antlr.v4.runtime.CharStream; |
| 12 | +import org.antlr.v4.runtime.CharStreams; |
| 13 | +import org.antlr.v4.runtime.CommonTokenStream; |
| 14 | +import org.antlr.v4.runtime.tree.ParseTree; |
| 15 | +import org.antlr.v4.runtime.tree.ParseTreeListener; |
| 16 | +import org.antlr.v4.runtime.tree.ParseTreeWalker; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.InputStream; |
| 20 | +import java.io.InputStreamReader; |
| 21 | +import java.io.Reader; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | + |
| 24 | +/** |
| 25 | + * An ANTLR4-based parser for N-Quads format. |
| 26 | + * This parser uses an ANTLR grammar to tokenize and parse N-Quads documents, |
| 27 | + * then a listener to build the RDF model. |
| 28 | + */ |
| 29 | +public class ANTLRNQuadsParser extends AbstractRDFParser { |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructor for the ANTLRNQuadsParser. |
| 33 | + * |
| 34 | + * @param model The RDF model to populate. |
| 35 | + * @param factory The ValueFactory for creating RDF resources. |
| 36 | + */ |
| 37 | + public ANTLRNQuadsParser(Model model, ValueFactory factory) { |
| 38 | + super(model, factory); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructor for the ANTLRNQuadsParser with configuration options. |
| 43 | + * |
| 44 | + * @param model The RDF model to populate. |
| 45 | + * @param factory The ValueFactory for creating RDF resources. |
| 46 | + * @param config The configuration options for parsing. |
| 47 | + */ |
| 48 | + public ANTLRNQuadsParser(Model model, ValueFactory factory, IOOptions config) { |
| 49 | + super(model, factory, config); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public RDFFormat getRDFFormat() { |
| 54 | + return RDFFormat.NQUADS; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + @Override |
| 59 | + public void parse(InputStream in) throws ParsingErrorException { |
| 60 | + parse(new InputStreamReader(in, StandardCharsets.UTF_8), null); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void parse(InputStream in, String baseURI) throws ParsingErrorException { |
| 65 | + parse(new InputStreamReader(in, StandardCharsets.UTF_8), baseURI); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void parse(Reader reader) throws ParsingErrorException { |
| 70 | + parse(reader, null); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Parses N-Quads data from a Reader using ANTLR4. |
| 75 | + * |
| 76 | + * @param reader The Reader to read RDF data from. |
| 77 | + * @param baseURI The base URI (ignored for N-Quads as all URIs are absolute). |
| 78 | + * @throws ParsingErrorException if a parsing or I/O error occurs. |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public void parse(Reader reader, String baseURI) throws ParsingErrorException { |
| 82 | + try { |
| 83 | + CharStream charStream = CharStreams.fromReader(reader); |
| 84 | + NQuadsLexer lexer = new NQuadsLexer(charStream); |
| 85 | + CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 86 | + |
| 87 | + NQuadsParser antlrParser = new NQuadsParser(tokens); |
| 88 | + ParseTreeWalker walker = new ParseTreeWalker(); |
| 89 | + ParseTree tree = antlrParser.nquadsDoc(); |
| 90 | + |
| 91 | + NQuadsListener listener = new NQuadsListener(getModel(), getValueFactory(), getConfig()); |
| 92 | + |
| 93 | + walker.walk((ParseTreeListener) listener, tree); |
| 94 | + |
| 95 | + } catch (IOException e) { |
| 96 | + throw new ParsingErrorException("Failed to parse N-Quads: " + e.getMessage(), e); |
| 97 | + } catch (Exception e) { |
| 98 | + throw new ParsingErrorException("Unexpected error during N-Quads parsing: " + e.getMessage(), e); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments