001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.rdf.jena.experimental;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.nio.file.Files;
024import java.util.function.Consumer;
025
026import org.apache.commons.rdf.api.IRI;
027import org.apache.commons.rdf.api.QuadLike;
028import org.apache.commons.rdf.api.RDF;
029import org.apache.commons.rdf.api.RDFTerm;
030import org.apache.commons.rdf.api.TripleLike;
031import org.apache.commons.rdf.jena.JenaGraph;
032import org.apache.commons.rdf.jena.JenaRDF;
033import org.apache.commons.rdf.simple.experimental.AbstractRDFParser;
034import org.apache.jena.graph.Graph;
035import org.apache.jena.riot.Lang;
036import org.apache.jena.riot.RDFParser;
037import org.apache.jena.riot.system.StreamRDF;
038import org.apache.jena.riot.system.StreamRDFLib;
039
040public class JenaRDFParser extends AbstractRDFParser<JenaRDFParser> {
041
042    private Consumer<TripleLike> generalizedConsumerTriple;
043    private Consumer<QuadLike<RDFTerm>> generalizedConsumerQuad;
044
045    @Override
046    protected RDF createRDFTermFactory() {
047        return new JenaRDF();
048    }
049
050    public JenaRDFParser targetGeneralizedTriple(final Consumer<TripleLike> consumer) {
051        final JenaRDFParser c = this.clone();
052        c.resetTarget();
053        c.generalizedConsumerTriple = consumer;
054        return c;
055    }
056
057    public JenaRDFParser targetGeneralizedQuad(final Consumer<QuadLike<RDFTerm>> consumer) {
058        final JenaRDFParser c = this.clone();
059        c.resetTarget();
060        c.generalizedConsumerQuad = consumer;
061        return c;
062    }
063
064    @Override
065    protected void resetTarget() {
066        super.resetTarget();
067        this.generalizedConsumerTriple = null;
068        this.generalizedConsumerQuad = null;
069    }
070
071    @Override
072    protected void parseSynchronusly() throws IOException {
073        StreamRDF dest;
074        final JenaRDF jenaRDF = getJenaFactory();
075        if (getTargetGraph().isPresent() && getTargetGraph().get() instanceof JenaGraph) {
076            final Graph jenaGraph = ((JenaGraph) getTargetGraph().get()).asJenaGraph();
077            dest = StreamRDFLib.graph(jenaGraph);
078        } else {
079            if (generalizedConsumerQuad != null) {
080                dest = jenaRDF.streamJenaToGeneralizedQuad(generalizedConsumerQuad);
081            } else if (generalizedConsumerTriple != null) {
082                dest = jenaRDF.streamJenaToGeneralizedTriple(generalizedConsumerTriple);
083            } else {
084                dest = JenaRDF.streamJenaToQuad(getRdfTermFactory().get(), getTarget());
085            }
086        }
087
088        final Lang lang = getContentTypeSyntax().flatMap(jenaRDF::asJenaLang).orElse(null);
089        final String baseStr = getBase().map(IRI::getIRIString).orElse(null);
090
091        if (getSourceIri().isPresent()) {
092                    RDFParser.source(getSourceIri().get().toString()).base(baseStr).lang(lang).parse(dest);
093        } else if (getSourceFile().isPresent()) {
094            try (InputStream s = Files.newInputStream(getSourceFile().get())) {
095                    RDFParser.source(s).base(baseStr).lang(lang).parse(dest);
096            }
097        } else {
098            RDFParser.source(getSourceInputStream().get()).base(baseStr).lang(lang).parse(dest);
099        }
100    }
101
102    private JenaRDF getJenaFactory() {
103        return (JenaRDF) getRdfTermFactory().filter(JenaRDF.class::isInstance).orElseGet(this::createRDFTermFactory);
104    }
105
106}