Content of file RdfUtilities.java.html
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>RdfUtilities.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueMarine II :: Commons for Tests</a> > <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-commons</a> > <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.util</a> > <span class="el_source">RdfUtilities.java</span></div><h1>RdfUtilities.java</h1><pre class="source lang-java linenums">/*
* *********************************************************************************************************************
*
* blueMarine II: Semantic Media Centre
* http://tidalwave.it/projects/bluemarine2
*
* Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *********************************************************************************************************************
*
* 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.
*
* *********************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/bluemarine2-src
* git clone https://github.com/tidalwave-it/bluemarine2-src
*
* *********************************************************************************************************************
*/
package it.tidalwave.bluemarine2.util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.text.Normalizer;
import java.time.Instant;
import java.util.Base64;
import java.util.Date;
import java.util.Iterator;
import java.util.Optional;
import java.util.stream.Stream;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.eclipse.rdf4j.common.iteration.Iteration;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
import org.eclipse.rdf4j.repository.RepositoryException;
import org.eclipse.rdf4j.rio.RDFHandler;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.n3.N3Writer;
import it.tidalwave.util.Id;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static java.text.Normalizer.Form.*;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.StreamSupport.stream;
import static java.nio.charset.StandardCharsets.UTF_8;
import static it.tidalwave.bluemarine2.util.Formatters.*;
import static lombok.AccessLevel.PRIVATE;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L71">@NoArgsConstructor(access = PRIVATE) @Slf4j</span>
public final class RdfUtilities
{
private static final String ALGORITHM = "SHA1";
<span class="nc" id="L76"> private static final ValueFactory FACTORY = SimpleValueFactory.getInstance(); // FIXME</span>
/*******************************************************************************************************************
*
* Exports the repository to the given file. FIXME: duplicated in DefaultPerstistence
| duplicated in DefaultPerstistence | |
*
******************************************************************************************************************/
public static void exportToFile (@Nonnull final Model model, @Nonnull final Path path)
throws RDFHandlerException, IOException, RepositoryException
{
<span class="nc" id="L86"> log.info("exportToFile({})", path);</span>
<span class="nc" id="L87"> Files.createDirectories(path.getParent());</span>
<span class="nc" id="L89"> try (final Writer w = Files.newBufferedWriter(path, UTF_8))</span>
{
<span class="nc" id="L91"> final RDFHandler writer = new SortingRDFHandler(new N3Writer(w));</span>
<span class="nc" id="L92"> writer.startRDF();</span>
<span class="nc" id="L93"> writer.handleNamespace("bio", "http://purl.org/vocab/bio/0.1/");</span>
<span class="nc" id="L94"> writer.handleNamespace("bmmo", "http://bluemarine.tidalwave.it/2015/04/mo/");</span>
<span class="nc" id="L95"> writer.handleNamespace("dc", "http://purl.org/dc/elements/1.1/");</span>
<span class="nc" id="L96"> writer.handleNamespace("foaf", "http://xmlns.com/foaf/0.1/");</span>
<span class="nc" id="L97"> writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");</span>
<span class="nc" id="L98"> writer.handleNamespace("mo", "http://purl.org/ontology/mo/");</span>
<span class="nc" id="L99"> writer.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");</span>
<span class="nc" id="L100"> writer.handleNamespace("rel", "http://purl.org/vocab/relationship/");</span>
<span class="nc" id="L101"> writer.handleNamespace("vocab", "http://dbtune.org/musicbrainz/resource/vocab/");</span>
<span class="nc" id="L102"> writer.handleNamespace("xs", "http://www.w3.org/2001/XMLSchema#");</span>
<span class="nc" id="L103"> model.stream().forEach(writer::handleStatement);</span>
<span class="nc" id="L104"> writer.endRDF();</span>
}
<span class="nc" id="L106"> }</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static <T, X extends RuntimeException> Stream<T> streamOf (@Nonnull final Iteration<T, X> iteration)
{
<span class="nc" id="L115"> return stream(spliteratorUnknownSize(iteratorOf(iteration), 0), false);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private static <T, X extends RuntimeException> Iterator<T> iteratorOf (@Nonnull final Iteration<T, X> iteration)
{
<span class="nc" id="L125"> return new Iterator<>()</span>
<span class="nc" id="L126"> {</span>
@Override
public boolean hasNext ()
{
<span class="nc" id="L130"> return iteration.hasNext();</span>
}
@Override
public T next ()
{
<span class="nc" id="L136"> return iteration.next();</span>
}
};
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (final Path path)
{
<span class="nc" id="L148"> return FACTORY.createLiteral(Normalizer.normalize(path.toString(), NFC));</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (final String string)
{
<span class="nc" id="L158"> return FACTORY.createLiteral(string);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Optional<Value> literalFor (final Optional<String> optionalString)
{
<span class="nc" id="L168"> return optionalString.map(RdfUtilities::literalFor);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (final Id id)
{
<span class="nc" id="L178"> return FACTORY.createLiteral(id.stringValue());</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (final int value)
{
<span class="nc" id="L188"> return FACTORY.createLiteral(value);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Optional<Value> literalForInt (final Optional<Integer> optionalInteger)
{
<span class="nc" id="L198"> return optionalInteger.map(RdfUtilities::literalFor);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (final long value)
{
<span class="nc" id="L208"> return FACTORY.createLiteral(value);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Optional<Value> literalForLong (final Optional<Long> optionalLong)
{
<span class="nc" id="L218"> return optionalLong.map(RdfUtilities::literalFor);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (final short value)
{
<span class="nc" id="L228"> return FACTORY.createLiteral(value);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (final float value)
{
<span class="nc" id="L238"> return FACTORY.createLiteral(value);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Optional<Value> literalForFloat (final Optional<Float> optionalFloat)
{
<span class="nc" id="L248"> return optionalFloat.map(RdfUtilities::literalFor);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Value literalFor (@Nonnull final Instant instant)
{
<span class="nc" id="L258"> return FACTORY.createLiteral(new Date(instant.toEpochMilli()));</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static IRI uriFor (@Nonnull final Id id)
{
<span class="nc" id="L268"> return uriFor(id.stringValue());</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static IRI uriFor (@Nonnull final String id)
{
<span class="nc" id="L278"> return FACTORY.createIRI(id);</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static IRI uriFor (@Nonnull final URL url)
{
<span class="nc" id="L288"> return FACTORY.createIRI(url.toString());</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static URL urlFor (@Nonnull final IRI uri)
throws MalformedURLException
{
<span class="nc" id="L299"> return new URL(uri.toString());</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static String emptyWhenNull (@Nullable final String string)
{
<span class="nc bnc" id="L309" title="All 2 branches missed."> return (string != null) ? string : "";</span>
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Id createSha1Id (@Nonnull final String string)
{
try
{
<span class="nc" id="L321"> final MessageDigest digestComputer = MessageDigest.getInstance(ALGORITHM);</span>
<span class="nc" id="L322"> digestComputer.update(string.getBytes(UTF_8));</span>
<span class="nc" id="L323"> return Id.of(toHexString(digestComputer.digest()));</span>
}
<span class="nc" id="L325"> catch (NoSuchAlgorithmException e)</span>
{
<span class="nc" id="L327"> throw new RuntimeException(e);</span>
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public static Id createSha1IdNew (@Nonnull final String string)
{
try
{
<span class="nc" id="L340"> final MessageDigest digestComputer = MessageDigest.getInstance(ALGORITHM);</span>
<span class="nc" id="L341"> digestComputer.update(string.getBytes(UTF_8));</span>
<span class="nc" id="L342"> return Id.of(Base64.getUrlEncoder().encodeToString(digestComputer.digest()));</span>
}
<span class="nc" id="L344"> catch (NoSuchAlgorithmException e)</span>
{
<span class="nc" id="L346"> throw new RuntimeException(e);</span>
}
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>