Skip to contentPackage: RdfUtilities$1
RdfUtilities$1
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.util;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.text.Normalizer;
32: import java.time.Instant;
33: import java.util.Base64;
34: import java.util.Date;
35: import java.util.Iterator;
36: import java.util.Optional;
37: import java.util.stream.Stream;
38: import java.io.IOException;
39: import java.io.Writer;
40: import java.nio.file.Files;
41: import java.nio.file.Path;
42: import java.net.MalformedURLException;
43: import java.net.URL;
44: import java.security.MessageDigest;
45: import java.security.NoSuchAlgorithmException;
46: import org.eclipse.rdf4j.common.iteration.Iteration;
47: import org.eclipse.rdf4j.model.IRI;
48: import org.eclipse.rdf4j.model.Model;
49: import org.eclipse.rdf4j.model.Value;
50: import org.eclipse.rdf4j.model.ValueFactory;
51: import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
52: import org.eclipse.rdf4j.repository.RepositoryException;
53: import org.eclipse.rdf4j.rio.RDFHandler;
54: import org.eclipse.rdf4j.rio.RDFHandlerException;
55: import org.eclipse.rdf4j.rio.n3.N3Writer;
56: import it.tidalwave.util.Id;
57: import lombok.NoArgsConstructor;
58: import lombok.extern.slf4j.Slf4j;
59: import static java.text.Normalizer.Form.*;
60: import static java.util.Spliterators.spliteratorUnknownSize;
61: import static java.util.stream.StreamSupport.stream;
62: import static java.nio.charset.StandardCharsets.UTF_8;
63: import static it.tidalwave.bluemarine2.util.Formatters.*;
64: import static lombok.AccessLevel.PRIVATE;
65:
66: /***********************************************************************************************************************
67: *
68: * @author Fabrizio Giudici
69: *
70: **********************************************************************************************************************/
71: @NoArgsConstructor(access = PRIVATE) @Slf4j
72: public final class RdfUtilities
73: {
74: private static final String ALGORITHM = "SHA1";
75:
76: private static final ValueFactory FACTORY = SimpleValueFactory.getInstance(); // FIXME
77:
78: /*******************************************************************************************************************
79: *
80: * Exports the repository to the given file. FIXME: duplicated in DefaultPerstistence
81: *
82: ******************************************************************************************************************/
83: public static void exportToFile (@Nonnull final Model model, @Nonnull final Path path)
84: throws RDFHandlerException, IOException, RepositoryException
85: {
86: log.info("exportToFile({})", path);
87: Files.createDirectories(path.getParent());
88:
89: try (final Writer w = Files.newBufferedWriter(path, UTF_8))
90: {
91: final RDFHandler writer = new SortingRDFHandler(new N3Writer(w));
92: writer.startRDF();
93: writer.handleNamespace("bio", "http://purl.org/vocab/bio/0.1/");
94: writer.handleNamespace("bmmo", "http://bluemarine.tidalwave.it/2015/04/mo/");
95: writer.handleNamespace("dc", "http://purl.org/dc/elements/1.1/");
96: writer.handleNamespace("foaf", "http://xmlns.com/foaf/0.1/");
97: writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");
98: writer.handleNamespace("mo", "http://purl.org/ontology/mo/");
99: writer.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
100: writer.handleNamespace("rel", "http://purl.org/vocab/relationship/");
101: writer.handleNamespace("vocab", "http://dbtune.org/musicbrainz/resource/vocab/");
102: writer.handleNamespace("xs", "http://www.w3.org/2001/XMLSchema#");
103: model.stream().forEach(writer::handleStatement);
104: writer.endRDF();
105: }
106: }
107:
108: /*******************************************************************************************************************
109: *
110: *
111: ******************************************************************************************************************/
112: @Nonnull
113: public static <T, X extends RuntimeException> Stream<T> streamOf (@Nonnull final Iteration<T, X> iteration)
114: {
115: return stream(spliteratorUnknownSize(iteratorOf(iteration), 0), false);
116: }
117:
118: /*******************************************************************************************************************
119: *
120: *
121: ******************************************************************************************************************/
122: @Nonnull
123: private static <T, X extends RuntimeException> Iterator<T> iteratorOf (@Nonnull final Iteration<T, X> iteration)
124: {
125: return new Iterator<>()
126: {
127: @Override
128: public boolean hasNext ()
129: {
130: return iteration.hasNext();
131: }
132:
133: @Override
134: public T next ()
135: {
136: return iteration.next();
137: }
138: };
139: }
140:
141: /*******************************************************************************************************************
142: *
143: *
144: ******************************************************************************************************************/
145: @Nonnull
146: public static Value literalFor (final Path path)
147: {
148: return FACTORY.createLiteral(Normalizer.normalize(path.toString(), NFC));
149: }
150:
151: /*******************************************************************************************************************
152: *
153: *
154: ******************************************************************************************************************/
155: @Nonnull
156: public static Value literalFor (final String string)
157: {
158: return FACTORY.createLiteral(string);
159: }
160:
161: /*******************************************************************************************************************
162: *
163: *
164: ******************************************************************************************************************/
165: @Nonnull
166: public static Optional<Value> literalFor (final Optional<String> optionalString)
167: {
168: return optionalString.map(RdfUtilities::literalFor);
169: }
170:
171: /*******************************************************************************************************************
172: *
173: *
174: ******************************************************************************************************************/
175: @Nonnull
176: public static Value literalFor (final Id id)
177: {
178: return FACTORY.createLiteral(id.stringValue());
179: }
180:
181: /*******************************************************************************************************************
182: *
183: *
184: ******************************************************************************************************************/
185: @Nonnull
186: public static Value literalFor (final int value)
187: {
188: return FACTORY.createLiteral(value);
189: }
190:
191: /*******************************************************************************************************************
192: *
193: *
194: ******************************************************************************************************************/
195: @Nonnull
196: public static Optional<Value> literalForInt (final Optional<Integer> optionalInteger)
197: {
198: return optionalInteger.map(RdfUtilities::literalFor);
199: }
200:
201: /*******************************************************************************************************************
202: *
203: *
204: ******************************************************************************************************************/
205: @Nonnull
206: public static Value literalFor (final long value)
207: {
208: return FACTORY.createLiteral(value);
209: }
210:
211: /*******************************************************************************************************************
212: *
213: *
214: ******************************************************************************************************************/
215: @Nonnull
216: public static Optional<Value> literalForLong (final Optional<Long> optionalLong)
217: {
218: return optionalLong.map(RdfUtilities::literalFor);
219: }
220:
221: /*******************************************************************************************************************
222: *
223: *
224: ******************************************************************************************************************/
225: @Nonnull
226: public static Value literalFor (final short value)
227: {
228: return FACTORY.createLiteral(value);
229: }
230:
231: /*******************************************************************************************************************
232: *
233: *
234: ******************************************************************************************************************/
235: @Nonnull
236: public static Value literalFor (final float value)
237: {
238: return FACTORY.createLiteral(value);
239: }
240:
241: /*******************************************************************************************************************
242: *
243: *
244: ******************************************************************************************************************/
245: @Nonnull
246: public static Optional<Value> literalForFloat (final Optional<Float> optionalFloat)
247: {
248: return optionalFloat.map(RdfUtilities::literalFor);
249: }
250:
251: /*******************************************************************************************************************
252: *
253: *
254: ******************************************************************************************************************/
255: @Nonnull
256: public static Value literalFor (@Nonnull final Instant instant)
257: {
258: return FACTORY.createLiteral(new Date(instant.toEpochMilli()));
259: }
260:
261: /*******************************************************************************************************************
262: *
263: *
264: ******************************************************************************************************************/
265: @Nonnull
266: public static IRI uriFor (@Nonnull final Id id)
267: {
268: return uriFor(id.stringValue());
269: }
270:
271: /*******************************************************************************************************************
272: *
273: *
274: ******************************************************************************************************************/
275: @Nonnull
276: public static IRI uriFor (@Nonnull final String id)
277: {
278: return FACTORY.createIRI(id);
279: }
280:
281: /*******************************************************************************************************************
282: *
283: *
284: ******************************************************************************************************************/
285: @Nonnull
286: public static IRI uriFor (@Nonnull final URL url)
287: {
288: return FACTORY.createIRI(url.toString());
289: }
290:
291: /*******************************************************************************************************************
292: *
293: *
294: ******************************************************************************************************************/
295: @Nonnull
296: public static URL urlFor (@Nonnull final IRI uri)
297: throws MalformedURLException
298: {
299: return new URL(uri.toString());
300: }
301:
302: /*******************************************************************************************************************
303: *
304: *
305: ******************************************************************************************************************/
306: @Nonnull
307: public static String emptyWhenNull (@Nullable final String string)
308: {
309: return (string != null) ? string : "";
310: }
311:
312: /*******************************************************************************************************************
313: *
314: *
315: ******************************************************************************************************************/
316: @Nonnull
317: public static Id createSha1Id (@Nonnull final String string)
318: {
319: try
320: {
321: final MessageDigest digestComputer = MessageDigest.getInstance(ALGORITHM);
322: digestComputer.update(string.getBytes(UTF_8));
323: return Id.of(toHexString(digestComputer.digest()));
324: }
325: catch (NoSuchAlgorithmException e)
326: {
327: throw new RuntimeException(e);
328: }
329: }
330:
331: /*******************************************************************************************************************
332: *
333: *
334: ******************************************************************************************************************/
335: @Nonnull
336: public static Id createSha1IdNew (@Nonnull final String string)
337: {
338: try
339: {
340: final MessageDigest digestComputer = MessageDigest.getInstance(ALGORITHM);
341: digestComputer.update(string.getBytes(UTF_8));
342: return Id.of(Base64.getUrlEncoder().encodeToString(digestComputer.digest()));
343: }
344: catch (NoSuchAlgorithmException e)
345: {
346: throw new RuntimeException(e);
347: }
348: }
349: }