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

knowledgepixels / nanodash / 24880477649

24 Apr 2026 08:39AM UTC coverage: 18.287% (+0.8%) from 17.496%
24880477649

push

github

web-flow
Merge pull request #453 from knowledgepixels/add-root-nanopub-placeholder

feat: support RootNanopubPlaceholder in templates

956 of 6218 branches covered (15.37%)

Branch coverage included in aggregate %.

2251 of 11319 relevant lines covered (19.89%)

2.76 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
        if (model.getObject().isEmpty() && template.isRootNanopubPlaceholder(iri) && context.getReferenceNanopub() == null) {
×
90
            model.setObject(LocalUri.of("nanopub").stringValue());
×
91
        }
92

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

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

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

125
        }));
126

127
        linkComp = new ExternalLink("link", new Model<String>() {
×
128

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

154
        }, new Model<String>() {
×
155

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

194
        });
195
        if (template.isIntroducedResource(iri) || template.isEmbeddedResource(iri)) {
×
196
            linkComp.add(AttributeAppender.append("class", "introduced"));
×
197
        }
198
        add(linkComp);
×
199
        add(new Label("description", new Model<String>() {
×
200

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

223
        }));
224
        Model<String> uriModel = new Model<String>() {
×
225

226
            @Override
227
            public String getObject() {
228
                String obj = getFullValue();
×
229
                if (obj != null && obj.startsWith("\"")) return "";
×
230
                if (isAssertionValue(obj)) {
×
231
                    return getAssertionValue();
×
232
                } else if (isNanopubValue(obj)) {
×
233
                    return getNanopubValue();
×
234
                }
235
                return obj;
×
236
            }
237

238
        };
239
        add(Utils.getUriLink("uri", uriModel));
×
240
        extraModel = Model.of("");
×
241
        extraComp = new Label("extra", extraModel);
×
242
        extraComp.setVisible(false);
×
243
        add(extraComp);
×
244
        languageModel = Model.of("");
×
245
        languageComp = new Label("language", languageModel);
×
246
        languageComp.setVisible(false);
×
247
        add(languageComp);
×
248
        datatypeModel = Model.of("");
×
249
        datatypeComp = new Label("datatype", datatypeModel);
×
250
        datatypeComp.setVisible(false);
×
251
        add(datatypeComp);
×
252

253
        showMoreLabelLiteral = new Label("show-more-literal", "");
×
254
        add(showMoreLabelLiteral);
×
255
        showMoreLabelLiteral.setVisible(false);
×
256

257
        showMoreLabelHTML = new Label("show-more-html", "");
×
258
        add(showMoreLabelHTML);
×
259
        showMoreLabelHTML.setVisible(false);
×
260
    }
×
261

262
    /**
263
     * {@inheritDoc}
264
     */
265
    @Override
266
    public void fillFinished() {
267
        String obj = getFullValue();
×
268
        if (obj != null) {
×
269
            if (isAssertionValue(obj)) {
×
270
                linkComp.add(new AttributeAppender("class", "this-assertion"));
×
271
            } else if (isNanopubValue(obj)) {
×
272
                linkComp.add(new AttributeAppender("class", "this-nanopub"));
×
273
            } else if (context.getExistingNanopub() != null) {
×
274
                Nanopub np = context.getExistingNanopub();
×
275
                if (NanopubUtils.getIntroducedIriIds(np).contains(obj) || NanopubUtils.getEmbeddedIriIds(np).contains(obj)) {
×
276
                    linkComp.add(AttributeAppender.append("class", "introduced"));
×
277
                }
278
            }
279
        }
280
    }
×
281

282
    /**
283
     * {@inheritDoc}
284
     */
285
    @Override
286
    public void finalizeValues() {
287
    }
×
288

289
    private String getLabelString(IRI iri) {
290
        if (template.getLabel(iri) != null) {
×
291
            return template.getLabel(iri).replaceFirst(" - .*$", "");
×
292
        } else if (context.getLabel(iri) != null) {
×
293
            return context.getLabel(iri).replaceFirst(" - .*$", "");
×
294
        } else {
295
            return Utils.getShortNameFromURI(iri.stringValue());
×
296
        }
297
    }
298

299
    /**
300
     * {@inheritDoc}
301
     */
302
    @Override
303
    public void removeFromContext() {
304
        // Nothing to be done here.
305
    }
×
306

307
    private String getFullValue() {
308
        String s = model.getObject();
×
309
        if (s == null) return null;
×
310
        if (template.isAutoEscapePlaceholder(iri)) {
×
311
            s = Utils.urlEncode(s);
×
312
        }
313
        if (!prefix.isEmpty()) {
×
314
            s = prefix + s;
×
315
        }
316
        return s;
×
317
    }
318

319
    private boolean isNanopubValue(Object obj) {
320
        if (obj == null) return false;
×
321
        if (obj.toString().equals(LocalUri.of("nanopub").stringValue())) return true;
×
322
        // A fillSource match means a *prior* nanopub (supersede/derive), not "this" one — don't label it as such.
323
        if (context.getExistingNanopub() == null) return false;
×
324
        return obj.toString().equals(context.getExistingNanopub().getUri().stringValue());
×
325
    }
326

327
    private String getNanopubValue() {
328
        Nanopub ref = context.getReferenceNanopub();
×
329
        if (ref != null) {
×
330
            return ref.getUri().stringValue();
×
331
        } else {
332
            return LocalUri.of("nanopub").stringValue();
×
333
        }
334
    }
335

336
    private boolean isAssertionValue(Object obj) {
337
        if (obj == null) return false;
×
338
        if (obj.toString().equals(LocalUri.of("assertion").stringValue())) return true;
×
339
        // A fillSource match means a *prior* assertion (supersede/derive), not "this" one.
340
        if (context.getExistingNanopub() == null) return false;
×
341
        return obj.toString().equals(context.getExistingNanopub().getAssertionUri().stringValue());
×
342
    }
343

344
    private String getAssertionValue() {
345
        Nanopub ref = context.getReferenceNanopub();
×
346
        if (ref != null) {
×
347
            return ref.getAssertionUri().stringValue();
×
348
        } else {
349
            return LocalUri.of("assertion").stringValue();
×
350
        }
351
    }
352

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

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

462
    /**
463
     * Validator class for validating the input.
464
     */
465
    protected class Validator extends InvalidityHighlighting implements IValidator<String> {
466

467
        /**
468
         * Default constructor for Validator.
469
         */
470
        public Validator() {
×
471
        }
×
472

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

537
    }
538

539
    /**
540
     * <p>toString.</p>
541
     *
542
     * @return a {@link java.lang.String} object
543
     */
544
    public String toString() {
545
        return "[read-only IRI item: " + iri + "]";
×
546
    }
547

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