All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.hasmac.jsonld.deseralization.ListToRdf Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 APICATALOG and HASMAC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 */

package no.hasmac.jsonld.deseralization;

import jakarta.json.JsonArray;
import jakarta.json.JsonValue;
import no.hasmac.jsonld.JsonLdError;
import no.hasmac.jsonld.JsonLdOptions;
import no.hasmac.jsonld.JsonLdOptions.RdfDirection;
import no.hasmac.jsonld.flattening.NodeMap;
import no.hasmac.jsonld.json.JsonUtils;
import no.hasmac.rdf.RdfValueFactory;
import no.hasmac.rdf.lang.RdfConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 *
 * @see List to RDF Conversion
 *
 */
final class ListToRdf {

    private final RdfValueFactory rdfValueFactory;
    // required
    private final JsonArray list;
    private final List triples;
    private final NodeMap nodeMap;
    private final JsonLdOptions options;

    // optional
    private RdfDirection rdfDirection;
    private boolean uriValidation;

    private ListToRdf(final JsonArray list, final List triples, NodeMap nodeMap, RdfValueFactory rdfValueFactory, JsonLdOptions options) {
        this.list = list;
        this.triples = triples;
        this.nodeMap = nodeMap;
        this.rdfValueFactory = rdfValueFactory;

        // default values
        this.rdfDirection = null;
        this.uriValidation = JsonLdOptions.DEFAULT_URI_VALIDATION;
        this.options = options;
    }

    public static  ListToRdf with(final JsonArray list, final List triples, NodeMap nodeMap, RdfValueFactory rdfValueFactory, JsonLdOptions options) {
        return new ListToRdf(list, triples, nodeMap, rdfValueFactory, options);
    }

    public ListToRdf rdfDirection(RdfDirection rdfDirection) {
        this.rdfDirection = rdfDirection;
        return this;
    }

    public Value build() throws JsonLdError {

        // 1.
        if (JsonUtils.isEmptyArray(list)) {
            return rdfValueFactory.createIRI(RdfConstants.NIL);
        }

        // 2.
        final String[] bnodes = new String[list.size()];

        IntStream.range(0,  bnodes.length).forEach(i -> bnodes[i] = nodeMap.createIdentifier());

        // 3.
        int index = 0;
        for (final JsonValue item : list) {

            final String subject = bnodes[index];
            index++;

            // 3.1.
            final List embeddedTriples = new ArrayList<>();

            // 3.2.
            Value rdfValue = ObjectToRdf
                    .with(item.asJsonObject(), embeddedTriples, nodeMap, rdfValueFactory, options)
                    .rdfDirection(rdfDirection)
                    .uriValidation(uriValidation)
                    .build();
            if(rdfValue != null) {
                                triples.add(rdfValueFactory.createTriple(
                                                rdfValueFactory.createBlankNode(subject),
                                                rdfValueFactory.createIRI(RdfConstants.FIRST),
                                                rdfValue));
            }

            // 3.4.
            final Value rest = (index < bnodes.length) ? rdfValueFactory.createBlankNode(bnodes[index])
                                        : rdfValueFactory.createIRI(RdfConstants.NIL)
                                        ;

            triples.add(rdfValueFactory.createTriple(
                                    rdfValueFactory.createBlankNode(subject),
                                    rdfValueFactory.createIRI(RdfConstants.REST),
                                    rest
                                    ));

            // 3.5.
            triples.addAll(embeddedTriples);
        }

        // 4.
        return rdfValueFactory.createBlankNode(bnodes[0]);
    }

    public ListToRdf uriValidation(boolean uriValidation) {
        this.uriValidation = uriValidation;
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy