protected RDFHandler makeRDFHandler()

in commons-rdf-rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/experimental/RDF4JParser.java [138:180]


    protected RDFHandler makeRDFHandler() {

        // TODO: Can we join the below DF4JDataset and RDF4JGraph cases
        // using RDF4JGraphLike<TripleLike<BlankNodeOrIRI,IRI,RDFTerm>>
        // or will that need tricky generics types?

        if (getTargetDataset().filter(RDF4JDataset.class::isInstance).isPresent()) {
            // One of us, we can add them as Statements directly
            final RDF4JDataset dataset = (RDF4JDataset) getTargetDataset().get();
            if (dataset.asRepository().isPresent()) {
                return new RDFInserter(dataset.asRepository().get().getConnection());
            }
            if (dataset.asModel().isPresent()) {
                final Model model = dataset.asModel().get();
                return new AddToModel(model);
            }
            // Not backed by Repository or Model?
            // Third-party RDF4JDataset subclass, so we'll fall through to the
            // getTarget() handling further down
        } else if (getTargetGraph().filter(RDF4JGraph.class::isInstance).isPresent()) {
            final RDF4JGraph graph = (RDF4JGraph) getTargetGraph().get();

            if (graph.asRepository().isPresent()) {
                final RDFInserter inserter = new RDFInserter(graph.asRepository().get().getConnection());
                if (!graph.getContextMask().isEmpty()) {
                    final Stream<RDF4JBlankNodeOrIRI> b = graph.getContextMask().stream();
                    final Stream<Resource> c = b.map(RDF4JBlankNodeOrIRI::asValue);
                    final Resource[] contexts = c.toArray(Resource[]::new);
                    inserter.enforceContext(contexts);
                }
                return inserter;
            }
            if (graph.asModel().isPresent() && graph.getContextMask().isEmpty()) {
                // the model accepts any quad
                final Model model = graph.asModel().get();
                return new AddToModel(model);
            }
            // else - fall through
        }

        // Fall thorough: let target() consume our converted quads.
        return new AddToQuadConsumer(getTarget());
    }