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 :: Persistence</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-commons</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.util</a> &gt; <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 &quot;License&quot;); 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 &quot;AS IS&quot; 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 = &quot;SHA1&quot;;

<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 * ******************************************************************************************************************/ public static void exportToFile (@Nonnull final Model model, @Nonnull final Path path) throws RDFHandlerException, IOException, RepositoryException { <span class="nc" id="L86"> log.info(&quot;exportToFile({})&quot;, 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(&quot;bio&quot;, &quot;http://purl.org/vocab/bio/0.1/&quot;);</span> <span class="nc" id="L94"> writer.handleNamespace(&quot;bmmo&quot;, &quot;http://bluemarine.tidalwave.it/2015/04/mo/&quot;);</span> <span class="nc" id="L95"> writer.handleNamespace(&quot;dc&quot;, &quot;http://purl.org/dc/elements/1.1/&quot;);</span> <span class="nc" id="L96"> writer.handleNamespace(&quot;foaf&quot;, &quot;http://xmlns.com/foaf/0.1/&quot;);</span> <span class="nc" id="L97"> writer.handleNamespace(&quot;owl&quot;, &quot;http://www.w3.org/2002/07/owl#&quot;);</span> <span class="nc" id="L98"> writer.handleNamespace(&quot;mo&quot;, &quot;http://purl.org/ontology/mo/&quot;);</span> <span class="nc" id="L99"> writer.handleNamespace(&quot;rdfs&quot;, &quot;http://www.w3.org/2000/01/rdf-schema#&quot;);</span> <span class="nc" id="L100"> writer.handleNamespace(&quot;rel&quot;, &quot;http://purl.org/vocab/relationship/&quot;);</span> <span class="nc" id="L101"> writer.handleNamespace(&quot;vocab&quot;, &quot;http://dbtune.org/musicbrainz/resource/vocab/&quot;);</span> <span class="nc" id="L102"> writer.handleNamespace(&quot;xs&quot;, &quot;http://www.w3.org/2001/XMLSchema#&quot;);</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 &lt;T, X extends RuntimeException&gt; Stream&lt;T&gt; streamOf (@Nonnull final Iteration&lt;T, X&gt; iteration) { <span class="nc" id="L115"> return stream(spliteratorUnknownSize(iteratorOf(iteration), 0), false);</span> } /******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private static &lt;T, X extends RuntimeException&gt; Iterator&lt;T&gt; iteratorOf (@Nonnull final Iteration&lt;T, X&gt; iteration) { <span class="nc" id="L125"> return new Iterator&lt;&gt;()</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&lt;Value&gt; literalFor (final Optional&lt;String&gt; 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&lt;Value&gt; literalForInt (final Optional&lt;Integer&gt; 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&lt;Value&gt; literalForLong (final Optional&lt;Long&gt; 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&lt;Value&gt; literalForFloat (final Optional&lt;Float&gt; 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 : &quot;&quot;;</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>