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

knowledgepixels / nanodash / 19295211807

12 Nov 2025 11:02AM UTC coverage: 14.151% (+0.5%) from 13.691%
19295211807

push

github

web-flow
Merge pull request #283 from knowledgepixels/282-html-rendering-queryresultlist

Fix the HTML rendering in `QueryResultList`

536 of 4748 branches covered (11.29%)

Branch coverage included in aggregate %.

1376 of 8763 relevant lines covered (15.7%)

0.7 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.User;
6
import com.knowledgepixels.nanodash.Utils;
7
import com.knowledgepixels.nanodash.component.StatementItem.RepetitionGroup;
8
import com.knowledgepixels.nanodash.page.ExplorePage;
9
import com.knowledgepixels.nanodash.page.UserPage;
10
import com.knowledgepixels.nanodash.template.ContextType;
11
import com.knowledgepixels.nanodash.template.Template;
12
import com.knowledgepixels.nanodash.template.UnificationException;
13
import net.trustyuri.TrustyUriUtils;
14
import org.apache.commons.codec.Charsets;
15
import org.apache.wicket.behavior.AttributeAppender;
16
import org.apache.wicket.markup.html.basic.Label;
17
import org.apache.wicket.markup.html.link.ExternalLink;
18
import org.apache.wicket.model.IModel;
19
import org.apache.wicket.model.Model;
20
import org.apache.wicket.validation.IValidatable;
21
import org.apache.wicket.validation.IValidator;
22
import org.apache.wicket.validation.Validatable;
23
import org.apache.wicket.validation.ValidationError;
24
import org.eclipse.rdf4j.common.net.ParsedIRI;
25
import org.eclipse.rdf4j.model.IRI;
26
import org.eclipse.rdf4j.model.Literal;
27
import org.eclipse.rdf4j.model.Value;
28
import org.eclipse.rdf4j.model.ValueFactory;
29
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
30
import org.eclipse.rdf4j.model.util.Literals;
31
import org.eclipse.rdf4j.model.vocabulary.XSD;
32
import org.nanopub.Nanopub;
33
import org.nanopub.SimpleCreatorPattern;
34
import org.nanopub.vocabulary.NTEMPLATE;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

38
import java.net.URISyntaxException;
39
import java.net.URLEncoder;
40
import java.util.HashMap;
41
import java.util.Map;
42

43
/**
44
 * ReadonlyItem is a component that displays a read-only item in the form.
45
 */
46
public class ReadonlyItem extends AbstractContextComponent {
47

48
    private static final int LONG_LITERAL_LENGTH = 100;
49
    private static final Logger logger = LoggerFactory.getLogger(ReadonlyItem.class);
×
50
    private static final ValueFactory vf = SimpleValueFactory.getInstance();
×
51

52
    private IModel<String> model;
53
    private String prefix;
54
    private ExternalLink linkComp;
55
    private Label extraComp, languageComp, datatypeComp;
56
    private IModel<String> extraModel, languageModel, datatypeModel;
57
    private IRI iri;
58
    private RestrictedChoice restrictedChoice;
59
    private Label showMoreLabelLiteral, showMoreLabelHTML;
60
    private final Template template;
61

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

85
        final Map<String, String> foafNameMap;
86
        if (context.getExistingNanopub() == null) {
×
87
            foafNameMap = new HashMap<>();
×
88
        } else {
89
            foafNameMap = Utils.getFoafNameMap(context.getExistingNanopub());
×
90
        }
91

92
        prefix = template.getPrefix(iri);
×
93
        if (prefix == null) prefix = "";
×
94
        if (template.isRestrictedChoicePlaceholder(iri)) {
×
95
            restrictedChoice = new RestrictedChoice(iri, context);
×
96
        }
97
        add(new Label("prefix", new Model<String>() {
×
98

99
            @Override
100
            public String getObject() {
101
                String prefixLabel = template.getPrefixLabel(iri);
×
102
                String v = getFullValue();
×
103
                if (prefixLabel == null || User.isUser(v) || foafNameMap.containsKey(v)) {
×
104
                    return "";
×
105
                } else {
106
                    if (!prefixLabel.isEmpty() && parentId.equals("subj") && !prefixLabel.matches("https?://.*")) {
×
107
                        // Capitalize first letter of label if at subject position:
108
                        prefixLabel = prefixLabel.substring(0, 1).toUpperCase() + prefixLabel.substring(1);
×
109
                    }
110
                    return prefixLabel;
×
111
                }
112
            }
113

114
        }));
115

116
        linkComp = new ExternalLink("link", new Model<String>() {
×
117

118
            @Override
119
            public String getObject() {
120
                String obj = getFullValue();
×
121
                if (obj == null) return "";
×
122
                if (obj.equals(LocalUri.of("nanopub").stringValue())) {
×
123
                    if (context.getExistingNanopub() != null) {
×
124
                        obj = context.getExistingNanopub().getUri().stringValue();
×
125
                        return ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(obj, Charsets.UTF_8);
×
126
                    } else {
127
                        return "";
×
128
                    }
129
                } else if (obj.equals(LocalUri.of("assertion").stringValue())) {
×
130
                    if (context.getExistingNanopub() != null) {
×
131
                        obj = context.getExistingNanopub().getAssertionUri().stringValue();
×
132
                        return ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(obj, Charsets.UTF_8);
×
133
                    } else {
134
                        return "";
×
135
                    }
136
                } else if (User.isUser(obj)) {
×
137
                    return UserPage.MOUNT_PATH + "?id=" + URLEncoder.encode(obj, Charsets.UTF_8);
×
138
                } else if (obj.matches("https?://.+")) {
×
139
                    return ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(obj, Charsets.UTF_8);
×
140
                } else {
141
                    return "";
×
142
                }
143
            }
144

145
        }, new Model<String>() {
×
146

147
            @Override
148
            public String getObject() {
149
                String obj = getFullValue();
×
150
                if (obj != null && obj.matches("https?://.+")) {
×
151
                    IRI objIri = vf.createIRI(obj);
×
152
                    if (iri.equals(NTEMPLATE.CREATOR_PLACEHOLDER)) {
×
153
                        // TODO We might want to introduce a "(you)" flag here at some point
154
                        return User.getShortDisplayName(objIri);
×
155
                    } else if (isAssertionValue(objIri)) {
×
156
                        if (context.getType() == ContextType.ASSERTION) {
×
157
                            return "this assertion";
×
158
                        } else {
159
                            return "the assertion above";
×
160
                        }
161
                    } else if (isNanopubValue(objIri)) {
×
162
                        return "this nanopublication";
×
163
                    } else if (User.isUser(obj)) {
×
164
                        return User.getShortDisplayName(objIri);
×
165
                    } else if (foafNameMap.containsKey(obj)) {
×
166
                        return foafNameMap.get(obj);
×
167
                    }
168
                    return getLabelString(objIri);
×
169
                }
170
                return obj;
×
171
            }
172

173
        });
174
        if (template.isIntroducedResource(iri) || template.isEmbeddedResource(iri)) {
×
175
            linkComp.add(AttributeAppender.append("class", "introduced"));
×
176
        }
177
        add(linkComp);
×
178
        add(new Label("description", new Model<String>() {
×
179

180
            @Override
181
            public String getObject() {
182
                String obj = getFullValue();
×
183
                if (obj != null && obj.matches("https?://.+")) {
×
184
                    IRI objIri = vf.createIRI(obj);
×
185
                    if (isAssertionValue(objIri)) {
×
186
                        return "This is the identifier for the assertion of this nanopublication.";
×
187
                    } else if (isNanopubValue(objIri)) {
×
188
                        return "This is the identifier for this whole nanopublication.";
×
189
                    } else if (context.isReadOnly() && obj.startsWith(context.getExistingNanopub().getUri().stringValue())) {
×
190
                        return "This is a local identifier minted within the nanopublication.";
×
191
                    }
192
                    String labelString = getLabelString(objIri);
×
193
                    String description = "";
×
194
                    if (labelString.contains(" - ")) description = labelString.replaceFirst("^.* - ", "");
×
195
                    return description;
×
196
                } else if (obj != null && obj.startsWith("\"")) {
×
197
                    return "(this is a literal)";
×
198
                }
199
                return "";
×
200
            }
201

202
        }));
203
        Model<String> uriModel = new Model<String>() {
×
204

205
            @Override
206
            public String getObject() {
207
                String obj = getFullValue();
×
208
                if (obj != null && obj.startsWith("\"")) return "";
×
209
                if (isAssertionValue(obj)) {
×
210
                    return getAssertionValue();
×
211
                } else if (isNanopubValue(obj)) {
×
212
                    return getNanopubValue();
×
213
                }
214
                return obj;
×
215
            }
216

217
        };
218
        add(Utils.getUriLink("uri", uriModel));
×
219
        extraModel = Model.of("");
×
220
        extraComp = new Label("extra", extraModel);
×
221
        extraComp.setVisible(false);
×
222
        add(extraComp);
×
223
        languageModel = Model.of("");
×
224
        languageComp = new Label("language", languageModel);
×
225
        languageComp.setVisible(false);
×
226
        add(languageComp);
×
227
        datatypeModel = Model.of("");
×
228
        datatypeComp = new Label("datatype", datatypeModel);
×
229
        datatypeComp.setVisible(false);
×
230
        add(datatypeComp);
×
231

232
        showMoreLabelLiteral = new Label("show-more-literal", "");
×
233
        add(showMoreLabelLiteral);
×
234
        showMoreLabelLiteral.setVisible(false);
×
235

236
        showMoreLabelHTML = new Label("show-more-html", "");
×
237
        add(showMoreLabelHTML);
×
238
        showMoreLabelHTML.setVisible(false);
×
239
    }
×
240

241
    /**
242
     * {@inheritDoc}
243
     */
244
    @Override
245
    public void fillFinished() {
246
        String obj = getFullValue();
×
247
        if (obj != null) {
×
248
            if (isAssertionValue(obj)) {
×
249
                linkComp.add(new AttributeAppender("class", "this-assertion"));
×
250
            } else if (isNanopubValue(obj)) {
×
251
                linkComp.add(new AttributeAppender("class", "this-nanopub"));
×
252
            } else if (context.getExistingNanopub() != null) {
×
253
                Nanopub np = context.getExistingNanopub();
×
254
                if (Utils.getIntroducedIriIds(np).contains(obj) || Utils.getEmbeddedIriIds(np).contains(obj)) {
×
255
                    linkComp.add(AttributeAppender.append("class", "introduced"));
×
256
                }
257
            }
258
        }
259
    }
×
260

261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    public void finalizeValues() {
266
    }
×
267

268
    private String getLabelString(IRI iri) {
269
        if (template.getLabel(iri) != null) {
×
270
            return template.getLabel(iri).replaceFirst(" - .*$", "");
×
271
        } else if (context.getLabel(iri) != null) {
×
272
            return context.getLabel(iri).replaceFirst(" - .*$", "");
×
273
        } else {
274
            return Utils.getShortNameFromURI(iri.stringValue());
×
275
        }
276
    }
277

278
    /**
279
     * {@inheritDoc}
280
     */
281
    @Override
282
    public void removeFromContext() {
283
        // Nothing to be done here.
284
    }
×
285

286
    private String getFullValue() {
287
        String s = model.getObject();
×
288
        if (s == null) return null;
×
289
        if (template.isAutoEscapePlaceholder(iri)) {
×
290
            s = Utils.urlEncode(s);
×
291
        }
292
        if (!prefix.isEmpty()) {
×
293
            s = prefix + s;
×
294
        }
295
        return s;
×
296
    }
297

298
    private boolean isNanopubValue(Object obj) {
299
        if (obj == null) return false;
×
300
        if (obj.toString().equals(LocalUri.of("nanopub").stringValue())) return true;
×
301
        if (context.getExistingNanopub() == null) return false;
×
302
        return obj.toString().equals(context.getExistingNanopub().getUri().stringValue());
×
303
    }
304

305
    private String getNanopubValue() {
306
        if (context.getExistingNanopub() != null) {
×
307
            return context.getExistingNanopub().getUri().stringValue();
×
308
        } else {
309
            return LocalUri.of("nanopub").stringValue();
×
310
        }
311
    }
312

313
    private boolean isAssertionValue(Object obj) {
314
        if (obj == null) return false;
×
315
        if (obj.toString().equals(LocalUri.of("assertion").stringValue())) return true;
×
316
        if (context.getExistingNanopub() == null) return false;
×
317
        return obj.toString().equals(context.getExistingNanopub().getAssertionUri().stringValue());
×
318
    }
319

320
    private String getAssertionValue() {
321
        if (context.getExistingNanopub() != null) {
×
322
            return context.getExistingNanopub().getAssertionUri().stringValue();
×
323
        } else {
324
            return LocalUri.of("assertion").stringValue();
×
325
        }
326
    }
327

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

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

437
    /**
438
     * Validator class for validating the input.
439
     */
440
    protected class Validator extends InvalidityHighlighting implements IValidator<String> {
441

442
        /**
443
         * Default constructor for Validator.
444
         */
445
        public Validator() {
×
446
        }
×
447

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

512
    }
513

514
    /**
515
     * <p>toString.</p>
516
     *
517
     * @return a {@link java.lang.String} object
518
     */
519
    public String toString() {
520
        return "[read-only IRI item: " + iri + "]";
×
521
    }
522

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