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.impl;
020
021import java.util.Optional;
022import java.util.UUID;
023
024import org.apache.commons.rdf.api.BlankNode;
025import org.apache.commons.rdf.api.BlankNodeOrIRI;
026import org.apache.commons.rdf.api.IRI;
027import org.apache.commons.rdf.api.RDFTerm;
028import org.apache.commons.rdf.jena.ConversionException;
029import org.apache.commons.rdf.jena.JenaBlankNode;
030import org.apache.commons.rdf.jena.JenaDataset;
031import org.apache.commons.rdf.jena.JenaGeneralizedQuadLike;
032import org.apache.commons.rdf.jena.JenaGeneralizedTripleLike;
033import org.apache.commons.rdf.jena.JenaGraph;
034import org.apache.commons.rdf.jena.JenaIRI;
035import org.apache.commons.rdf.jena.JenaLiteral;
036import org.apache.commons.rdf.jena.JenaQuad;
037import org.apache.commons.rdf.jena.JenaRDF;
038import org.apache.commons.rdf.jena.JenaRDFTerm;
039import org.apache.commons.rdf.jena.JenaTriple;
040import org.apache.jena.graph.Node;
041import org.apache.jena.graph.NodeFactory;
042import org.apache.jena.rdf.model.Model;
043import org.apache.jena.sparql.core.DatasetGraph;
044import org.apache.jena.sparql.core.DatasetGraphFactory;
045import org.apache.jena.sparql.graph.GraphFactory;
046import org.apache.jena.system.JenaSystem;
047
048/**
049 * Construct Jena implementations of Commons RDF.
050 * <p>
051 * This class is deliberately an abstract class, as it is an internal helper
052 * which <strong>may change</strong> in any minor version update; users should
053 * instead use {@link JenaRDF}.
054 * <p>
055 * For the purpose of blank node identity, some of these methods require a
056 * {@link UUID} to use as a salt. See {@link BlankNode#uniqueReference()} for
057 * details.
058 *
059 */
060public abstract class InternalJenaFactory {
061
062    static {
063        // http://jena.apache.org/documentation/notes/system-initialization.html
064        JenaSystem.init();
065    }
066
067
068    public JenaBlankNode createBlankNode(final String id, final UUID salt) {
069        return new JenaBlankNodeImpl(NodeFactory.createBlankNode(id), salt);
070    }
071
072    public JenaBlankNode createBlankNode(final UUID salt) {
073        return new JenaBlankNodeImpl(NodeFactory.createBlankNode(), salt);
074    }
075
076    public JenaDataset createDataset(final DatasetGraph datasetGraph, final UUID salt) {
077        return new JenaDatasetImpl(datasetGraph, salt);
078    }
079
080    public JenaDataset createDataset(final UUID salt) {
081        final DatasetGraph dg = DatasetGraphFactory.createGeneral();
082        // Or which createMethod() -- a bit confusing with lots of choice..
083        return new JenaDatasetImpl(dg, salt);
084    }
085
086    public JenaGeneralizedQuadLike createGeneralizedQuad(final org.apache.jena.sparql.core.Quad quad, final UUID salt) {
087        return new JenaGeneralizedQuadLikeImpl(quad, salt);
088    }
089
090    public JenaGeneralizedQuadLike createGeneralizedQuad(final RDFTerm subject, final RDFTerm predicate, final RDFTerm object,
091            final RDFTerm graphName) {
092        return new JenaGeneralizedQuadLikeImpl(subject, predicate, object, Optional.ofNullable(graphName));
093    }
094
095    public JenaGeneralizedTripleLike createGeneralizedTriple(final org.apache.jena.graph.Triple triple, final UUID salt) {
096        return new JenaGeneralizedTripleLikeImpl(triple, salt);
097    }
098
099    public JenaGeneralizedTripleLike createGeneralizedTriple(final RDFTerm subject, final RDFTerm predicate, final RDFTerm object) {
100        return new JenaGeneralizedTripleLikeImpl(subject, predicate, object);
101    }
102
103    public JenaGraph createGraph(final Model model, final UUID salt) {
104        return new JenaGraphImpl(model, salt);
105    }
106
107    public JenaGraph createGraph(final org.apache.jena.graph.Graph graph, final UUID salt) {
108        return new JenaGraphImpl(graph, salt);
109    }
110
111    public JenaGraph createGraph(final UUID salt) {
112        return new JenaGraphImpl(GraphFactory.createDefaultGraph(), salt);
113    }
114
115    public JenaIRI createIRI(final String iriStr) {
116        return new JenaIRIImpl(iriStr);
117    }
118
119    public JenaLiteral createLiteral(final String lexStr) {
120        return new JenaLiteralImpl(NodeFactory.createLiteral(lexStr));
121    }
122
123    public JenaLiteral createLiteralDT(final String lexStr, final String datatypeIRI) {
124        return new JenaLiteralImpl(NodeFactory.createLiteral(lexStr, NodeFactory.getType(datatypeIRI)));
125    }
126
127    public JenaLiteral createLiteralLang(final String lexStr, final String langTag) {
128        return new JenaLiteralImpl(NodeFactory.createLiteral(lexStr, langTag));
129    }
130
131    public JenaQuad createQuad(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object, final BlankNodeOrIRI graphName) {
132        return new JenaQuadImpl(subject, predicate, object, Optional.ofNullable(graphName));
133    }
134
135    public JenaQuad createQuad(final org.apache.jena.sparql.core.Quad quad, final UUID salt) {
136        return new JenaQuadImpl(quad, salt);
137    }
138
139    public JenaRDFTerm createRDFTerm(final Node node, final UUID salt) throws ConversionException {
140        if (!node.isConcrete()) {
141            throw new ConversionException("Node is not a concrete RDF Term: " + node);
142        }
143        if (node.isURI()) {
144            return new JenaIRIImpl(node);
145        }
146        if (node.isLiteral()) {
147            return new JenaLiteralImpl(node);
148        }
149        if (node.isBlank()) {
150            return new JenaBlankNodeImpl(node, salt);
151        }
152        if (node.equals(Node.ANY)) {
153            // NOTE: JenaAny no longer supported by Commons RDF
154            // return JenaAnyImpl.Singleton.instance;
155        }
156        if (node.isVariable()) {
157            // NOTE: JenaVariable no longer supported by Commons RDF
158            // return new JenaVariableImpl(node);
159        }
160        throw new ConversionException("Unrecognized node type: " + node);
161    }
162
163    public JenaTriple createTriple(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
164        return new JenaTripleImpl(subject, predicate, object);
165    }
166
167    public JenaTriple createTriple(final org.apache.jena.graph.Triple triple, final UUID salt) {
168        return new JenaTripleImpl(triple, salt);
169    }
170
171}