@Override public Set<Statement> deserialize(final String topic, final byte[] data) { if(data == null || data.length == 0) { // Return null because that is the contract of this method. return null; } try { final RDFParser parser = PARSER_FACTORY.getParser(); final Set<Statement> statements = new HashSet<>(); parser.setRDFHandler(new AbstractRDFHandler() { @Override public void handleStatement(final Statement statement) throws RDFHandlerException { log.debug("Statement: " + statement); statements.add( statement ); } }); parser.parse(new ByteArrayInputStream(data), null); return statements; } catch(final RDFParseException | RDFHandlerException | IOException e) { log.error("Could not deserialize a Set of VisibilityStatement objects using the RDF4J Rio Binary format.", e); return null; } }
/** * Parses the content of <code>is</code> input stream with the * specified parser <code>p</code> using <code>baseIRI</code>. * * @param format input format type. * @param is input stream containing <code>RDF</code>. * @param baseIRI base uri. * @return list of statements detected within the input stream. * @throws IOException if there is an error reading the {@link java.io.InputStream} */ public static Statement[] parseRDF(RDFFormat format, InputStream is, String baseIRI) throws IOException { final StatementCollector handler = new StatementCollector(); final RDFParser parser = getParser(format); parser.getParserConfig().set(BasicParserSettings.VERIFY_DATATYPE_VALUES, true); parser.setStopAtFirstError(true); parser.setPreserveBNodeIDs(true); parser.setRDFHandler(handler); parser.parse(is, baseIRI); return handler.getStatements().toArray( new Statement[handler.getStatements().size()] ); }
RDFHandler rdfHandler) throws IOException, RDFParseException, RDFHandlerException { RDFParser rdfParser = Rio.createParser(dataFormat, vf); rdfParser.setParserConfig(config); rdfParser.setParseErrorListener(new ParseErrorLogger()); rdfParser.setRDFHandler(rdfHandler); rdfParser.parse((InputStream) inputStreamOrReader, baseURI); } else if (inputStreamOrReader instanceof Reader) { rdfParser.parse((Reader) inputStreamOrReader, baseURI); } else { throw new IllegalArgumentException(
/** * Checks if the stream contains <i>Turtle</i> triple patterns. * * @param is input stream to be verified. * @return <code>true</code> if <i>Turtle</i> patterns are detected, <code>false</code> otherwise. * @throws IOException if there is an error checking the {@link java.io.InputStream} */ public static boolean checkTurtleFormat(InputStream is) throws IOException { String sample = extractDataSample(is, '.'); RDFParser turtleParser = Rio.createParser(RDFFormat.TURTLE); turtleParser.setDatatypeHandling(RDFParser.DatatypeHandling.VERIFY); turtleParser.setStopAtFirstError(true); turtleParser.setVerifyData(true); ByteArrayInputStream bais = new ByteArrayInputStream( sample.getBytes() ); try { turtleParser.parse(bais, ""); return true; } catch (Exception e) { return false; } }
/** * @param contentType * @param inputStream * @param baseURI * @throws ExecutionException * @throws InterruptedException */ void delete(RDFFormat contentType, InputStream inputStream, String baseURI) throws InterruptedException, ExecutionException { Future<Boolean> result = submit(() -> { logger.debug("executing delete operation"); RDFParser parser = Rio.createParser(contentType, txnConnection.getValueFactory()); parser.setRDFHandler(new WildcardRDFRemover(txnConnection)); parser.getParserConfig().set(BasicParserSettings.PRESERVE_BNODE_IDS, true); try { parser.parse(inputStream, baseURI); return true; } catch (IOException e) { logger.error("error during txn delete operation", e); throw new RuntimeException(e); } }); getFromFuture(result); }
@Override public void handleResponse(HttpMethod method) throws IOException, AGHttpException { String mimeType = getResponseMIMEType(method); if (!mimeType.equals(getRequestMIMEType())) { throw new AGHttpException("unexpected response MIME type: " + mimeType); } InputStream response = getInputStream(method); // Note: we ignore charset specified in the response. Reader reader = new InputStreamReader(response, StandardCharsets.UTF_8); try { RDFParser parser = Rio.createParser(format, vf); parser.setPreserveBNodeIDs(true); parser.setRDFHandler(rdfhandler); parser.parse(reader, method.getURI().getURI()); } catch (RDFParseException | RDFHandlerException e) { throw new AGHttpException(e); } }
parser.setRDFHandler(this); parser.setParseErrorListener(this); parser.setStopAtFirstError(!skipInvalid); if (skipInvalid) { parser.set(BasicParserSettings.VERIFY_URI_SYNTAX, false); parser.set(BasicParserSettings.VERIFY_RELATIVE_URIS, false); parser.set(BasicParserSettings.VERIFY_LANGUAGE_TAGS, false); parser.set(BasicParserSettings.VERIFY_DATATYPE_VALUES, verifyDataTypeValues); localIn = this.in; //synchronised parameters must be copied to a local variable for use outide of sync block localBaseUri = this.baseUri; if (defaultRdfContextPattern != null || overrideRdfContext) { final IRI defaultRdfContext = defaultRdfContextPattern == null ? null : SimpleValueFactory.getInstance().createIRI(MessageFormat.format(defaultRdfContextPattern, localBaseUri, file.toUri().getPath(), file.getName())); parser.setValueFactory(new SimpleValueFactory(){ @Override public Statement createStatement(Resource subject, IRI predicate, Value object) { parser.parse(localIn, localBaseUri); } catch (Exception e) { if (skipInvalid) {
private Model loadModel(String fileName) throws Exception { URL modelURL = this.getClass().getResource(TESTCASES_DIR + fileName); assertNotNull("Test file not found: " + fileName, modelURL); Model model = createEmptyModel(); Optional<RDFFormat> rdfFormat = Rio.getParserFormatForFileName(fileName); assertTrue("Unable to determine RDF format for file: " + fileName, rdfFormat.isPresent()); RDFParser parser = Rio.createParser(rdfFormat.get()); parser.setDatatypeHandling(DatatypeHandling.IGNORE); parser.setPreserveBNodeIDs(true); parser.setRDFHandler(new StatementCollector(model)); InputStream in = modelURL.openStream(); try { parser.parse(in, modelURL.toString()); return model; } finally { in.close(); } }
final ValueFactory vf = con.getRepository().getValueFactory(); RDFParser rdfParser = new TurtleParser(); rdfParser.setValueFactory(vf); rdfParser.setVerifyData(false); rdfParser.setStopAtFirstError(true); rdfParser.setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE); rdfParser.setRDFHandler(rdfInserter); rdfParser.parse(in, baseURI); con.commit(); } catch (RDFHandlerException e) {
@Override public void run() { org.eclipse.rdf4j.rio.RDFParser rdfParser = Rio .createParser(format); if (this.config != null) { rdfParser.setParserConfig(config); } rdfParser.setRDFHandler(new RDFListener(buffer)); try { rdfParser.parse(this.reader, ""); } catch (RDFParseException e) { throw new ParseError("An error occured while parsing", e); } catch (RDFHandlerException e) { throw new ParseError("An error occured while parsing", e); } catch (IOException e) { throw new ParseError("An error occured while parsing", e); } buffer.close(); try { this.reader.close(); } catch (IOException e) { } } }
final ExtractionResult extractionResult ) { parser.setDatatypeHandling( verifyDataType ? RDFParser.DatatypeHandling.VERIFY : RDFParser.DatatypeHandling.IGNORE ); parser.setStopAtFirstError(stopAtFirstError); parser.setParseErrorListener( new InternalParseErrorListener(extractionResult) ); parser.setValueFactory( new Any23ValueFactoryWrapper( SimpleValueFactory.getInstance(), parser.setRDFHandler(new RDFHandlerAdapter(extractionResult));
try { final RDFParser parser = getParser(extractionContext, extractionResult); parser.getParserConfig().setNonFatalErrors(new HashSet<RioSetting<?>>()); parser.getParserConfig().set(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES, true); parser.getParserConfig().addNonFatalError(BasicParserSettings.FAIL_ON_UNKNOWN_DATATYPES); parser.getParserConfig().set(BasicParserSettings.VERIFY_DATATYPE_VALUES, true); parser.getParserConfig().addNonFatalError(BasicParserSettings.VERIFY_DATATYPE_VALUES); parser.getParserConfig().set(BasicParserSettings.NORMALIZE_DATATYPE_VALUES, false); parser.getParserConfig().addNonFatalError(BasicParserSettings.NORMALIZE_DATATYPE_VALUES); RDFFormat format = parser.getRDFFormat(); String iri = extractionContext.getDocumentIRI().stringValue(); parser.parse(in, iri); } catch (RDFHandlerException ex) { throw new IllegalStateException("Unexpected exception.", ex);
@Override public void run() { try { logger.info("Starting parser"); rdfParser.parse(pipeIn, ""); } catch (RDFHandlerException | RDFParseException | IOException e) { logger.error(e.getMessage(), e); fail(e, this); throw new RuntimeException(e.getMessage(), e); } finally { try { pipeIn.close(); } catch (IOException e) { logger.warn(e); } } } };
/** * Instantiates the RecordReader. * @param format RDF serialization format to parse. * @param charBufferSize Number of input characters to hold in * memory; if exceeded, wait until the parser * thread consumes some text before proceeding * with reading input. * @param statementBufferSize Number of output statements to hold in * memory; if exceeded, wait until the * client consumes data before proceeding * with parsing. * @param timeoutSeconds Number of seconds to wait for the parser * thread to provide the next statement (or * state that there are none). If exceeded, * abort. */ RdfFileRecordReader(RDFFormat format, int charBufferSize, int statementBufferSize, int timeoutSeconds) { rdfParser = Rio.createParser(format); rdfParser.setRDFHandler(this); statementCache = new LinkedBlockingQueue<RyaStatementWritable>(statementBufferSize); pipeOut = new PipedWriter(); pipeIn = new PipedReader(charBufferSize); this.timeoutSeconds = timeoutSeconds; logger.info("Initializing RecordReader with parameters:"); logger.info("\tRDF serialization format = " + format.getName()); logger.info("\tinput buffer size = " + charBufferSize + " characters"); logger.info("\tstatement cache size = " + statementBufferSize); logger.info("\tparser timeout = " + timeoutSeconds + " seconds"); }
"Server responded with an unsupported file format: " + mimeType)); RDFParser parser = Rio.createParser(format, getValueFactory()); parser.setParserConfig(getParserConfig()); parser.setParseErrorListener(new ParseErrorLogger());
@Override public ParserConfig getParserConfig() { //Returns the parser configuration this connection uses for Rio-based operations. return rdfParser.getParserConfig(); }
@Override public void setParserConfig(ParserConfig config) { //Set the parser configuration this connection should use for RDFParser-based operations. rdfParser.setParserConfig(config); }
ParserConfig config = parser.getParserConfig(); parser.setRDFHandler(new StatementCollector(result)); parser.parse(in, resultFileURL);
private Statement parseNtriples(String ntriples) throws IOException, RDFParseException, RDFHandlerException { RDFParser parser = Rio.createParser(RDFFormat.NTRIPLES, vf); parser.setPreserveBNodeIDs(true); StatementCollector collector = new StatementCollector(); parser.setRDFHandler(collector); parser.parse(new StringReader(ntriples), "http://example.com/"); Statement st = collector.getStatements().iterator().next(); return st; }
/** * Processes a single <i>html script</i> node. * * @param turtleParser the parser used to digest node content. * @param documentIRI the IRI of the original HTML document. * @param n the script node. * @param er the extraction result used to store triples. */ private void processScriptNode(RDFParser turtleParser, IRI documentIRI, Node n, ExtractionResult er) { final Node idAttribute = n.getAttributes().getNamedItem("id"); final String graphName = documentIRI.stringValue() + ( idAttribute == null ? "" : "#" + idAttribute.getTextContent() ); try { turtleParser.parse( new StringReader(n.getTextContent()), graphName ); } catch (RDFParseException rdfpe) { er.notifyIssue( IssueReport.IssueLevel.ERROR, String.format( "An error occurred while parsing turtle content within script node: %s", Arrays.toString(DomUtils.getXPathListForNode(n)) ), rdfpe.getLineNumber(), rdfpe.getColumnNumber() ); } catch (Exception e) { er.notifyIssue(IssueReport.IssueLevel.ERROR, "An error occurred while processing RDF data.", -1, -1); } }