Skip to contentMethod: literalFor(long)
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.PrintWriter;
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 PrintWriter pw = new PrintWriter(Files.newBufferedWriter(path, UTF_8)))
90: {
91: final RDFHandler writer = new SortingRDFHandler(new N3Writer(pw));
92: writer.startRDF();
93: // FIXME: use Iterations - and sort
94: // for (final Namespace namespace : connection.getNamespaces().asList())
95: // {
96: // writer.handleNamespace(namespace.getPrefix(), namespace.getName());
97: // }
98:
99: writer.handleNamespace("bio", "http://purl.org/vocab/bio/0.1/");
100: writer.handleNamespace("bmmo", "http://bluemarine.tidalwave.it/2015/04/mo/");
101: writer.handleNamespace("dc", "http://purl.org/dc/elements/1.1/");
102: writer.handleNamespace("foaf", "http://xmlns.com/foaf/0.1/");
103: writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");
104: writer.handleNamespace("mo", "http://purl.org/ontology/mo/");
105: writer.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
106: writer.handleNamespace("rel", "http://purl.org/vocab/relationship/");
107: writer.handleNamespace("vocab", "http://dbtune.org/musicbrainz/resource/vocab/");
108: writer.handleNamespace("xs", "http://www.w3.org/2001/XMLSchema#");
109:
110: model.stream().forEachOrdered(writer::handleStatement);
111: writer.endRDF();
112: }
113: }
114:
115: /*******************************************************************************************************************
116: *
117: *
118: ******************************************************************************************************************/
119: @Nonnull
120: public static <T, X extends RuntimeException> Stream<T> streamOf (@Nonnull final Iteration<T, X> iteration)
121: {
122: return stream(spliteratorUnknownSize(iteratorOf(iteration), 0), false);
123: }
124:
125: /*******************************************************************************************************************
126: *
127: *
128: ******************************************************************************************************************/
129: @Nonnull
130: private static <T, X extends RuntimeException> Iterator<T> iteratorOf (@Nonnull final Iteration<T, X> iteration)
131: {
132: return new Iterator<>()
133: {
134: @Override
135: public boolean hasNext ()
136: {
137: return iteration.hasNext();
138: }
139:
140: @Override
141: public T next ()
142: {
143: return iteration.next();
144: }
145: };
146: }
147:
148: /*******************************************************************************************************************
149: *
150: *
151: ******************************************************************************************************************/
152: @Nonnull
153: public static Value literalFor (final Path path)
154: {
155: return FACTORY.createLiteral(Normalizer.normalize(path.toString(), NFC));
156: }
157:
158: /*******************************************************************************************************************
159: *
160: *
161: ******************************************************************************************************************/
162: @Nonnull
163: public static Value literalFor (final String string)
164: {
165: return FACTORY.createLiteral(string);
166: }
167:
168: /*******************************************************************************************************************
169: *
170: *
171: ******************************************************************************************************************/
172: @Nonnull
173: public static Optional<Value> literalFor (final Optional<String> optionalString)
174: {
175: return optionalString.map(RdfUtilities::literalFor);
176: }
177:
178: /*******************************************************************************************************************
179: *
180: *
181: ******************************************************************************************************************/
182: @Nonnull
183: public static Value literalFor (final Id id)
184: {
185: return FACTORY.createLiteral(id.stringValue());
186: }
187:
188: /*******************************************************************************************************************
189: *
190: *
191: ******************************************************************************************************************/
192: @Nonnull
193: public static Value literalFor (final int value)
194: {
195: return FACTORY.createLiteral(value);
196: }
197:
198: /*******************************************************************************************************************
199: *
200: *
201: ******************************************************************************************************************/
202: @Nonnull
203: public static Optional<Value> literalForInt (final Optional<Integer> optionalInteger)
204: {
205: return optionalInteger.map(RdfUtilities::literalFor);
206: }
207:
208: /*******************************************************************************************************************
209: *
210: *
211: ******************************************************************************************************************/
212: @Nonnull
213: public static Value literalFor (final long value)
214: {
215: return FACTORY.createLiteral(value);
216: }
217:
218: /*******************************************************************************************************************
219: *
220: *
221: ******************************************************************************************************************/
222: @Nonnull
223: public static Optional<Value> literalForLong (final Optional<Long> optionalLong)
224: {
225: return optionalLong.map(RdfUtilities::literalFor);
226: }
227:
228: /*******************************************************************************************************************
229: *
230: *
231: ******************************************************************************************************************/
232: @Nonnull
233: public static Value literalFor (final short value)
234: {
235: return FACTORY.createLiteral(value);
236: }
237:
238: /*******************************************************************************************************************
239: *
240: *
241: ******************************************************************************************************************/
242: @Nonnull
243: public static Value literalFor (final float value)
244: {
245: return FACTORY.createLiteral(value);
246: }
247:
248: /*******************************************************************************************************************
249: *
250: *
251: ******************************************************************************************************************/
252: @Nonnull
253: public static Optional<Value> literalForFloat (final Optional<Float> optionalFloat)
254: {
255: return optionalFloat.map(RdfUtilities::literalFor);
256: }
257:
258: /*******************************************************************************************************************
259: *
260: *
261: ******************************************************************************************************************/
262: @Nonnull
263: public static Value literalFor (@Nonnull final Instant instant)
264: {
265: return FACTORY.createLiteral(new Date(instant.toEpochMilli()));
266: }
267:
268: /*******************************************************************************************************************
269: *
270: *
271: ******************************************************************************************************************/
272: @Nonnull
273: public static IRI uriFor (@Nonnull final Id id)
274: {
275: return uriFor(id.stringValue());
276: }
277:
278: /*******************************************************************************************************************
279: *
280: *
281: ******************************************************************************************************************/
282: @Nonnull
283: public static IRI uriFor (@Nonnull final String id)
284: {
285: return FACTORY.createIRI(id);
286: }
287:
288: /*******************************************************************************************************************
289: *
290: *
291: ******************************************************************************************************************/
292: @Nonnull
293: public static IRI uriFor (@Nonnull final URL url)
294: {
295: return FACTORY.createIRI(url.toString());
296: }
297:
298: /*******************************************************************************************************************
299: *
300: *
301: ******************************************************************************************************************/
302: @Nonnull
303: public static URL urlFor (@Nonnull final IRI uri)
304: throws MalformedURLException
305: {
306: return new URL(uri.toString());
307: }
308:
309: /*******************************************************************************************************************
310: *
311: *
312: ******************************************************************************************************************/
313: @Nonnull
314: public static String emptyWhenNull (@Nullable final String string)
315: {
316: return (string != null) ? string : "";
317: }
318:
319: /*******************************************************************************************************************
320: *
321: *
322: ******************************************************************************************************************/
323: @Nonnull
324: public static Id createSha1Id (@Nonnull final String string)
325: {
326: try
327: {
328: final MessageDigest digestComputer = MessageDigest.getInstance(ALGORITHM);
329: digestComputer.update(string.getBytes(UTF_8));
330: return Id.of(toHexString(digestComputer.digest()));
331: }
332: catch (NoSuchAlgorithmException e)
333: {
334: throw new RuntimeException(e);
335: }
336: }
337:
338: /*******************************************************************************************************************
339: *
340: *
341: ******************************************************************************************************************/
342: @Nonnull
343: public static Id createSha1IdNew (@Nonnull final String string)
344: {
345: try
346: {
347: final MessageDigest digestComputer = MessageDigest.getInstance(ALGORITHM);
348: digestComputer.update(string.getBytes(UTF_8));
349: return Id.of(Base64.getUrlEncoder().encodeToString(digestComputer.digest()));
350: }
351: catch (NoSuchAlgorithmException e)
352: {
353: throw new RuntimeException(e);
354: }
355: }
356: }