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

knowledgepixels / nanodash / 24458776542

15 Apr 2026 01:59PM UTC coverage: 15.798% (-0.002%) from 15.8%
24458776542

push

github

tkuhn
fix: hide prefix label on read-only items when it is a raw URL

Prefix labels that leak through as http(s):// URLs (e.g. when no
human-readable label was provided) cluttered the UI on read-only
items. Treat them the same as a missing label and render nothing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

790 of 6188 branches covered (12.77%)

Branch coverage included in aggregate %.

1979 of 11339 relevant lines covered (17.45%)

2.39 hits per line

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

0.0
src/main/java/com/knowledgepixels/nanodash/component/ReadonlyItem.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.LocalUri;
4
import com.knowledgepixels.nanodash.RestrictedChoice;
5
import com.knowledgepixels.nanodash.domain.User;
6
import com.knowledgepixels.nanodash.Utils;
7
import com.knowledgepixels.nanodash.component.StatementItem.RepetitionGroup;
8
import com.knowledgepixels.nanodash.domain.IndividualAgent;
9
import com.knowledgepixels.nanodash.domain.MaintainedResource;
10
import com.knowledgepixels.nanodash.domain.Space;
11
import com.knowledgepixels.nanodash.page.ExplorePage;
12
import com.knowledgepixels.nanodash.repository.MaintainedResourceRepository;
13
import com.knowledgepixels.nanodash.repository.SpaceRepository;
14
import com.knowledgepixels.nanodash.template.ContextType;
15
import com.knowledgepixels.nanodash.template.Template;
16
import com.knowledgepixels.nanodash.template.UnificationException;
17
import net.trustyuri.TrustyUriUtils;
18
import org.apache.commons.codec.Charsets;
19
import org.apache.wicket.behavior.AttributeAppender;
20
import org.apache.wicket.markup.html.basic.Label;
21
import org.apache.wicket.markup.html.link.ExternalLink;
22
import org.apache.wicket.model.IModel;
23
import org.apache.wicket.model.Model;
24
import org.apache.wicket.validation.IValidatable;
25
import org.apache.wicket.validation.IValidator;
26
import org.apache.wicket.validation.Validatable;
27
import org.apache.wicket.validation.ValidationError;
28
import org.eclipse.rdf4j.common.net.ParsedIRI;
29
import org.eclipse.rdf4j.model.IRI;
30
import org.eclipse.rdf4j.model.Literal;
31
import org.eclipse.rdf4j.model.Value;
32
import org.eclipse.rdf4j.model.ValueFactory;
33
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
34
import org.eclipse.rdf4j.model.util.Literals;
35
import org.eclipse.rdf4j.model.vocabulary.XSD;
36
import org.nanopub.Nanopub;
37
import org.nanopub.NanopubUtils;
38
import org.nanopub.SimpleCreatorPattern;
39
import org.nanopub.vocabulary.NTEMPLATE;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

43
import java.net.URISyntaxException;
44
import java.net.URLEncoder;
45
import java.util.HashMap;
46
import java.util.Map;
47

48
/**
49
 * ReadonlyItem is a component that displays a read-only item in the form.
50
 */
51
public class ReadonlyItem extends AbstractContextComponent {
52

53
    private static final int LONG_LITERAL_LENGTH = 100;
54
    private static final Logger logger = LoggerFactory.getLogger(ReadonlyItem.class);
×
55
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
×
56

57
    private IModel<String> model;
58
    private String prefix;
59
    private ExternalLink linkComp;
60
    private Label extraComp, languageComp, datatypeComp;
61
    private IModel<String> extraModel, languageModel, datatypeModel;
62
    private IRI iri;
63
    private RestrictedChoice restrictedChoice;
64
    private Label showMoreLabelLiteral, showMoreLabelHTML;
65
    private final Template template;
66

67
    /**
68
     * Constructor for ReadonlyItem.
69
     *
70
     * @param id              the component id
71
     * @param parentId        the parent id (e.g., "subj", "obj")
72
     * @param iriP            the IRI of the item
73
     * @param statementPartId the statement part ID
74
     * @param rg              the repetition group
75
     */
76
    public ReadonlyItem(String id, String parentId, final IRI iriP, IRI statementPartId, final RepetitionGroup rg) {
77
        super(id, rg.getContext());
×
78
        this.iri = iriP;
×
79
        template = context.getTemplate();
×
80
        model = (IModel<String>) context.getComponentModels().get(iri);
×
81
        if (model == null) {
×
82
            model = Model.of("");
×
83
            context.getComponentModels().put(iri, model);
×
84
        }
85
        String postfix = Utils.getUriPostfix(iri);
×
86
        if (context.hasParam(postfix)) {
×
87
            model.setObject(context.getParam(postfix));
×
88
        }
89

90
        final Map<String, String> foafNameMap;
91
        if (context.getExistingNanopub() == null) {
×
92
            foafNameMap = new HashMap<>();
×
93
        } else {
94
            foafNameMap = Utils.getFoafNameMap(context.getExistingNanopub());
×
95
        }
96

97
        prefix = template.getPrefix(iri);
×
98
        if (prefix == null) prefix = "";
×
99
        if (template.isRestrictedChoicePlaceholder(iri)) {
×
100
            restrictedChoice = new RestrictedChoice(iri, context);
×
101
        }
102
        add(new Label("prefix", new Model<String>() {
×
103

104
            @Override
105
            public String getObject() {
106
                String prefixLabel = template.getPrefixLabel(iri);
×
107
                String v = getFullValue();
×
108
                if (prefixLabel == null || IndividualAgent.isUser(v) || foafNameMap.containsKey(v)) {
×
109
                    return "";
×
110
                } else if (prefixLabel.matches("https?://.*")) {
×
111
                    // Hide prefix label that leaks through as a raw URL.
112
                    return "";
×
113
                } else {
114
                    if (!prefixLabel.isEmpty() && parentId.equals("subj")) {
×
115
                        // Capitalize first letter of label if at subject position:
116
                        prefixLabel = prefixLabel.substring(0, 1).toUpperCase() + prefixLabel.substring(1);
×
117
                    }
118
                    return prefixLabel;
×
119
                }
120
            }
121

122
        }));
123

124
        linkComp = new ExternalLink("link", new Model<String>() {
×
125

126
            @Override
127
            public String getObject() {
128
                String obj = getFullValue();
×
129
                if (obj == null) return "";
×
130
                if (obj.equals(LocalUri.of("nanopub").stringValue())) {
×
131
                    if (context.getExistingNanopub() != null) {
×
132
                        obj = context.getExistingNanopub().getUri().stringValue();
×
133
                        return ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(obj, Charsets.UTF_8);
×
134
                    } else {
135
                        return "";
×
136
                    }
137
                } else if (obj.equals(LocalUri.of("assertion").stringValue())) {
×
138
                    if (context.getExistingNanopub() != null) {
×
139
                        obj = context.getExistingNanopub().getAssertionUri().stringValue();
×
140
                        return ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(obj, Charsets.UTF_8);
×
141
                    } else {
142
                        return "";
×
143
                    }
144
                } else if (obj.matches("https?://.+")) {
×
145
                    return NanodashLink.getPageUrl(obj);
×
146
                } else {
147
                    return "";
×
148
                }
149
            }
150

151
        }, new Model<String>() {
×
152

153
            @Override
154
            public String getObject() {
155
                String obj = getFullValue();
×
156
                if (obj != null && obj.matches("https?://.+")) {
×
157
                    IRI objIri = vf.createIRI(obj);
×
158
                    if (iri.equals(NTEMPLATE.CREATOR_PLACEHOLDER)) {
×
159
                        // TODO We might want to introduce a "(you)" flag here at some point
160
                        return User.getShortDisplayName(objIri);
×
161
                    } else if (isAssertionValue(objIri)) {
×
162
                        if (context.getType() == ContextType.ASSERTION) {
×
163
                            return "this assertion";
×
164
                        } else {
165
                            return "the assertion above";
×
166
                        }
167
                    } else if (isNanopubValue(objIri)) {
×
168
                        return "this nanopublication";
×
169
                    } else if (IndividualAgent.isUser(obj)) {
×
170
                        return User.getShortDisplayName(objIri);
×
171
                    } else if (foafNameMap.containsKey(obj)) {
×
172
                        return foafNameMap.get(obj);
×
173
                    }
174
                    Space space = SpaceRepository.get().findById(obj);
×
175
                    if (space != null) return space.getLabel();
×
176
                    space = SpaceRepository.get().findByAltId(obj);
×
177
                    if (space != null) return space.getLabel();
×
178
                    MaintainedResource mr = MaintainedResourceRepository.get().findById(obj);
×
179
                    if (mr != null) return mr.getLabel();
×
180
                    return getLabelString(objIri);
×
181
                }
182
                return obj;
×
183
            }
184

185
        });
186
        if (template.isIntroducedResource(iri) || template.isEmbeddedResource(iri)) {
×
187
            linkComp.add(AttributeAppender.append("class", "introduced"));
×
188
        }
189
        add(linkComp);
×
190
        add(new Label("description", new Model<String>() {
×
191

192
            @Override
193
            public String getObject() {
194
                String obj = getFullValue();
×
195
                if (obj != null && obj.matches("https?://.+")) {
×
196
                    IRI objIri = vf.createIRI(obj);
×
197
                    if (isAssertionValue(objIri)) {
×
198
                        return "This is the identifier for the assertion of this nanopublication.";
×
199
                    } else if (isNanopubValue(objIri)) {
×
200
                        return "This is the identifier for this whole nanopublication.";
×
201
                    } else if (context.isReadOnly() && obj.startsWith(context.getExistingNanopub().getUri().stringValue())) {
×
202
                        return "This is a local identifier minted within the nanopublication.";
×
203
                    }
204
                    String labelString = getLabelString(objIri);
×
205
                    String description = "";
×
206
                    if (labelString.contains(" - ")) description = labelString.replaceFirst("^.* - ", "");
×
207
                    return description;
×
208
                } else if (obj != null && obj.startsWith("\"")) {
×
209
                    return "(this is a literal)";
×
210
                }
211
                return "";
×
212
            }
213

214
        }));
215
        Model<String> uriModel = new Model<String>() {
×
216

217
            @Override
218
            public String getObject() {
219
                String obj = getFullValue();
×
220
                if (obj != null && obj.startsWith("\"")) return "";
×
221
                if (isAssertionValue(obj)) {
×
222
                    return getAssertionValue();
×
223
                } else if (isNanopubValue(obj)) {
×
224
                    return getNanopubValue();
×
225
                }
226
                return obj;
×
227
            }
228

229
        };
230
        add(Utils.getUriLink("uri", uriModel));
×
231
        extraModel = Model.of("");
×
232
        extraComp = new Label("extra", extraModel);
×
233
        extraComp.setVisible(false);
×
234
        add(extraComp);
×
235
        languageModel = Model.of("");
×
236
        languageComp = new Label("language", languageModel);
×
237
        languageComp.setVisible(false);
×
238
        add(languageComp);
×
239
        datatypeModel = Model.of("");
×
240
        datatypeComp = new Label("datatype", datatypeModel);
×
241
        datatypeComp.setVisible(false);
×
242
        add(datatypeComp);
×
243

244
        showMoreLabelLiteral = new Label("show-more-literal", "");
×
245
        add(showMoreLabelLiteral);
×
246
        showMoreLabelLiteral.setVisible(false);
×
247

248
        showMoreLabelHTML = new Label("show-more-html", "");
×
249
        add(showMoreLabelHTML);
×
250
        showMoreLabelHTML.setVisible(false);
×
251
    }
×
252

253
    /**
254
     * {@inheritDoc}
255
     */
256
    @Override
257
    public void fillFinished() {
258
        String obj = getFullValue();
×
259
        if (obj != null) {
×
260
            if (isAssertionValue(obj)) {
×
261
                linkComp.add(new AttributeAppender("class", "this-assertion"));
×
262
            } else if (isNanopubValue(obj)) {
×
263
                linkComp.add(new AttributeAppender("class", "this-nanopub"));
×
264
            } else if (context.getExistingNanopub() != null) {
×
265
                Nanopub np = context.getExistingNanopub();
×
266
                if (NanopubUtils.getIntroducedIriIds(np).contains(obj) || NanopubUtils.getEmbeddedIriIds(np).contains(obj)) {
×
267
                    linkComp.add(AttributeAppender.append("class", "introduced"));
×
268
                }
269
            }
270
        }
271
    }
×
272

273
    /**
274
     * {@inheritDoc}
275
     */
276
    @Override
277
    public void finalizeValues() {
278
    }
×
279

280
    private String getLabelString(IRI iri) {
281
        if (template.getLabel(iri) != null) {
×
282
            return template.getLabel(iri).replaceFirst(" - .*$", "");
×
283
        } else if (context.getLabel(iri) != null) {
×
284
            return context.getLabel(iri).replaceFirst(" - .*$", "");
×
285
        } else {
286
            return Utils.getShortNameFromURI(iri.stringValue());
×
287
        }
288
    }
289

290
    /**
291
     * {@inheritDoc}
292
     */
293
    @Override
294
    public void removeFromContext() {
295
        // Nothing to be done here.
296
    }
×
297

298
    private String getFullValue() {
299
        String s = model.getObject();
×
300
        if (s == null) return null;
×
301
        if (template.isAutoEscapePlaceholder(iri)) {
×
302
            s = Utils.urlEncode(s);
×
303
        }
304
        if (!prefix.isEmpty()) {
×
305
            s = prefix + s;
×
306
        }
307
        return s;
×
308
    }
309

310
    private boolean isNanopubValue(Object obj) {
311
        if (obj == null) return false;
×
312
        if (obj.toString().equals(LocalUri.of("nanopub").stringValue())) return true;
×
313
        if (context.getExistingNanopub() == null) return false;
×
314
        return obj.toString().equals(context.getExistingNanopub().getUri().stringValue());
×
315
    }
316

317
    private String getNanopubValue() {
318
        if (context.getExistingNanopub() != null) {
×
319
            return context.getExistingNanopub().getUri().stringValue();
×
320
        } else {
321
            return LocalUri.of("nanopub").stringValue();
×
322
        }
323
    }
324

325
    private boolean isAssertionValue(Object obj) {
326
        if (obj == null) return false;
×
327
        if (obj.toString().equals(LocalUri.of("assertion").stringValue())) return true;
×
328
        if (context.getExistingNanopub() == null) return false;
×
329
        return obj.toString().equals(context.getExistingNanopub().getAssertionUri().stringValue());
×
330
    }
331

332
    private String getAssertionValue() {
333
        if (context.getExistingNanopub() != null) {
×
334
            return context.getExistingNanopub().getAssertionUri().stringValue();
×
335
        } else {
336
            return LocalUri.of("assertion").stringValue();
×
337
        }
338
    }
339

340
    /**
341
     * {@inheritDoc}
342
     */
343
    @Override
344
    public boolean isUnifiableWith(Value v) {
345
        if (v == null) return true;
×
346
        if (v instanceof IRI) {
×
347
            String vs = v.stringValue();
×
348
            if (vs.equals(LocalUri.of("nanopub").stringValue())) {
×
349
                vs = getNanopubValue();
×
350
            } else if (vs.equals(LocalUri.of("assertion").stringValue())) {
×
351
                vs = getAssertionValue();
×
352
            }
353
            if (vs.startsWith(prefix)) vs = vs.substring(prefix.length());
×
354
//                        if (Utils.isLocalURI(vs)) vs = vs.replaceFirst("^" + LocalUri.PREFIX, "");
355
            if (template.isAutoEscapePlaceholder(iri)) {
×
356
                vs = Utils.urlDecode(vs);
×
357
            }
358
            Validatable<String> validatable = new Validatable<>(vs);
×
359
//                        if (template.isLocalResource(iri) && !Utils.isUriPostfix(vs)) {
360
//                                vs = Utils.getUriPostfix(vs);
361
//                        }
362
            new Validator().validate(validatable);
×
363
            if (!validatable.isValid()) {
×
364
                return false;
×
365
            }
366
            if (model.getObject().isEmpty()) {
×
367
                return true;
×
368
            }
369
            return vs.equals(model.getObject());
×
370
        } else if (v instanceof Literal vL) {
×
371
            if (template.getRegex(iri) != null && !v.stringValue().matches(template.getRegex(iri))) {
×
372
                return false;
×
373
            }
374
            String languagetag = template.getLanguageTag(iri);
×
375
            IRI datatype = template.getDatatype(iri);
×
376
            if (languagetag != null) {
×
377
                if (vL.getLanguage().isEmpty() || !Literals.normalizeLanguageTag(vL.getLanguage().get()).equals(languagetag)) {
×
378
                    return false;
×
379
                }
380
            } else if (datatype != null) {
×
381
                if (!vL.getDatatype().equals(datatype)) {
×
382
                    return false;
×
383
                }
384
            }
385
            if (linkComp.getDefaultModelObject() == null || linkComp.getDefaultModelObject().toString().isEmpty()) {
×
386
                return true;
×
387
            }
388
            return linkComp.getDefaultModelObject().equals("\"" + v.stringValue() + "\"");
×
389
        }
390
        return false;
×
391
    }
392

393
    /**
394
     * {@inheritDoc}
395
     */
396
    @Override
397
    public void unifyWith(Value v) throws UnificationException {
398
        if (v == null) return;
×
399
        String vs = v.stringValue();
×
400
        if (!isUnifiableWith(v)) {
×
401
            logger.error("Cannot unify {}", v);
×
402
            throw new UnificationException(vs);
×
403
        }
404
        if (v instanceof IRI) {
×
405
            if (vs.equals(LocalUri.of("nanopub").stringValue())) {
×
406
                vs = getNanopubValue();
×
407
            } else if (vs.equals(LocalUri.of("assertion").stringValue())) {
×
408
                vs = getAssertionValue();
×
409
            }
410
            if (!prefix.isEmpty() && vs.startsWith(prefix)) {
×
411
                vs = vs.substring(prefix.length());
×
412
                // With read-only items, we don't need preliminary local identifiers:
413
//                        } else if (Utils.isLocalURI(vs)) {
414
//                                vs = vs.replaceFirst("^" + LocalUri.PREFIX, "");
415
//                        } else if (template.isLocalResource(iri) && !Utils.isUriPostfix(vs)) {
416
//                                vs = Utils.getUriPostfix(vs);
417
            }
418
            if (template.isAutoEscapePlaceholder(iri)) {
×
419
                vs = Utils.urlDecode(vs);
×
420
            }
421
            model.setObject(vs);
×
422
        } else if (v instanceof Literal vL) {
×
423
            if (vs.length() >= LONG_LITERAL_LENGTH) {
×
424
                linkComp.add(AttributeAppender.append("class", "long-literal collapsed"));
×
425
                showMoreLabelLiteral.setVisible(true);
×
426
            }
427
            if (vL.getLanguage().isPresent()) {
×
428
                model.setObject("\"" + vs + "\"");
×
429
                languageModel.setObject("(" + Literals.normalizeLanguageTag(vL.getLanguage().get()) + ")");
×
430
                languageComp.setVisible(true);
×
431
            } else if (!vL.getDatatype().equals(XSD.STRING)) {
×
432
                model.setObject("\"" + vs + "\"");
×
433
                datatypeModel.setObject("(" + vL.getDatatype().stringValue().replace(XSD.NAMESPACE, "xsd:") + ")");
×
434
                datatypeComp.setVisible(true);
×
435
            } else {
436
                model.setObject("\"" + vs + "\"");
×
437
            }
438
            if (Utils.looksLikeHtml(vs)) {
×
439
                linkComp.setVisible(false);
×
440
                extraModel.setObject(Utils.sanitizeHtml(vs));
×
441
                extraComp.setEscapeModelStrings(false);
×
442
                extraComp.setVisible(true);
×
443
                showMoreLabelLiteral.setVisible(false);
×
444
                showMoreLabelHTML.setVisible(true);
×
445
            }
446
        }
447
    }
×
448

449
    /**
450
     * Validator class for validating the input.
451
     */
452
    protected class Validator extends InvalidityHighlighting implements IValidator<String> {
453

454
        /**
455
         * Default constructor for Validator.
456
         */
457
        public Validator() {
×
458
        }
×
459

460
        @Override
461
        public void validate(IValidatable<String> s) {
462
            String sv = s.getValue();
×
463
            String p = prefix;
×
464
            if (template.isAutoEscapePlaceholder(iri)) {
×
465
                sv = Utils.urlEncode(sv);
×
466
            }
467
            if (sv.matches("https?://.+")) {
×
468
                p = "";
×
469
            } else if (sv.contains(":")) {
×
470
                s.error(new ValidationError("Colon character is not allowed in postfix"));
×
471
            }
472
            String iriString = p + sv;
×
473
            if (iriString.matches("[^:# ]+")) {
×
474
                p = LocalUri.PREFIX;
×
475
                iriString = p + sv;
×
476
            }
477
            try {
478
                ParsedIRI piri = new ParsedIRI(iriString);
×
479
                if (!piri.isAbsolute()) {
×
480
                    s.error(new ValidationError("IRI not well-formed"));
×
481
                }
482
                if (p.isEmpty() && !Utils.isLocalURI(sv) && !sv.matches("https?://.+")) {
×
483
                    s.error(new ValidationError("Only http(s):// IRIs are allowed here"));
×
484
                }
485
            } catch (URISyntaxException ex) {
×
486
                s.error(new ValidationError("IRI not well-formed"));
×
487
            }
×
488
            String regex = template.getRegex(iri);
×
489
            if (regex != null) {
×
490
                if (!sv.matches(regex)) {
×
491
                    s.error(new ValidationError("Value '" + sv + "' doesn't match the pattern '" + regex + "'"));
×
492
                }
493
            }
494
            if (template.isRestrictedChoicePlaceholder(iri)) {
×
495
                if (!restrictedChoice.getPossibleValues().contains(iriString) && !restrictedChoice.hasPossibleRefValues()) {
×
496
                    // not checking the possible ref values can overgenerate, but normally works
497
                    s.error(new ValidationError("Invalid choice"));
×
498
                }
499
            }
500
            if (template.isExternalUriPlaceholder(iri)) {
×
501
                if (!iriString.matches("https?://.+")) {
×
502
                    s.error(new ValidationError("Not an external IRI"));
×
503
                }
504
            }
505
            if (template.isTrustyUriPlaceholder(iri)) {
×
506
                if (!TrustyUriUtils.isPotentialTrustyUri(iriString)) {
×
507
                    s.error(new ValidationError("Not a trusty URI"));
×
508
                }
509
            }
510
            if (iri.equals(NTEMPLATE.CREATOR_PLACEHOLDER) && context.getExistingNanopub() != null) {
×
511
                boolean found = false;
×
512
                for (IRI creator : SimpleCreatorPattern.getCreators(context.getExistingNanopub())) {
×
513
                    if (creator.stringValue().equals(iriString)) {
×
514
                        found = true;
×
515
                        break;
×
516
                    }
517
                }
×
518
                if (!found) {
×
519
                    s.error(new ValidationError("Not a creator of nanopub"));
×
520
                }
521
            }
522
        }
×
523

524
    }
525

526
    /**
527
     * <p>toString.</p>
528
     *
529
     * @return a {@link java.lang.String} object
530
     */
531
    public String toString() {
532
        return "[read-only IRI item: " + iri + "]";
×
533
    }
534

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