• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanodash / 22618039211

03 Mar 2026 10:05AM UTC coverage: 16.058% (+0.2%) from 15.884%
22618039211

Pull #365

github

web-flow
Merge 1e7e700f0 into a8c4b4a77
Pull Request #365: Refactor of `ResourceWithProfile` and related classes

699 of 5287 branches covered (13.22%)

Branch coverage included in aggregate %.

1721 of 9783 relevant lines covered (17.59%)

2.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.25
src/main/java/com/knowledgepixels/nanodash/Utils.java
1
package com.knowledgepixels.nanodash;
2

3
import com.google.common.hash.Hashing;
4
import com.knowledgepixels.nanodash.domain.User;
5
import net.trustyuri.TrustyUriUtils;
6
import org.apache.commons.codec.Charsets;
7
import org.apache.commons.lang.StringUtils;
8
import org.apache.http.client.utils.URIBuilder;
9
import org.apache.wicket.markup.html.link.ExternalLink;
10
import org.apache.wicket.model.IModel;
11
import org.apache.wicket.request.mapper.parameter.PageParameters;
12
import org.apache.wicket.util.string.StringValue;
13
import org.eclipse.rdf4j.model.IRI;
14
import org.eclipse.rdf4j.model.Literal;
15
import org.eclipse.rdf4j.model.Statement;
16
import org.eclipse.rdf4j.model.ValueFactory;
17
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
18
import org.eclipse.rdf4j.model.util.Literals;
19
import org.eclipse.rdf4j.model.vocabulary.FOAF;
20
import org.eclipse.rdf4j.model.vocabulary.XSD;
21
import org.nanopub.Nanopub;
22
import org.nanopub.NanopubUtils;
23
import org.nanopub.extra.security.KeyDeclaration;
24
import org.nanopub.extra.security.MalformedCryptoElementException;
25
import org.nanopub.extra.security.NanopubSignatureElement;
26
import org.nanopub.extra.security.SignatureUtils;
27
import org.nanopub.extra.server.GetNanopub;
28
import org.nanopub.extra.services.ApiResponseEntry;
29
import org.nanopub.extra.setting.IntroNanopub;
30
import org.nanopub.vocabulary.FIP;
31
import org.nanopub.vocabulary.NPX;
32
import org.owasp.html.HtmlPolicyBuilder;
33
import org.owasp.html.PolicyFactory;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36
import org.wicketstuff.select2.Select2Choice;
37

38
import java.io.Serializable;
39
import java.net.URISyntaxException;
40
import java.net.URLDecoder;
41
import java.net.URLEncoder;
42
import java.nio.charset.StandardCharsets;
43
import java.util.*;
44
import java.util.regex.Pattern;
45

46
import static java.nio.charset.StandardCharsets.UTF_8;
47

48
/**
49
 * Utility class providing various helper methods for handling nanopublications, URIs, and other related functionalities.
50
 */
51
public class Utils {
52

53
    private Utils() {
54
    }  // no instances allowed
55

56
    /**
57
     * ValueFactory instance for creating RDF model objects.
58
     */
59
    public static final ValueFactory vf = SimpleValueFactory.getInstance();
6✔
60
    private static final Logger logger = LoggerFactory.getLogger(Utils.class);
9✔
61
    private static final Pattern LEADING_TAG = Pattern.compile("^\\s*<(p|div|span|img)(\\s|>|/).*", Pattern.CASE_INSENSITIVE);
12✔
62
    private static final String DEFAULT_MAIN_QUERY_URL = "https://query.knowledgepixels.com/";
63
    private static final String DEFAULT_MAIN_REGISTRY_URL = "https://registry.knowledgepixels.com/";
64

65
    /**
66
     * Generates a short name from a given IRI object.
67
     *
68
     * @param uri the IRI object
69
     * @return a short representation of the URI
70
     */
71
    public static String getShortNameFromURI(IRI uri) {
72
        return getShortNameFromURI(uri.stringValue());
12✔
73
    }
74

75
    /**
76
     * Generates a short name from a given URI string.
77
     *
78
     * @param uri the URI string
79
     * @return a short representation of the URI
80
     */
81
    public static String getShortNameFromURI(String uri) {
82
        if (uri.startsWith("https://doi.org/") || uri.startsWith("http://dx.doi.org/")) {
24✔
83
            return uri.replaceFirst("^https?://(dx\\.)?doi.org/", "doi:");
15✔
84
        }
85
        uri = uri.replaceFirst("\\?.*$", "");
15✔
86
        uri = uri.replaceFirst("[/#]$", "");
15✔
87
        uri = uri.replaceFirst("^.*[/#]([^/#]*)[/#]([0-9]+)$", "$1/$2");
15✔
88
        if (uri.contains("#")) {
12✔
89
            uri = uri.replaceFirst("^.*#(.*[^0-9].*)$", "$1");
18✔
90
        } else {
91
            uri = uri.replaceFirst("^.*/([^/]*[^0-9/][^/]*)$", "$1");
15✔
92
        }
93
        uri = uri.replaceFirst("((^|[^A-Za-z0-9\\-_])RA[A-Za-z0-9\\-_]{8})[A-Za-z0-9\\-_]{35}$", "$1");
15✔
94
        uri = uri.replaceFirst("(^|[^A-Za-z0-9\\-_])RA[A-Za-z0-9\\-_]{43}[^A-Za-z0-9\\-_](.+)$", "$2");
15✔
95
        uri = URLDecoder.decode(uri, UTF_8);
12✔
96
        return uri;
6✔
97
    }
98

99
    /**
100
     * Generates a short nanopublication ID from a given nanopublication ID or URI.
101
     *
102
     * @param npId the nanopublication ID or URI
103
     * @return the first 10 characters of the artifact code
104
     */
105
    public static String getShortNanopubId(Object npId) {
106
        return TrustyUriUtils.getArtifactCode(npId.toString()).substring(0, 10);
21✔
107
    }
108

109
    private static Map<String, Nanopub> nanopubs = new HashMap<>();
12✔
110

111
    /**
112
     * Retrieves a Nanopub object based on the given URI or artifact code.
113
     *
114
     * @param uriOrArtifactCode the URI or artifact code of the nanopublication
115
     * @return the Nanopub object, or null if not found
116
     */
117
    public static Nanopub getNanopub(String uriOrArtifactCode) {
118
        String artifactCode = GetNanopub.getArtifactCode(uriOrArtifactCode);
9✔
119
        if (!nanopubs.containsKey(artifactCode)) {
12✔
120
            for (int i = 0; i < 3; i++) {  // Try 3 times to get nanopub
15!
121
                Nanopub np = GetNanopub.get(artifactCode);
9✔
122
                if (np != null) {
6!
123
                    nanopubs.put(artifactCode, np);
15✔
124
                    break;
3✔
125
                }
126
            }
127
        }
128
        return nanopubs.get(artifactCode);
15✔
129
    }
130

131
    /**
132
     * URL-encodes the string representation of the given object using UTF-8 encoding.
133
     *
134
     * @param o the object to be URL-encoded
135
     * @return the URL-encoded string
136
     */
137
    public static String urlEncode(Object o) {
138
        return URLEncoder.encode((o == null ? "" : o.toString()), Charsets.UTF_8);
27✔
139
    }
140

141
    /**
142
     * URL-decodes the string representation of the given object using UTF-8 encoding.
143
     *
144
     * @param o the object to be URL-decoded
145
     * @return the URL-decoded string
146
     */
147
    public static String urlDecode(Object o) {
148
        return URLDecoder.decode((o == null ? "" : o.toString()), Charsets.UTF_8);
27✔
149
    }
150

151
    /**
152
     * Generates a URL with the given base and appends the provided PageParameters as query parameters.
153
     *
154
     * @param base       the base URL
155
     * @param parameters the PageParameters to append
156
     * @return the complete URL with parameters
157
     */
158
    public static String getUrlWithParameters(String base, PageParameters parameters) {
159
        try {
160
            URIBuilder u = new URIBuilder(base);
15✔
161
            for (String key : parameters.getNamedKeys()) {
33✔
162
                for (StringValue value : parameters.getValues(key)) {
36✔
163
                    if (!value.isNull()) u.addParameter(key, value.toString());
27!
164
                }
3✔
165
            }
3✔
166
            return u.build().toString();
12✔
167
        } catch (URISyntaxException ex) {
3✔
168
            logger.error("Could not build URL with parameters: {} {}", base, parameters, ex);
51✔
169
            return "/";
6✔
170
        }
171
    }
172

173
    /**
174
     * Generates a short name for a public key or public key hash.
175
     *
176
     * @param pubkeyOrPubkeyhash the public key (64 characters) or public key hash (40 characters)
177
     * @return a short representation of the public key or public key hash
178
     */
179
    public static String getShortPubkeyName(String pubkeyOrPubkeyhash) {
180
        if (pubkeyOrPubkeyhash.length() == 64) {
12!
181
            return pubkeyOrPubkeyhash.replaceFirst("^(.{8}).*$", "$1");
×
182
        } else {
183
            return pubkeyOrPubkeyhash.replaceFirst("^(.).{39}(.{5}).*$", "$1..$2..");
15✔
184
        }
185
    }
186

187
    /**
188
     * Generates a short label for a public key or public key hash, including its status (local or approved).
189
     *
190
     * @param pubkeyOrPubkeyhash the public key (64 characters) or public key hash (40 characters)
191
     * @param user               the IRI of the user associated with the public key
192
     * @return a short label indicating the public key and its status
193
     */
194
    public static String getShortPubkeyhashLabel(String pubkeyOrPubkeyhash, IRI user) {
195
        String s = getShortPubkeyName(pubkeyOrPubkeyhash);
×
196
        NanodashSession session = NanodashSession.get();
×
197
        List<String> l = new ArrayList<>();
×
198
        if (pubkeyOrPubkeyhash.equals(session.getPubkeyString()) || pubkeyOrPubkeyhash.equals(session.getPubkeyhash()))
×
199
            l.add("local");
×
200
        // TODO: Make this more efficient:
201
        String hashed = Utils.createSha256HexHash(pubkeyOrPubkeyhash);
×
202
        if (User.getPubkeyhashes(user, true).contains(pubkeyOrPubkeyhash) || User.getPubkeyhashes(user, true).contains(hashed))
×
203
            l.add("approved");
×
204
        if (!l.isEmpty()) s += " (" + String.join("/", l) + ")";
×
205
        return s;
×
206
    }
207

208
    /**
209
     * Retrieves the name of the public key location based on the public key.
210
     *
211
     * @param pubkeyhash the public key string
212
     * @return the name of the public key location
213
     */
214
    public static String getPubkeyLocationName(String pubkeyhash) {
215
        return getPubkeyLocationName(pubkeyhash, getShortPubkeyName(pubkeyhash));
×
216
    }
217

218
    /**
219
     * Retrieves the name of the public key location, or returns a fallback name if not found.
220
     * If the key location is localhost, it returns "localhost".
221
     *
222
     * @param pubkeyhash the public key string
223
     * @param fallback   the fallback name to return if the key location is not found
224
     * @return the name of the public key location or the fallback name
225
     */
226
    public static String getPubkeyLocationName(String pubkeyhash, String fallback) {
227
        IRI keyLocation = User.getUserData().getKeyLocationForPubkeyhash(pubkeyhash);
×
228
        if (keyLocation == null) return fallback;
×
229
        if (keyLocation.stringValue().equals("http://localhost:37373/")) return "localhost";
×
230
        return keyLocation.stringValue().replaceFirst("https?://(nanobench\\.)?(nanodash\\.)?(.*[^/])/?$", "$3");
×
231
    }
232

233
    /**
234
     * Generates a short label for a public key location, including its status (local or approved).
235
     *
236
     * @param pubkeyhash the public key string
237
     * @param user       the IRI of the user associated with the public key
238
     * @return a short label indicating the public key location and its status
239
     */
240
    public static String getShortPubkeyLocationLabel(String pubkeyhash, IRI user) {
241
        String s = getPubkeyLocationName(pubkeyhash);
×
242
        NanodashSession session = NanodashSession.get();
×
243
        List<String> l = new ArrayList<>();
×
244
        if (pubkeyhash.equals(session.getPubkeyhash())) l.add("local");
×
245
        // TODO: Make this more efficient:
246
        if (User.getPubkeyhashes(user, true).contains(pubkeyhash)) l.add("approved");
×
247
        if (!l.isEmpty()) s += " (" + String.join("/", l) + ")";
×
248
        return s;
×
249
    }
250

251
    /**
252
     * Checks if a given public key has a Nanodash location.
253
     * A Nanodash location is identified by specific keywords in the key location.
254
     *
255
     * @param pubkeyhash the public key to check
256
     * @return true if the public key has a Nanodash location, false otherwise
257
     */
258
    public static boolean hasNanodashLocation(String pubkeyhash) {
259
        IRI keyLocation = User.getUserData().getKeyLocationForPubkeyhash(pubkeyhash);
×
260
        if (keyLocation == null) return true; // potentially a Nanodash location
×
261
        if (keyLocation.stringValue().contains("nanodash")) return true;
×
262
        if (keyLocation.stringValue().contains("nanobench")) return true;
×
263
        if (keyLocation.stringValue().contains(":37373")) return true;
×
264
        return false;
×
265
    }
266

267
    /**
268
     * Retrieves the short ORCID ID from an IRI object.
269
     *
270
     * @param orcidIri the IRI object representing the ORCID ID
271
     * @return the short ORCID ID as a string
272
     */
273
    public static String getShortOrcidId(IRI orcidIri) {
274
        return orcidIri.stringValue().replaceFirst("^https://orcid.org/", "");
18✔
275
    }
276

277
    /**
278
     * Retrieves the URI postfix from a given URI object.
279
     *
280
     * @param uri the URI object from which to extract the postfix
281
     * @return the URI postfix as a string
282
     */
283
    public static String getUriPostfix(Object uri) {
284
        String s = uri.toString();
9✔
285
        if (s.contains("#")) return s.replaceFirst("^.*#(.*)$", "$1");
27✔
286
        return s.replaceFirst("^.*/(.*)$", "$1");
15✔
287
    }
288

289
    /**
290
     * Retrieves the URI prefix from a given URI object.
291
     *
292
     * @param uri the URI object from which to extract the prefix
293
     * @return the URI prefix as a string
294
     */
295
    public static String getUriPrefix(Object uri) {
296
        String s = uri.toString();
9✔
297
        if (s.contains("#")) return s.replaceFirst("^(.*#).*$", "$1");
27✔
298
        return s.replaceFirst("^(.*/).*$", "$1");
15✔
299
    }
300

301
    /**
302
     * Checks if a given string is a valid URI postfix.
303
     * A valid URI postfix does not contain a colon (":").
304
     *
305
     * @param s the string to check
306
     * @return true if the string is a valid URI postfix, false otherwise
307
     */
308
    public static boolean isUriPostfix(String s) {
309
        return !s.contains(":");
24✔
310
    }
311

312
    /**
313
     * Retrieves the location of a given IntroNanopub.
314
     *
315
     * @param inp the IntroNanopub from which to extract the location
316
     * @return the IRI location of the nanopublication, or null if not found
317
     */
318
    public static IRI getLocation(IntroNanopub inp) {
319
        NanopubSignatureElement el = getNanopubSignatureElement(inp);
×
320
        for (KeyDeclaration kd : inp.getKeyDeclarations()) {
×
321
            if (el.getPublicKeyString().equals(kd.getPublicKeyString())) {
×
322
                return kd.getKeyLocation();
×
323
            }
324
        }
×
325
        return null;
×
326
    }
327

328
    /**
329
     * Retrieves the NanopubSignatureElement from a given IntroNanopub.
330
     *
331
     * @param inp the IntroNanopub from which to extract the signature element
332
     * @return the NanopubSignatureElement associated with the nanopublication
333
     */
334
    public static NanopubSignatureElement getNanopubSignatureElement(IntroNanopub inp) {
335
        try {
336
            return SignatureUtils.getSignatureElement(inp.getNanopub());
×
337
        } catch (MalformedCryptoElementException ex) {
×
338
            throw new RuntimeException(ex);
×
339
        }
340
    }
341

342
    /**
343
     * Retrieves a Nanopub object from a given URI if it is a potential Trusty URI.
344
     *
345
     * @param uri the URI to check and retrieve the Nanopub from
346
     * @return the Nanopub object if found, or null if not a known nanopublication
347
     */
348
    public static Nanopub getAsNanopub(String uri) {
349
        if (uri == null) return null;
6!
350
        if (TrustyUriUtils.isPotentialTrustyUri(uri)) {
9!
351
            try {
352
                return Utils.getNanopub(uri);
9✔
353
            } catch (Exception ex) {
×
354
                logger.error("The given URI is not a known nanopublication: {}", uri, ex);
×
355
            }
356
        }
357
        return null;
×
358
    }
359

360
    private static final PolicyFactory htmlSanitizePolicy = new HtmlPolicyBuilder()
9✔
361
            .allowCommonBlockElements()
3✔
362
            .allowCommonInlineFormattingElements()
45✔
363
            .allowUrlProtocols("https", "http", "mailto")
21✔
364
            .allowElements("a")
21✔
365
            .allowAttributes("href").onElements("a")
42✔
366
            .allowElements("img")
21✔
367
            .allowAttributes("src").onElements("img")
24✔
368
            .requireRelNofollowOnLinks()
3✔
369
            .toFactory();
6✔
370

371
    /**
372
     * Sanitizes raw HTML input to ensure safe rendering.
373
     *
374
     * @param rawHtml the raw HTML input to sanitize
375
     * @return sanitized HTML string
376
     */
377
    public static String sanitizeHtml(String rawHtml) {
378
        return htmlSanitizePolicy.sanitize(rawHtml);
12✔
379
    }
380

381
    /**
382
     * Checks if a given string is likely to be HTML content.
383
     *
384
     * @param value the string to check
385
     * @return true if the given string is HTML content, false otherwise
386
     */
387
    public static boolean looksLikeHtml(String value) {
388
        return LEADING_TAG.matcher(value).find();
×
389
    }
390

391
    /**
392
     * Converts PageParameters to a URL-encoded string representation.
393
     *
394
     * @param params the PageParameters to convert
395
     * @return a string representation of the parameters in URL-encoded format
396
     */
397
    public static String getPageParametersAsString(PageParameters params) {
398
        String s = "";
6✔
399
        for (String n : params.getNamedKeys()) {
33✔
400
            if (!s.isEmpty()) s += "&";
18✔
401
            s += n + "=" + URLEncoder.encode(params.get(n).toString(), Charsets.UTF_8);
30✔
402
        }
3✔
403
        return s;
6✔
404
    }
405

406
    /**
407
     * Sets a minimal escape markup function for a Select2Choice component.
408
     * This function replaces certain characters and formats the display of choices.
409
     *
410
     * @param selectItem the Select2Choice component to set the escape markup for
411
     */
412
    public static void setSelect2ChoiceMinimalEscapeMarkup(Select2Choice<?> selectItem) {
413
        selectItem.getSettings().setEscapeMarkup("function(markup) {" +
×
414
                                                 "return markup" +
415
                                                 ".replaceAll('<','&lt;').replaceAll('>', '&gt;')" +
416
                                                 ".replace(/^(.*?) - /, '<span class=\"term\">$1</span><br>')" +
417
                                                 ".replace(/\\((https?:[\\S]+)\\)$/, '<br><code>$1</code>')" +
418
                                                 ".replace(/^([^<].*)$/, '<span class=\"term\">$1</span>')" +
419
                                                 ";}"
420
        );
421
    }
×
422

423
    /**
424
     * Checks if a nanopublication is of a specific class.
425
     *
426
     * @param np       the nanopublication to check
427
     * @param classIri the IRI of the class to check against
428
     * @return true if the nanopublication is of the specified class, false otherwise
429
     */
430
    public static boolean isNanopubOfClass(Nanopub np, IRI classIri) {
431
        return NanopubUtils.getTypes(np).contains(classIri);
15✔
432
    }
433

434
    /**
435
     * Checks if a nanopublication uses a specific predicate in its assertion.
436
     *
437
     * @param np           the nanopublication to check
438
     * @param predicateIri the IRI of the predicate to look for
439
     * @return true if the predicate is used in the assertion, false otherwise
440
     */
441
    public static boolean usesPredicateInAssertion(Nanopub np, IRI predicateIri) {
442
        for (Statement st : np.getAssertion()) {
33✔
443
            if (predicateIri.equals(st.getPredicate())) {
15✔
444
                return true;
6✔
445
            }
446
        }
3✔
447
        return false;
6✔
448
    }
449

450
    /**
451
     * Retrieves a map of FOAF names from the nanopublication's pubinfo.
452
     *
453
     * @param np the nanopublication from which to extract FOAF names
454
     * @return a map where keys are subjects and values are FOAF names
455
     */
456
    public static Map<String, String> getFoafNameMap(Nanopub np) {
457
        Map<String, String> foafNameMap = new HashMap<>();
12✔
458
        for (Statement st : np.getPubinfo()) {
33✔
459
            if (st.getPredicate().equals(FOAF.NAME) && st.getObject() instanceof Literal objL) {
42✔
460
                foafNameMap.put(st.getSubject().stringValue(), objL.stringValue());
24✔
461
            }
462
        }
3✔
463
        return foafNameMap;
6✔
464
    }
465

466
    /**
467
     * Creates an SHA-256 hash of the string representation of an object and returns it as a hexadecimal string.
468
     *
469
     * @param obj the object to hash
470
     * @return the SHA-256 hash of the object's string representation in hexadecimal format
471
     */
472
    public static String createSha256HexHash(Object obj) {
473
        return Hashing.sha256().hashString(obj.toString(), StandardCharsets.UTF_8).toString();
21✔
474
    }
475

476
    /**
477
     * Gets the types of a nanopublication.
478
     *
479
     * @param np the nanopublication from which to extract types
480
     * @return a list of IRI types associated with the nanopublication
481
     */
482
    public static List<IRI> getTypes(Nanopub np) {
483
        List<IRI> l = new ArrayList<>();
12✔
484
        for (IRI t : NanopubUtils.getTypes(np)) {
33✔
485
            if (t.equals(FIP.AVAILABLE_FAIR_ENABLING_RESOURCE)) continue;
15✔
486
            if (t.equals(FIP.FAIR_ENABLING_RESOURCE_TO_BE_DEVELOPED))
12✔
487
                continue;
3✔
488
            if (t.equals(FIP.AVAILABLE_FAIR_SUPPORTING_RESOURCE)) continue;
12!
489
            if (t.equals(FIP.FAIR_SUPPORTING_RESOURCE_TO_BE_DEVELOPED))
12!
490
                continue;
×
491
            l.add(t);
12✔
492
        }
3✔
493
        return l;
6✔
494
    }
495

496
    /**
497
     * Gets a label for a type IRI.
498
     *
499
     * @param typeIri the IRI of the type
500
     * @return a label for the type, potentially truncated
501
     */
502
    public static String getTypeLabel(IRI typeIri) {
503
        if (typeIri.equals(FIP.FAIR_ENABLING_RESOURCE)) return "FER";
18✔
504
        if (typeIri.equals(FIP.FAIR_SUPPORTING_RESOURCE)) return "FSR";
18✔
505
        if (typeIri.equals(FIP.FAIR_IMPLEMENTATION_PROFILE)) return "FIP";
18✔
506
        if (typeIri.equals(NPX.DECLARED_BY)) return "user intro";
18✔
507
        String l = typeIri.stringValue();
9✔
508
        l = l.replaceFirst("^.*[/#]([^/#]+)[/#]?$", "$1");
15✔
509
        l = l.replaceFirst("^(.+)Nanopub$", "$1");
15✔
510
        if (l.length() > 25) l = l.substring(0, 20) + "...";
30✔
511
        return l;
6✔
512
    }
513

514
    /**
515
     * Gets a label for a URI.
516
     *
517
     * @param uri the URI to get the label from
518
     * @return a label for the URI, potentially truncated
519
     */
520
    public static String getUriLabel(String uri) {
521
        if (uri == null) return "";
12✔
522
        String uriLabel = uri;
6✔
523
        if (uriLabel.matches(".*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{43}([^A-Za-z0-9-_].*)?")) {
12✔
524
            String newUriLabel = uriLabel.replaceFirst("(.*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{8})[A-Za-z0-9-_]{35}([^A-Za-z0-9-_].*)?", "$1...$2");
15✔
525
            if (newUriLabel.length() <= 70) return newUriLabel;
18!
526
        }
527
        if (uriLabel.length() > 70) return uri.substring(0, 30) + "..." + uri.substring(uri.length() - 30);
48✔
528
        return uriLabel;
6✔
529
    }
530

531
    /**
532
     * Gets an ExternalLink with a URI label.
533
     *
534
     * @param markupId the markup ID for the link
535
     * @param uri      the URI to link to
536
     * @return an ExternalLink with the URI label
537
     */
538
    public static ExternalLink getUriLink(String markupId, String uri) {
539
        return new ExternalLink(markupId, (Utils.isLocalURI(uri) ? "" : uri), getUriLabel(uri));
39✔
540
    }
541

542
    /**
543
     * Gets an ExternalLink with a model for the URI label.
544
     *
545
     * @param markupId the markup ID for the link
546
     * @param model    the model containing the URI
547
     * @return an ExternalLink with the URI label
548
     */
549
    public static ExternalLink getUriLink(String markupId, IModel<String> model) {
550
        return new ExternalLink(markupId, model, new UriLabelModel(model));
×
551
    }
552

553
    private static class UriLabelModel implements IModel<String> {
554

555
        private IModel<String> uriModel;
556

557
        public UriLabelModel(IModel<String> uriModel) {
×
558
            this.uriModel = uriModel;
×
559
        }
×
560

561
        @Override
562
        public String getObject() {
563
            return getUriLabel(uriModel.getObject());
×
564
        }
565

566
    }
567

568
    /**
569
     * Creates a sublist from a list based on the specified indices.
570
     *
571
     * @param list      the list from which to create the sublist
572
     * @param fromIndex the starting index (inclusive) for the sublist
573
     * @param toIndex   the ending index (exclusive) for the sublist
574
     * @param <E>       the type of elements in the list
575
     * @return an ArrayList containing the elements from the specified range
576
     */
577
    public static <E> ArrayList<E> subList(List<E> list, long fromIndex, long toIndex) {
578
        // So the resulting list is serializable:
579
        return new ArrayList<E>(list.subList((int) fromIndex, (int) toIndex));
×
580
    }
581

582
    /**
583
     * Creates a sublist from an array based on the specified indices.
584
     *
585
     * @param array     the array from which to create the sublist
586
     * @param fromIndex the starting index (inclusive) for the sublist
587
     * @param toIndex   the ending index (exclusive) for the sublist
588
     * @param <E>       the type of elements in the array
589
     * @return an ArrayList containing the elements from the specified range
590
     */
591
    public static <E> ArrayList<E> subList(E[] array, long fromIndex, long toIndex) {
592
        return subList(Arrays.asList(array), fromIndex, toIndex);
×
593
    }
594

595
    /**
596
     * Comparator for sorting ApiResponseEntry objects based on a specified field.
597
     */
598
    // TODO Move this to ApiResponseEntry class?
599
    public static class ApiResponseEntrySorter implements Comparator<ApiResponseEntry>, Serializable {
600

601
        private String field;
602
        private boolean descending;
603

604
        /**
605
         * Constructor for ApiResponseEntrySorter.
606
         *
607
         * @param field      the field to sort by
608
         * @param descending if true, sorts in descending order; if false, sorts in ascending order
609
         */
610
        public ApiResponseEntrySorter(String field, boolean descending) {
×
611
            this.field = field;
×
612
            this.descending = descending;
×
613
        }
×
614

615
        /**
616
         * Compares two ApiResponseEntry objects based on the specified field.
617
         *
618
         * @param o1 the first object to be compared.
619
         * @param o2 the second object to be compared.
620
         * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
621
         */
622
        @Override
623
        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
624
            if (descending) {
×
625
                return o2.get(field).compareTo(o1.get(field));
×
626
            } else {
627
                return o1.get(field).compareTo(o2.get(field));
×
628
            }
629
        }
630

631
    }
632

633
    /**
634
     * MIME type for TriG RDF format.
635
     */
636
    public static final String TYPE_TRIG = "application/trig";
637

638
    /**
639
     * MIME type for Jelly RDF format.
640
     */
641
    public static final String TYPE_JELLY = "application/x-jelly-rdf";
642

643
    /**
644
     * MIME type for JSON-LD format.
645
     */
646
    public static final String TYPE_JSONLD = "application/ld+json";
647

648
    /**
649
     * MIME type for N-Quads format.
650
     */
651
    public static final String TYPE_NQUADS = "application/n-quads";
652

653
    /**
654
     * MIME type for Trix format.
655
     */
656
    public static final String TYPE_TRIX = "application/trix";
657

658
    /**
659
     * MIME type for HTML format.
660
     */
661
    public static final String TYPE_HTML = "text/html";
662

663
    /**
664
     * Comma-separated list of supported MIME types for nanopublications.
665
     */
666
    public static final String SUPPORTED_TYPES =
667
            TYPE_TRIG + "," +
668
            TYPE_JELLY + "," +
669
            TYPE_JSONLD + "," +
670
            TYPE_NQUADS + "," +
671
            TYPE_TRIX + "," +
672
            TYPE_HTML;
673

674
    /**
675
     * List of supported MIME types for nanopublications.
676
     */
677
    public static final List<String> SUPPORTED_TYPES_LIST = Arrays.asList(StringUtils.split(SUPPORTED_TYPES, ','));
18✔
678

679
    /**
680
     * Returns the URL of the default Nanopub Registry as configured by the given instance.
681
     *
682
     * @return Nanopub Registry URL
683
     */
684
    public static String getMainRegistryUrl() {
685
        String envValue = System.getenv("NANODASH_MAIN_REGISTRY");
9✔
686
        if (envValue != null) {
6!
687
            logger.info("Found environment variable NANODASH_MAIN_REGISTRY with value: {}", envValue);
×
688
            return envValue;
×
689
        } else {
690
            logger.info("Environment variable NANODASH_MAIN_REGISTRY not set, using default: {}", DEFAULT_MAIN_REGISTRY_URL);
12✔
691
            return DEFAULT_MAIN_REGISTRY_URL;
6✔
692
        }
693
    }
694

695
    /**
696
     * Returns the URL of the default Nanopub Query as configured by the given instance.
697
     *
698
     * @return Nanopub Query URL
699
     */
700
    public static String getMainQueryUrl() {
701
        String envValue = System.getenv("NANODASH_MAIN_QUERY");
×
702
        if (envValue != null) {
×
703
            logger.info("Found environment variable NANODASH_MAIN_QUERY with value: {}", envValue);
×
704
            return envValue;
×
705
        } else {
706
            logger.info("Environment variable NANODASH_MAIN_QUERY not set, using default: {}", DEFAULT_MAIN_QUERY_URL);
×
707
            return DEFAULT_MAIN_QUERY_URL;
×
708
        }
709
    }
710

711
    private static final String PLAIN_LITERAL_PATTERN = "^\"(([^\\\\\\\"]|\\\\\\\\|\\\\\")*)\"";
712
    private static final String LANGTAG_LITERAL_PATTERN = "^\"(([^\\\\\\\"]|\\\\\\\\|\\\\\")*)\"@([0-9a-zA-Z-]{2,})$";
713
    private static final String DATATYPE_LITERAL_PATTERN = "^\"(([^\\\\\\\"]|\\\\\\\\|\\\\\")*)\"\\^\\^<([^ ><\"^]+)>";
714

715
    /**
716
     * Checks whether string is valid literal serialization.
717
     *
718
     * @param literalString the literal string
719
     * @return true if valid
720
     */
721
    public static boolean isValidLiteralSerialization(String literalString) {
722
        if (literalString.matches(PLAIN_LITERAL_PATTERN)) {
12✔
723
            return true;
6✔
724
        } else if (literalString.matches(LANGTAG_LITERAL_PATTERN)) {
12✔
725
            return true;
6✔
726
        } else if (literalString.matches(DATATYPE_LITERAL_PATTERN)) {
12✔
727
            return true;
6✔
728
        }
729
        return false;
6✔
730
    }
731

732
    /**
733
     * Returns a serialized version of the literal.
734
     *
735
     * @param literal the literal
736
     * @return the String serialization of the literal
737
     */
738
    public static String getSerializedLiteral(Literal literal) {
739
        if (literal.getLanguage().isPresent()) {
12✔
740
            return "\"" + getEscapedLiteralString(literal.stringValue()) + "\"@" + Literals.normalizeLanguageTag(literal.getLanguage().get());
30✔
741
        } else if (literal.getDatatype().equals(XSD.STRING)) {
15✔
742
            return "\"" + getEscapedLiteralString(literal.stringValue()) + "\"";
15✔
743
        } else {
744
            return "\"" + getEscapedLiteralString(literal.stringValue()) + "\"^^<" + literal.getDatatype() + ">";
24✔
745
        }
746
    }
747

748
    /**
749
     * Parses a serialized literal into a Literal object.
750
     *
751
     * @param serializedLiteral The serialized String of the literal
752
     * @return The parse Literal object
753
     */
754
    public static Literal getParsedLiteral(String serializedLiteral) {
755
        if (serializedLiteral.matches(PLAIN_LITERAL_PATTERN)) {
12✔
756
            return vf.createLiteral(getUnescapedLiteralString(serializedLiteral.replaceFirst(PLAIN_LITERAL_PATTERN, "$1")));
24✔
757
        } else if (serializedLiteral.matches(LANGTAG_LITERAL_PATTERN)) {
12✔
758
            String langtag = serializedLiteral.replaceFirst(LANGTAG_LITERAL_PATTERN, "$3");
15✔
759
            return vf.createLiteral(getUnescapedLiteralString(serializedLiteral.replaceFirst(LANGTAG_LITERAL_PATTERN, "$1")), langtag);
27✔
760
        } else if (serializedLiteral.matches(DATATYPE_LITERAL_PATTERN)) {
12✔
761
            IRI datatype = vf.createIRI(serializedLiteral.replaceFirst(DATATYPE_LITERAL_PATTERN, "$3"));
21✔
762
            return vf.createLiteral(getUnescapedLiteralString(serializedLiteral.replaceFirst(DATATYPE_LITERAL_PATTERN, "$1")), datatype);
27✔
763
        }
764
        throw new IllegalArgumentException("Not a valid literal serialization: " + serializedLiteral);
18✔
765
    }
766

767
    /**
768
     * Escapes quotes (") and slashes (/) of a literal string.
769
     *
770
     * @param unescapedString un-escaped string
771
     * @return escaped string
772
     */
773
    public static String getEscapedLiteralString(String unescapedString) {
774
        return unescapedString.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\"");
24✔
775
    }
776

777
    /**
778
     * Un-escapes quotes (") and slashes (/) of a literal string.
779
     *
780
     * @param escapedString escaped string
781
     * @return un-escaped string
782
     */
783
    public static String getUnescapedLiteralString(String escapedString) {
784
        return escapedString.replaceAll("\\\\(\\\\|\\\")", "$1");
15✔
785
    }
786

787
    /**
788
     * Checks if a given IRI is a local URI.
789
     *
790
     * @param uri the IRI to check
791
     * @return true if the IRI is a local URI, false otherwise
792
     */
793
    public static boolean isLocalURI(IRI uri) {
794
        return uri != null && isLocalURI(uri.stringValue());
30✔
795
    }
796

797
    /**
798
     * Checks if a given string is a local URI.
799
     *
800
     * @param uriAsString the string to check
801
     * @return true if the string is a local URI, false otherwise
802
     */
803
    public static boolean isLocalURI(String uriAsString) {
804
        return !uriAsString.isBlank() && uriAsString.startsWith(LocalUri.PREFIX);
33✔
805
    }
806

807
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc