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

knowledgepixels / nanodash / 28444877373

30 Jun 2026 12:38PM UTC coverage: 28.035% (-0.01%) from 28.046%
28444877373

push

github

web-flow
Merge pull request #522 from knowledgepixels/feat/truncate-entity-link-labels

feat: truncate over-long entity labels in links, buttons, and breadcrumbs

1723 of 7007 branches covered (24.59%)

Branch coverage included in aggregate %.

3607 of 12005 relevant lines covered (30.05%)

4.45 hits per line

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

61.55
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.apache.wicket.util.string.Strings;
14
import org.eclipse.rdf4j.model.IRI;
15
import org.eclipse.rdf4j.model.Literal;
16
import org.eclipse.rdf4j.model.Statement;
17
import org.eclipse.rdf4j.model.ValueFactory;
18
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
19
import org.eclipse.rdf4j.model.util.Literals;
20
import org.eclipse.rdf4j.model.vocabulary.FOAF;
21
import org.eclipse.rdf4j.model.vocabulary.XSD;
22
import org.nanopub.Nanopub;
23
import org.nanopub.NanopubUtils;
24
import org.nanopub.extra.security.KeyDeclaration;
25
import org.nanopub.extra.security.MalformedCryptoElementException;
26
import org.nanopub.extra.security.NanopubSignatureElement;
27
import org.nanopub.extra.security.SignatureUtils;
28
import org.nanopub.extra.server.GetNanopub;
29
import org.nanopub.extra.server.NanopubServerUtils;
30
import org.nanopub.extra.services.ApiResponseEntry;
31
import org.nanopub.extra.services.NotEnoughAPIInstancesException;
32
import org.nanopub.extra.services.QueryCall;
33
import org.nanopub.extra.setting.IntroNanopub;
34
import org.nanopub.vocabulary.FIP;
35
import org.nanopub.vocabulary.NPX;
36
import org.owasp.html.HtmlPolicyBuilder;
37
import org.owasp.html.PolicyFactory;
38
import org.slf4j.Logger;
39
import org.slf4j.LoggerFactory;
40
import org.wicketstuff.select2.Select2Choice;
41

42
import java.io.Serializable;
43
import java.net.URISyntaxException;
44
import java.net.URLDecoder;
45
import java.net.URLEncoder;
46
import java.nio.charset.StandardCharsets;
47
import java.util.*;
48
import java.util.regex.Pattern;
49

50
import static java.nio.charset.StandardCharsets.UTF_8;
51

52
/**
53
 * Utility class providing various helper methods for handling nanopublications, URIs, and other related functionalities.
54
 */
55
public class Utils {
56

57
    private Utils() {
58
    }  // no instances allowed
59

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

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

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

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

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

115
    /**
116
     * Adds a nanopublication to the local cache so it can be retrieved immediately
117
     * without needing to fetch it from the registry.
118
     *
119
     * @param np the nanopublication to cache
120
     */
121
    public static void cacheNanopub(Nanopub np) {
122
        String artifactCode = GetNanopub.getArtifactCode(np.getUri().stringValue()).toString();
×
123
        nanopubs.put(artifactCode, np);
×
124
    }
×
125

126
    /**
127
     * Retrieves a Nanopub object based on the given URI or artifact code.
128
     *
129
     * @param uriOrArtifactCode the URI or artifact code of the nanopublication
130
     * @return the Nanopub object, or null if not found
131
     */
132
    public static Nanopub getNanopub(String uriOrArtifactCode) {
133
        String artifactCode = GetNanopub.getArtifactCode(uriOrArtifactCode).toString();
12✔
134
        if (!nanopubs.containsKey(artifactCode)) {
12✔
135
            for (int i = 0; i < 3; i++) {  // Try 3 times to get nanopub
15!
136
                Nanopub np = GetNanopub.get(artifactCode);
9✔
137
                if (np != null) {
6!
138
                    nanopubs.put(artifactCode, np);
15✔
139
                    break;
3✔
140
                }
141
            }
142
        }
143
        return nanopubs.get(artifactCode);
15✔
144
    }
145

146
    /**
147
     * URL-encodes the string representation of the given object using UTF-8 encoding.
148
     *
149
     * @param o the object to be URL-encoded
150
     * @return the URL-encoded string
151
     */
152
    public static String urlEncode(Object o) {
153
        return URLEncoder.encode((o == null ? "" : o.toString()), Charsets.UTF_8);
27✔
154
    }
155

156
    public static String truncateLabel(String label) {
157
        if (label != null && label.length() > 120) {
×
158
            return label.substring(0, 100) + "...";
×
159
        }
160
        return label;
×
161
    }
162

163
    /**
164
     * Truncates an over-long entity label for display in a link or breadcrumb:
165
     * labels longer than 60 characters are cut to 47 characters with an ellipsis
166
     * ("...") appended, so a single long label (e.g. a full IRI tail) never blows
167
     * up the surrounding UI. Shorter labels are returned unchanged. The full label
168
     * stays available on the entity's own page, which shows it as the title.
169
     *
170
     * @param label the label to truncate, or null
171
     * @return the truncated label, or the original if 60 characters or shorter
172
     */
173
    public static String truncateLinkLabel(String label) {
174
        if (label == null || label.length() <= 60) return label;
24!
175
        return label.substring(0, 47).stripTrailing() + "...";
×
176
    }
177

178
    /**
179
     * Builds the HTML body for a menu entry whose label may begin with a leading
180
     * symbol/emoji used as the entry's icon. If {@code label} starts with a token
181
     * of symbol/emoji characters (no letters or digits) followed by whitespace,
182
     * that token is wrapped in the {@code .actionmenu-icon} slot and the remaining
183
     * text follows it (both escaped). Returns {@code null} when there is no such
184
     * leading icon, so callers can fall back to the plain (escaped) label.
185
     *
186
     * @param label the menu entry label
187
     * @return the icon+text HTML body to render with escaping disabled, or null
188
     */
189
    public static String menuEntryIconBodyHtml(String label) {
190
        if (label == null) return null;
×
191
        int sp = -1;
×
192
        for (int i = 0; i < label.length(); i++) {
×
193
            if (Character.isWhitespace(label.charAt(i))) {
×
194
                sp = i;
×
195
                break;
×
196
            }
197
        }
198
        if (sp <= 0) return null;
×
199
        String icon = label.substring(0, sp);
×
200
        String rest = label.substring(sp).replaceFirst("^\\s+", "");
×
201
        if (rest.isEmpty()) return null;
×
202
        // Only a pure symbol/emoji token (no letters or digits) counts as an icon.
203
        if (icon.codePoints().anyMatch(Character::isLetterOrDigit)) return null;
×
204
        return "<span class=\"actionmenu-icon\">" + Strings.escapeMarkup(icon) + "</span>"
×
205
                + Strings.escapeMarkup(rest);
×
206
    }
207

208
    /**
209
     * URL-decodes the string representation of the given object using UTF-8 encoding.
210
     *
211
     * @param o the object to be URL-decoded
212
     * @return the URL-decoded string
213
     */
214
    public static String urlDecode(Object o) {
215
        return URLDecoder.decode((o == null ? "" : o.toString()), Charsets.UTF_8);
27✔
216
    }
217

218
    /**
219
     * Generates a URL with the given base and appends the provided PageParameters as query parameters.
220
     *
221
     * @param base       the base URL
222
     * @param parameters the PageParameters to append
223
     * @return the complete URL with parameters
224
     */
225
    public static String getUrlWithParameters(String base, PageParameters parameters) {
226
        try {
227
            URIBuilder u = new URIBuilder(base);
15✔
228
            for (String key : parameters.getNamedKeys()) {
33✔
229
                for (StringValue value : parameters.getValues(key)) {
36✔
230
                    if (!value.isNull()) u.addParameter(key, value.toString());
27!
231
                }
3✔
232
            }
3✔
233
            return u.build().toString();
12✔
234
        } catch (URISyntaxException ex) {
3✔
235
            logger.error("Could not build URL with parameters: {} {}", base, parameters, ex);
51✔
236
            return "/";
6✔
237
        }
238
    }
239

240
    /**
241
     * Generates a short name for a public key or public key hash.
242
     *
243
     * @param pubkeyOrPubkeyhash the public key (64 characters) or public key hash (40 characters)
244
     * @return a short representation of the public key or public key hash
245
     */
246
    public static String getShortPubkeyName(String pubkeyOrPubkeyhash) {
247
        if (pubkeyOrPubkeyhash.length() == 64) {
12!
248
            return pubkeyOrPubkeyhash.replaceFirst("^(.{8}).*$", "$1");
×
249
        } else {
250
            return pubkeyOrPubkeyhash.replaceFirst("^(.).{39}(.{5}).*$", "$1..$2..");
15✔
251
        }
252
    }
253

254
    /**
255
     * Generates a short label for a public key or public key hash, including its status (local or approved).
256
     *
257
     * @param pubkeyOrPubkeyhash the public key (64 characters) or public key hash (40 characters)
258
     * @param user               the IRI of the user associated with the public key
259
     * @return a short label indicating the public key and its status
260
     */
261
    public static String getShortPubkeyhashLabel(String pubkeyOrPubkeyhash, IRI user) {
262
        String s = getShortPubkeyName(pubkeyOrPubkeyhash);
×
263
        NanodashSession session = NanodashSession.get();
×
264
        List<String> l = new ArrayList<>();
×
265
        if (pubkeyOrPubkeyhash.equals(session.getPubkeyString()) || pubkeyOrPubkeyhash.equals(session.getPubkeyhash()))
×
266
            l.add("local");
×
267
        // TODO: Make this more efficient:
268
        String hashed = Utils.createSha256HexHash(pubkeyOrPubkeyhash);
×
269
        if (User.getPubkeyhashes(user, true).contains(pubkeyOrPubkeyhash) || User.getPubkeyhashes(user, true).contains(hashed))
×
270
            l.add("approved");
×
271
        if (!l.isEmpty()) s += " (" + String.join("/", l) + ")";
×
272
        return s;
×
273
    }
274

275
    /**
276
     * Retrieves the name of the public key location based on the public key.
277
     *
278
     * @param pubkeyhash the public key string
279
     * @return the name of the public key location
280
     */
281
    public static String getPubkeyLocationName(String pubkeyhash) {
282
        return getPubkeyLocationName(pubkeyhash, getShortPubkeyName(pubkeyhash));
×
283
    }
284

285
    /**
286
     * Retrieves the name of the public key location, or returns a fallback name if not found.
287
     * If the key location is localhost, it returns "localhost".
288
     *
289
     * @param pubkeyhash the public key string
290
     * @param fallback   the fallback name to return if the key location is not found
291
     * @return the name of the public key location or the fallback name
292
     */
293
    public static String getPubkeyLocationName(String pubkeyhash, String fallback) {
294
        IRI keyLocation = User.getUserData().getKeyLocationForPubkeyHash(pubkeyhash);
×
295
        if (keyLocation == null) return fallback;
×
296
        if (keyLocation.stringValue().equals("http://localhost:37373/")) return "localhost";
×
297
        return keyLocation.stringValue().replaceFirst("https?://(nanobench\\.)?(nanodash\\.(?=.*\\..))?(.*[^/])/?$", "$3");
×
298
    }
299

300
    /**
301
     * Generates a short label for a public key location, including its status (local or approved).
302
     *
303
     * @param pubkeyhash the public key string
304
     * @param user       the IRI of the user associated with the public key
305
     * @return a short label indicating the public key location and its status
306
     */
307
    public static String getShortPubkeyLocationLabel(String pubkeyhash, IRI user) {
308
        String s = getPubkeyLocationName(pubkeyhash);
×
309
        NanodashSession session = NanodashSession.get();
×
310
        List<String> l = new ArrayList<>();
×
311
        if (pubkeyhash.equals(session.getPubkeyhash())) l.add("local");
×
312
        // TODO: Make this more efficient:
313
        if (User.getPubkeyhashes(user, true).contains(pubkeyhash)) l.add("approved");
×
314
        if (!l.isEmpty()) s += " (" + String.join("/", l) + ")";
×
315
        return s;
×
316
    }
317

318
    /**
319
     * Checks if a given public key has a Nanodash location.
320
     * A Nanodash location is identified by specific keywords in the key location.
321
     *
322
     * @param pubkeyhash the public key to check
323
     * @return true if the public key has a Nanodash location, false otherwise
324
     */
325
    public static boolean hasNanodashLocation(String pubkeyhash) {
326
        IRI keyLocation = User.getUserData().getKeyLocationForPubkeyHash(pubkeyhash);
×
327
        if (keyLocation == null) return true; // potentially a Nanodash location
×
328
        if (keyLocation.stringValue().contains("nanodash")) return true;
×
329
        if (keyLocation.stringValue().contains("nanobench")) return true;
×
330
        if (keyLocation.stringValue().contains(":37373")) return true;
×
331
        return false;
×
332
    }
333

334
    /**
335
     * Retrieves the short ORCID ID from an IRI object.
336
     *
337
     * @param orcidIri the IRI object representing the ORCID ID
338
     * @return the short ORCID ID as a string
339
     */
340
    public static String getShortOrcidId(IRI orcidIri) {
341
        return orcidIri.stringValue().replaceFirst("^https://orcid.org/", "");
18✔
342
    }
343

344
    /**
345
     * Retrieves the URI postfix from a given URI object.
346
     *
347
     * @param uri the URI object from which to extract the postfix
348
     * @return the URI postfix as a string
349
     */
350
    public static String getUriPostfix(Object uri) {
351
        String s = uri.toString();
9✔
352
        if (s.contains("#")) return s.replaceFirst("^.*#(.*)$", "$1");
27✔
353
        return s.replaceFirst("^.*/(.*)$", "$1");
15✔
354
    }
355

356
    /**
357
     * Retrieves the URI prefix from a given URI object.
358
     *
359
     * @param uri the URI object from which to extract the prefix
360
     * @return the URI prefix as a string
361
     */
362
    public static String getUriPrefix(Object uri) {
363
        String s = uri.toString();
9✔
364
        if (s.contains("#")) return s.replaceFirst("^(.*#).*$", "$1");
27✔
365
        return s.replaceFirst("^(.*/).*$", "$1");
15✔
366
    }
367

368
    /**
369
     * Checks if a given string is a valid URI postfix.
370
     * A valid URI postfix does not contain a colon (":").
371
     *
372
     * @param s the string to check
373
     * @return true if the string is a valid URI postfix, false otherwise
374
     */
375
    public static boolean isUriPostfix(String s) {
376
        return !s.contains(":");
24✔
377
    }
378

379
    /**
380
     * Retrieves the location of a given IntroNanopub.
381
     *
382
     * @param inp the IntroNanopub from which to extract the location
383
     * @return the IRI location of the nanopublication, or null if not found
384
     */
385
    public static IRI getLocation(IntroNanopub inp) {
386
        NanopubSignatureElement el = getNanopubSignatureElement(inp);
×
387
        for (KeyDeclaration kd : inp.getKeyDeclarations()) {
×
388
            if (el.getPublicKeyString().equals(kd.getPublicKeyString())) {
×
389
                return kd.getKeyLocation();
×
390
            }
391
        }
×
392
        return null;
×
393
    }
394

395
    /**
396
     * Retrieves the NanopubSignatureElement from a given IntroNanopub.
397
     *
398
     * @param inp the IntroNanopub from which to extract the signature element
399
     * @return the NanopubSignatureElement associated with the nanopublication
400
     */
401
    public static NanopubSignatureElement getNanopubSignatureElement(IntroNanopub inp) {
402
        try {
403
            return SignatureUtils.getSignatureElement(inp.getNanopub());
×
404
        } catch (MalformedCryptoElementException ex) {
×
405
            throw new RuntimeException(ex);
×
406
        }
407
    }
408

409
    /**
410
     * Retrieves a Nanopub object from a given URI if it is a potential Trusty URI.
411
     *
412
     * @param uri the URI to check and retrieve the Nanopub from
413
     * @return the Nanopub object if found, or null if not a known nanopublication
414
     */
415
    public static Nanopub getAsNanopub(String uri) {
416
        if (uri == null) return null;
6!
417
        if (TrustyUriUtils.isPotentialTrustyUri(uri)) {
9!
418
            try {
419
                return Utils.getNanopub(uri);
9✔
420
            } catch (Exception ex) {
×
421
                logger.error("The given URI is not a known nanopublication: {}", uri, ex);
×
422
            }
423
        }
424
        return null;
×
425
    }
426

427
    private static final PolicyFactory htmlSanitizePolicy = new HtmlPolicyBuilder()
9✔
428
            .allowCommonBlockElements()
3✔
429
            .allowCommonInlineFormattingElements()
45✔
430
            .allowUrlProtocols("https", "http", "mailto")
21✔
431
            .allowElements("a")
21✔
432
            .allowAttributes("href").onElements("a")
42✔
433
            .allowElements("img")
21✔
434
            .allowAttributes("src").onElements("img")
42✔
435
            .allowElements("pre")
3✔
436
            .requireRelNofollowOnLinks()
3✔
437
            .toFactory();
6✔
438

439
    /**
440
     * Sanitizes raw HTML input to ensure safe rendering.
441
     *
442
     * @param rawHtml the raw HTML input to sanitize
443
     * @return sanitized HTML string
444
     */
445
    public static String sanitizeHtml(String rawHtml) {
446
        return htmlSanitizePolicy.sanitize(rawHtml);
12✔
447
    }
448

449
    /**
450
     * Checks if a given string is likely to be HTML content.
451
     *
452
     * @param value the string to check
453
     * @return true if the given string is HTML content, false otherwise
454
     */
455
    public static boolean looksLikeHtml(String value) {
456
        return LEADING_TAG.matcher(value).find();
15✔
457
    }
458

459
    /**
460
     * Matches an xsd:dateTime-style literal with a time component, e.g.
461
     * "2026-04-16T08:27:12.954Z" or "2026-04-16T08:27:12+02:00".
462
     */
463
    private static final Pattern DATETIME_LITERAL =
3✔
464
            Pattern.compile("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})?$");
6✔
465

466
    /**
467
     * Checks whether a (raw query-result) string looks like an ISO-8601 date-time literal.
468
     *
469
     * @param value the string to check
470
     * @return true if the string parses as an xsd:dateTime-style value
471
     */
472
    public static boolean isDateTimeLiteral(String value) {
473
        return value != null && DATETIME_LITERAL.matcher(value).matches();
×
474
    }
475

476
    /**
477
     * Renders a {@code <time>} element for an ISO-8601 date-time value. The machine-readable
478
     * value goes in the {@code datetime} attribute; client-side script (nanodash.js) rewrites
479
     * the visible text to a relative form ("10 minutes ago") in the viewer's local timezone and
480
     * puts the absolute date-time in the tooltip. If script does not run, {@code fallbackText}
481
     * remains visible.
482
     *
483
     * @param isoValue     the ISO-8601 date-time string (machine-readable)
484
     * @param fallbackText the human-readable text shown when script is unavailable
485
     * @return an HTML {@code <time>} element string (caller must render with escaping disabled)
486
     */
487
    public static String friendlyDateHtml(String isoValue, String fallbackText) {
488
        return "<time class=\"friendly-date\" datetime=\"" + Strings.escapeMarkup(isoValue) + "\">"
×
489
                + Strings.escapeMarkup(fallbackText) + "</time>";
×
490
    }
491

492
    /**
493
     * Converts PageParameters to a URL-encoded string representation.
494
     *
495
     * @param params the PageParameters to convert
496
     * @return a string representation of the parameters in URL-encoded format
497
     */
498
    public static String getPageParametersAsString(PageParameters params) {
499
        String s = "";
6✔
500
        for (String n : params.getNamedKeys()) {
33✔
501
            if (!s.isEmpty()) s += "&";
18✔
502
            s += n + "=" + URLEncoder.encode(params.get(n).toString(), Charsets.UTF_8);
30✔
503
        }
3✔
504
        return s;
6✔
505
    }
506

507
    /**
508
     * Sets a minimal escape markup function for a Select2Choice component.
509
     * This function replaces certain characters and formats the display of choices.
510
     *
511
     * @param selectItem the Select2Choice component to set the escape markup for
512
     */
513
    public static void setSelect2ChoiceMinimalEscapeMarkup(Select2Choice<?> selectItem) {
514
        selectItem.getSettings().setEscapeMarkup("function(markup) {" +
15✔
515
                                                 "return markup" +
516
                                                 ".replaceAll('<','&lt;').replaceAll('>', '&gt;')" +
517
                                                 ".replace(/^(.*?) - /, '<span class=\"term\">$1</span><br>')" +
518
                                                 ".replace(/\\((https?:[\\S]+)\\)$/, '<br><code>$1</code>')" +
519
                                                 ".replace(/^([^<].*)$/, '<span class=\"term\">$1</span>')" +
520
                                                 ";}"
521
        );
522
    }
3✔
523

524
    /**
525
     * Checks if a nanopublication is of a specific class.
526
     *
527
     * @param np       the nanopublication to check
528
     * @param classIri the IRI of the class to check against
529
     * @return true if the nanopublication is of the specified class, false otherwise
530
     */
531
    public static boolean isNanopubOfClass(Nanopub np, IRI classIri) {
532
        return NanopubUtils.getTypes(np).contains(classIri);
15✔
533
    }
534

535
    /**
536
     * Checks if a nanopublication uses a specific predicate in its assertion.
537
     *
538
     * @param np           the nanopublication to check
539
     * @param predicateIri the IRI of the predicate to look for
540
     * @return true if the predicate is used in the assertion, false otherwise
541
     */
542
    public static boolean usesPredicateInAssertion(Nanopub np, IRI predicateIri) {
543
        for (Statement st : np.getAssertion()) {
33✔
544
            if (predicateIri.equals(st.getPredicate())) {
15✔
545
                return true;
6✔
546
            }
547
        }
3✔
548
        return false;
6✔
549
    }
550

551
    /**
552
     * Retrieves a map of FOAF names from the nanopublication's pubinfo.
553
     *
554
     * @param np the nanopublication from which to extract FOAF names
555
     * @return a map where keys are subjects and values are FOAF names
556
     */
557
    public static Map<String, String> getFoafNameMap(Nanopub np) {
558
        Map<String, String> foafNameMap = new HashMap<>();
12✔
559
        for (Statement st : np.getPubinfo()) {
33✔
560
            if (st.getPredicate().equals(FOAF.NAME) && st.getObject() instanceof Literal objL) {
42✔
561
                foafNameMap.put(st.getSubject().stringValue(), objL.stringValue());
24✔
562
            }
563
        }
3✔
564
        return foafNameMap;
6✔
565
    }
566

567
    /**
568
     * Creates an SHA-256 hash of the string representation of an object and returns it as a hexadecimal string.
569
     *
570
     * @param obj the object to hash
571
     * @return the SHA-256 hash of the object's string representation in hexadecimal format
572
     */
573
    public static String createSha256HexHash(Object obj) {
574
        return Hashing.sha256().hashString(obj.toString(), StandardCharsets.UTF_8).toString();
21✔
575
    }
576

577
    /**
578
     * Gets the types of a nanopublication.
579
     *
580
     * @param np the nanopublication from which to extract types
581
     * @return a list of IRI types associated with the nanopublication
582
     */
583
    public static List<IRI> getTypes(Nanopub np) {
584
        List<IRI> l = new ArrayList<>();
12✔
585
        for (IRI t : NanopubUtils.getTypes(np)) {
33✔
586
            if (t.equals(FIP.AVAILABLE_FAIR_ENABLING_RESOURCE)) continue;
15✔
587
            if (t.equals(FIP.FAIR_ENABLING_RESOURCE_TO_BE_DEVELOPED))
12✔
588
                continue;
3✔
589
            if (t.equals(FIP.AVAILABLE_FAIR_SUPPORTING_RESOURCE)) continue;
12!
590
            if (t.equals(FIP.FAIR_SUPPORTING_RESOURCE_TO_BE_DEVELOPED))
12!
591
                continue;
×
592
            l.add(t);
12✔
593
        }
3✔
594
        return l;
6✔
595
    }
596

597
    /**
598
     * Gets a label for a type IRI.
599
     *
600
     * @param typeIri the IRI of the type
601
     * @return a label for the type, potentially truncated
602
     */
603
    public static String getTypeLabel(IRI typeIri) {
604
        if (typeIri.equals(FIP.FAIR_ENABLING_RESOURCE)) return "FER";
18✔
605
        if (typeIri.equals(FIP.FAIR_SUPPORTING_RESOURCE)) return "FSR";
18✔
606
        if (typeIri.equals(FIP.FAIR_IMPLEMENTATION_PROFILE)) return "FIP";
18✔
607
        if (typeIri.equals(NPX.DECLARED_BY)) return "user intro";
18✔
608
        String l = typeIri.stringValue();
9✔
609
        l = l.replaceFirst("^.*[/#]([^/#]+)[/#]?$", "$1");
15✔
610
        l = l.replaceFirst("^(.+)Nanopub$", "$1");
15✔
611
        if (l.length() > 25) l = l.substring(0, 20) + "...";
30✔
612
        return l;
6✔
613
    }
614

615
    /**
616
     * Gets a label for a URI.
617
     *
618
     * @param uri the URI to get the label from
619
     * @return a label for the URI, potentially truncated
620
     */
621
    public static String getUriLabel(String uri) {
622
        if (uri == null) return "";
12✔
623
        String uriLabel = uri;
6✔
624
        if (uriLabel.matches(".*[^A-Za-z0-9-_]RA[A-Za-z0-9-_]{43}([^A-Za-z0-9-_].*)?")) {
12✔
625
            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✔
626
            if (newUriLabel.length() <= 70) return newUriLabel;
18!
627
        }
628
        if (uriLabel.length() > 70) return uri.substring(0, 30) + "..." + uri.substring(uri.length() - 30);
48✔
629
        return uriLabel;
6✔
630
    }
631

632
    /**
633
     * Gets an ExternalLink with a URI label.
634
     *
635
     * @param markupId the markup ID for the link
636
     * @param uri      the URI to link to
637
     * @return an ExternalLink with the URI label
638
     */
639
    public static ExternalLink getUriLink(String markupId, String uri) {
640
        return new ExternalLink(markupId, (Utils.isLocalURI(uri) ? "" : uri), getUriLabel(uri));
39✔
641
    }
642

643
    /**
644
     * Gets an ExternalLink with a model for the URI label.
645
     *
646
     * @param markupId the markup ID for the link
647
     * @param model    the model containing the URI
648
     * @return an ExternalLink with the URI label
649
     */
650
    public static ExternalLink getUriLink(String markupId, IModel<String> model) {
651
        return new ExternalLink(markupId, model, new UriLabelModel(model));
30✔
652
    }
653

654
    private static class UriLabelModel implements IModel<String> {
655

656
        private IModel<String> uriModel;
657

658
        public UriLabelModel(IModel<String> uriModel) {
6✔
659
            this.uriModel = uriModel;
9✔
660
        }
3✔
661

662
        @Override
663
        public String getObject() {
664
            return getUriLabel(uriModel.getObject());
×
665
        }
666

667
    }
668

669
    /**
670
     * Creates a sublist from a list based on the specified indices.
671
     *
672
     * @param list      the list from which to create the sublist
673
     * @param fromIndex the starting index (inclusive) for the sublist
674
     * @param toIndex   the ending index (exclusive) for the sublist
675
     * @param <E>       the type of elements in the list
676
     * @return an ArrayList containing the elements from the specified range
677
     */
678
    public static <E> ArrayList<E> subList(List<E> list, long fromIndex, long toIndex) {
679
        // So the resulting list is serializable:
680
        return new ArrayList<E>(list.subList((int) fromIndex, (int) toIndex));
×
681
    }
682

683
    /**
684
     * Creates a sublist from an array based on the specified indices.
685
     *
686
     * @param array     the array from which to create the sublist
687
     * @param fromIndex the starting index (inclusive) for the sublist
688
     * @param toIndex   the ending index (exclusive) for the sublist
689
     * @param <E>       the type of elements in the array
690
     * @return an ArrayList containing the elements from the specified range
691
     */
692
    public static <E> ArrayList<E> subList(E[] array, long fromIndex, long toIndex) {
693
        return subList(Arrays.asList(array), fromIndex, toIndex);
×
694
    }
695

696
    /**
697
     * Comparator for sorting ApiResponseEntry objects based on a specified field.
698
     */
699
    // TODO Move this to ApiResponseEntry class?
700
    public static class ApiResponseEntrySorter implements Comparator<ApiResponseEntry>, Serializable {
701

702
        private String field;
703
        private boolean descending;
704

705
        /**
706
         * Constructor for ApiResponseEntrySorter.
707
         *
708
         * @param field      the field to sort by
709
         * @param descending if true, sorts in descending order; if false, sorts in ascending order
710
         */
711
        public ApiResponseEntrySorter(String field, boolean descending) {
×
712
            this.field = field;
×
713
            this.descending = descending;
×
714
        }
×
715

716
        /**
717
         * Compares two ApiResponseEntry objects based on the specified field.
718
         *
719
         * @param o1 the first object to be compared.
720
         * @param o2 the second object to be compared.
721
         * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
722
         */
723
        @Override
724
        public int compare(ApiResponseEntry o1, ApiResponseEntry o2) {
725
            if (descending) {
×
726
                return o2.get(field).compareTo(o1.get(field));
×
727
            } else {
728
                return o1.get(field).compareTo(o2.get(field));
×
729
            }
730
        }
731

732
    }
733

734
    /**
735
     * MIME type for TriG RDF format.
736
     */
737
    public static final String TYPE_TRIG = "application/trig";
738

739
    /**
740
     * MIME type for Jelly RDF format.
741
     */
742
    public static final String TYPE_JELLY = "application/x-jelly-rdf";
743

744
    /**
745
     * MIME type for JSON-LD format.
746
     */
747
    public static final String TYPE_JSONLD = "application/ld+json";
748

749
    /**
750
     * MIME type for N-Quads format.
751
     */
752
    public static final String TYPE_NQUADS = "application/n-quads";
753

754
    /**
755
     * MIME type for Trix format.
756
     */
757
    public static final String TYPE_TRIX = "application/trix";
758

759
    /**
760
     * MIME type for HTML format.
761
     */
762
    public static final String TYPE_HTML = "text/html";
763

764
    /**
765
     * Comma-separated list of supported MIME types for nanopublications.
766
     */
767
    public static final String SUPPORTED_TYPES =
768
            TYPE_TRIG + "," +
769
            TYPE_JELLY + "," +
770
            TYPE_JSONLD + "," +
771
            TYPE_NQUADS + "," +
772
            TYPE_TRIX + "," +
773
            TYPE_HTML;
774

775
    /**
776
     * List of supported MIME types for nanopublications.
777
     */
778
    public static final List<String> SUPPORTED_TYPES_LIST = Arrays.asList(StringUtils.split(SUPPORTED_TYPES, ','));
18✔
779

780
    private static volatile String resolvedMainRegistryUrl;
781
    private static volatile String resolvedMainQueryUrl;
782

783
    /**
784
     * Eagerly resolves the main registry and query URLs. Call at application startup
785
     * so the (potentially slow) first-time discovery does not happen during a user request.
786
     */
787
    public static void initMainUrls() {
788
        getMainRegistryUrl();
6✔
789
        getMainQueryUrl();
6✔
790
    }
3✔
791

792
    /**
793
     * Returns the URL of the main Nanopub Registry for this nanodash instance.
794
     * <p>
795
     * If {@code NANODASH_MAIN_REGISTRY} is set and matches an entry in the library's
796
     * discovered registry instance list, that URL is used. Otherwise the first entry
797
     * of the library list is used. If the library list is empty, the env var value
798
     * (or built-in default) is used unvalidated. The result is cached for the JVM lifetime.
799
     *
800
     * @return Nanopub Registry URL (with trailing slash)
801
     */
802
    public static String getMainRegistryUrl() {
803
        if (resolvedMainRegistryUrl == null) {
6✔
804
            synchronized (Utils.class) {
12✔
805
                if (resolvedMainRegistryUrl == null) {
6!
806
                    resolvedMainRegistryUrl = resolveMainRegistryUrl();
6✔
807
                }
808
            }
9✔
809
        }
810
        return resolvedMainRegistryUrl;
6✔
811
    }
812

813
    /**
814
     * Returns the URL of the main Nanopub Query API for this nanodash instance.
815
     * <p>
816
     * If {@code NANODASH_MAIN_QUERY} is set and matches an entry in the library's
817
     * discovered query instance list, that URL is used. Otherwise the first entry
818
     * of the library list is used. If the library list is empty, the env var value
819
     * (or built-in default) is used unvalidated. The result is cached for the JVM lifetime.
820
     *
821
     * @return Nanopub Query URL (with trailing slash)
822
     */
823
    public static String getMainQueryUrl() {
824
        if (resolvedMainQueryUrl == null) {
6✔
825
            synchronized (Utils.class) {
12✔
826
                if (resolvedMainQueryUrl == null) {
6!
827
                    resolvedMainQueryUrl = resolveMainQueryUrl();
6✔
828
                }
829
            }
9✔
830
        }
831
        return resolvedMainQueryUrl;
6✔
832
    }
833

834
    private static String resolveMainRegistryUrl() {
835
        String envValue = trimToNull(System.getenv("NANODASH_MAIN_REGISTRY"));
12✔
836
        List<String> instances;
837
        try {
838
            instances = NanopubServerUtils.getRegistryServerList();
6✔
839
        } catch (Exception ex) {
×
840
            logger.warn("Could not retrieve registry instance list from nanopub library: {}", ex.toString());
×
841
            instances = Collections.emptyList();
×
842
        }
3✔
843
        return resolveMainUrl("NANODASH_MAIN_REGISTRY", envValue, instances, DEFAULT_MAIN_REGISTRY_URL);
18✔
844
    }
845

846
    private static String resolveMainQueryUrl() {
847
        String envValue = trimToNull(System.getenv("NANODASH_MAIN_QUERY"));
12✔
848
        List<String> instances;
849
        try {
850
            instances = QueryCall.getApiInstances();
6✔
851
        } catch (NotEnoughAPIInstancesException ex) {
×
852
            logger.warn("Nanopub library reports not enough query API instances available: {}", ex.toString());
×
853
            instances = Collections.emptyList();
×
854
        } catch (Exception ex) {
×
855
            logger.warn("Could not retrieve query instance list from nanopub library: {}", ex.toString());
×
856
            instances = Collections.emptyList();
×
857
        }
3✔
858
        return resolveMainUrl("NANODASH_MAIN_QUERY", envValue, instances, DEFAULT_MAIN_QUERY_URL);
18✔
859
    }
860

861
    private static String resolveMainUrl(String envVarName, String envValue, List<String> instances, String builtInDefault) {
862
        if (envValue != null) {
6!
863
            if (containsNormalized(instances, envValue)) {
×
864
                logger.info("Using main URL from {} (validated against library instance list): {}", envVarName, envValue);
×
865
                return ensureTrailingSlash(envValue);
×
866
            }
867
            if (instances.isEmpty()) {
×
868
                logger.warn("Library instance list is empty; using {} unvalidated: {}", envVarName, envValue);
×
869
                return ensureTrailingSlash(envValue);
×
870
            }
871
            logger.warn("{}={} is not in the library instance list {}; falling back to first library instance",
×
872
                    envVarName, envValue, instances);
873
            return ensureTrailingSlash(instances.get(0));
×
874
        }
875
        if (!instances.isEmpty()) {
9!
876
            String first = instances.get(0);
15✔
877
            logger.info("{} not set; using first library instance: {}", envVarName, first);
15✔
878
            return ensureTrailingSlash(first);
9✔
879
        }
880
        logger.warn("{} not set and library instance list is empty; using built-in default: {}", envVarName, builtInDefault);
×
881
        return builtInDefault;
×
882
    }
883

884
    private static boolean containsNormalized(List<String> urls, String target) {
885
        String normTarget = normalizeUrl(target);
×
886
        for (String url : urls) {
×
887
            if (normalizeUrl(url).equals(normTarget)) return true;
×
888
        }
×
889
        return false;
×
890
    }
891

892
    private static String normalizeUrl(String url) {
893
        if (url == null) return "";
×
894
        return url.trim().replaceFirst("/+$", "").toLowerCase(Locale.ROOT);
×
895
    }
896

897
    private static String ensureTrailingSlash(String url) {
898
        return url.endsWith("/") ? url : url + "/";
21!
899
    }
900

901
    private static String trimToNull(String s) {
902
        if (s == null) return null;
12!
903
        s = s.trim();
×
904
        return s.isEmpty() ? null : s;
×
905
    }
906

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

911
    /**
912
     * Checks whether string is valid literal serialization.
913
     *
914
     * @param literalString the literal string
915
     * @return true if valid
916
     */
917
    public static boolean isValidLiteralSerialization(String literalString) {
918
        if (literalString.matches(PLAIN_LITERAL_PATTERN)) {
12✔
919
            return true;
6✔
920
        } else if (literalString.matches(LANGTAG_LITERAL_PATTERN)) {
12✔
921
            return true;
6✔
922
        } else if (literalString.matches(DATATYPE_LITERAL_PATTERN)) {
12✔
923
            return true;
6✔
924
        }
925
        return false;
6✔
926
    }
927

928
    /**
929
     * Returns a serialized version of the literal.
930
     *
931
     * @param literal the literal
932
     * @return the String serialization of the literal
933
     */
934
    public static String getSerializedLiteral(Literal literal) {
935
        if (literal.getLanguage().isPresent()) {
12✔
936
            return "\"" + getEscapedLiteralString(literal.stringValue()) + "\"@" + Literals.normalizeLanguageTag(literal.getLanguage().get());
30✔
937
        } else if (literal.getDatatype().equals(XSD.STRING)) {
15✔
938
            return "\"" + getEscapedLiteralString(literal.stringValue()) + "\"";
15✔
939
        } else {
940
            return "\"" + getEscapedLiteralString(literal.stringValue()) + "\"^^<" + literal.getDatatype() + ">";
24✔
941
        }
942
    }
943

944
    /**
945
     * Parses a serialized literal into a Literal object.
946
     *
947
     * @param serializedLiteral The serialized String of the literal
948
     * @return The parse Literal object
949
     */
950
    public static Literal getParsedLiteral(String serializedLiteral) {
951
        if (serializedLiteral.matches(PLAIN_LITERAL_PATTERN)) {
12✔
952
            return vf.createLiteral(getUnescapedLiteralString(serializedLiteral.replaceFirst(PLAIN_LITERAL_PATTERN, "$1")));
24✔
953
        } else if (serializedLiteral.matches(LANGTAG_LITERAL_PATTERN)) {
12✔
954
            String langtag = serializedLiteral.replaceFirst(LANGTAG_LITERAL_PATTERN, "$3");
15✔
955
            return vf.createLiteral(getUnescapedLiteralString(serializedLiteral.replaceFirst(LANGTAG_LITERAL_PATTERN, "$1")), langtag);
27✔
956
        } else if (serializedLiteral.matches(DATATYPE_LITERAL_PATTERN)) {
12✔
957
            IRI datatype = vf.createIRI(serializedLiteral.replaceFirst(DATATYPE_LITERAL_PATTERN, "$3"));
21✔
958
            return vf.createLiteral(getUnescapedLiteralString(serializedLiteral.replaceFirst(DATATYPE_LITERAL_PATTERN, "$1")), datatype);
27✔
959
        }
960
        throw new IllegalArgumentException("Not a valid literal serialization: " + serializedLiteral);
18✔
961
    }
962

963
    /**
964
     * Escapes quotes (") and slashes (/) of a literal string.
965
     *
966
     * @param unescapedString un-escaped string
967
     * @return escaped string
968
     */
969
    public static String getEscapedLiteralString(String unescapedString) {
970
        return unescapedString.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\"");
24✔
971
    }
972

973
    /**
974
     * Un-escapes quotes (") and slashes (/) of a literal string.
975
     *
976
     * @param escapedString escaped string
977
     * @return un-escaped string
978
     */
979
    public static String getUnescapedLiteralString(String escapedString) {
980
        return escapedString.replaceAll("\\\\(\\\\|\\\")", "$1");
15✔
981
    }
982

983
    /**
984
     * Checks if a given IRI is a local URI.
985
     *
986
     * @param uri the IRI to check
987
     * @return true if the IRI is a local URI, false otherwise
988
     */
989
    public static boolean isLocalURI(IRI uri) {
990
        return uri != null && isLocalURI(uri.stringValue());
30✔
991
    }
992

993
    /**
994
     * Checks if a given string is a local URI.
995
     *
996
     * @param uriAsString the string to check
997
     * @return true if the string is a local URI, false otherwise
998
     */
999
    public static boolean isLocalURI(String uriAsString) {
1000
        return !uriAsString.isBlank() && uriAsString.startsWith(LocalUri.PREFIX);
33✔
1001
    }
1002

1003
    public static String unescapeMultiValue(String s) {
1004
        StringBuilder sb = new StringBuilder();
12✔
1005
        for (int i = 0; i < s.length(); i++) {
24✔
1006
            if (s.charAt(i) == '\\' && i + 1 < s.length()) {
33✔
1007
                char next = s.charAt(i + 1);
18✔
1008
                if (next == 'n') {
9✔
1009
                    sb.append('\n');
15✔
1010
                } else if (next == '\\') {
9!
1011
                    sb.append('\\');
15✔
1012
                } else {
1013
                    sb.append(next);
×
1014
                }
1015
                i++;
3✔
1016
            } else {
3✔
1017
                sb.append(s.charAt(i));
18✔
1018
            }
1019
        }
1020
        return sb.toString();
9✔
1021
    }
1022

1023
}
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