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

knowledgepixels / nanodash / 18941803064

30 Oct 2025 01:12PM UTC coverage: 14.299% (+0.1%) from 14.184%
18941803064

push

github

ashleycaselli
refactor(Utils): merge IriItem.getShortNameFromURI with Utils.getShortNameFromURI

512 of 4504 branches covered (11.37%)

Branch coverage included in aggregate %.

1331 of 8385 relevant lines covered (15.87%)

0.71 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/IriItem.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.LocalUri;
4
import com.knowledgepixels.nanodash.Utils;
5
import com.knowledgepixels.nanodash.component.StatementItem.RepetitionGroup;
6
import com.knowledgepixels.nanodash.page.ExplorePage;
7
import com.knowledgepixels.nanodash.template.ContextType;
8
import com.knowledgepixels.nanodash.template.Template;
9
import com.knowledgepixels.nanodash.template.UnificationException;
10
import org.apache.wicket.behavior.AttributeAppender;
11
import org.apache.wicket.markup.html.basic.Label;
12
import org.apache.wicket.markup.html.link.ExternalLink;
13
import org.eclipse.rdf4j.model.IRI;
14
import org.eclipse.rdf4j.model.Value;
15
import org.eclipse.rdf4j.model.ValueFactory;
16
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
17
import org.eclipse.rdf4j.model.vocabulary.PROV;
18
import org.nanopub.vocabulary.NTEMPLATE;
19

20
import java.net.URLEncoder;
21

22
import static java.nio.charset.StandardCharsets.UTF_8;
23

24
/**
25
 * A panel that displays an IRI with a label and a link to explore it.
26
 */
27
public class IriItem extends AbstractContextComponent {
28

29
    private IRI iri;
30

31
    /**
32
     * Constructor for creating an IRI item with a given IRI and repetition group.
33
     *
34
     * @param id              the component ID
35
     * @param parentId        the parent ID (e.g., "subj", "obj")
36
     * @param iriP            the IRI to display
37
     * @param objectPosition  whether this is in the object position of a statement
38
     * @param statementPartId the ID of the statement part this IRI belongs to
39
     * @param rg              the repetition group context
40
     */
41
    public IriItem(String id, String parentId, IRI iriP, boolean objectPosition, IRI statementPartId, RepetitionGroup rg) {
42
        super(id, rg.getContext());
×
43
        this.iri = iriP;
×
44
        final Template template = context.getTemplate();
×
45
        String labelString = null;
×
46
        if (iri.equals(NTEMPLATE.ASSERTION_PLACEHOLDER)) {
×
47
            if (context.getType() == ContextType.ASSERTION) {
×
48
                labelString = "this assertion";
×
49
            } else {
50
                labelString = "the assertion above";
×
51
            }
52
        } else if (iri.equals(NTEMPLATE.NANOPUB_PLACEHOLDER)) {
×
53
            labelString = "this nanopublication";
×
54
        }
55
        if (template.getLabel(iri) != null) {
×
56
            labelString = template.getLabel(iri);
×
57
        } else if (iri.equals(PROV.WAS_ATTRIBUTED_TO)) {
×
58
            // temporary solution until we have full provenance graph support
59
            labelString = "is attributed to";
×
60
        } else if (labelString == null) {
×
61
            labelString = Utils.getShortNameFromURI(iri.stringValue());
×
62
        }
63
        if (!statementPartId.equals(template.getFirstOccurence(iri))) {
×
64
            labelString = labelString.replaceFirst("^[aA]n? ", "the ");
×
65
        }
66
        if (!labelString.isEmpty() && parentId.equals("subj") && !labelString.matches("https?://.*")) {
×
67
            // Capitalize first letter of label if at subject position:
68
            labelString = labelString.substring(0, 1).toUpperCase() + labelString.substring(1);
×
69
        }
70
        labelString = labelString.replaceAll("%I%", "" + rg.getRepeatIndex());
×
71

72
        String iriString = iri.stringValue();
×
73
        String description = "";
×
74
        if (iri.equals(NTEMPLATE.ASSERTION_PLACEHOLDER)) {
×
75
            if (this.context.getExistingNanopub() != null) {
×
76
                iriString = this.context.getExistingNanopub().getAssertionUri().stringValue();
×
77
            } else {
78
                iriString = LocalUri.PREFIX + "assertion";
×
79
            }
80
            description = "This is the identifier for the assertion of this nanopublication.";
×
81
        } else if (iri.equals(NTEMPLATE.NANOPUB_PLACEHOLDER)) {
×
82
            if (this.context.getExistingNanopub() != null) {
×
83
                iriString = this.context.getExistingNanopub().getUri().stringValue();
×
84
            } else {
85
                iriString = LocalUri.PREFIX + "nanopub";
×
86
            }
87
            description = "This is the identifier for this whole nanopublication.";
×
88
        } else if (template.isLocalResource(iri)) {
×
89
            if (this.context.getExistingNanopub() == null) {
×
90
                iriString = iriString.replace(Utils.getUriPrefix(iriString), LocalUri.PREFIX);
×
91
            }
92
        }
93
        if (iriString.startsWith(context.getTemplateId())) {
×
94
            iriString = iriString.replace(context.getTemplateId(), "");
×
95
            if (this.context.getExistingNanopub() != null) {
×
96
                iriString = this.context.getExistingNanopub().getUri().stringValue() + iriString;
×
97
            }
98
            description = "This is a local identifier minted within the nanopublication.";
×
99
        }
100
        if (labelString.contains(" - ")) description = labelString.replaceFirst("^.* - ", "");
×
101
        add(new Label("description", description));
×
102
        add(Utils.getUriLink("uri", iriString));
×
103

104
        String href = null;
×
105
        if (Utils.isLocalURI(iriString)) {
×
106
            href = "";
×
107
        } else {
108
            href = ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(iriString, UTF_8);
×
109
        }
110
        ExternalLink linkComp = new ExternalLink("link", href, labelString.replaceFirst(" - .*$", ""));
×
111
        if (iri.equals(NTEMPLATE.ASSERTION_PLACEHOLDER)) {
×
112
            linkComp.add(new AttributeAppender("class", " this-assertion "));
×
113
            iri = vf.createIRI(LocalUri.PREFIX + "assertion");
×
114
        } else if (iri.equals(NTEMPLATE.NANOPUB_PLACEHOLDER)) {
×
115
            linkComp.add(new AttributeAppender("class", " this-nanopub "));
×
116
            iri = vf.createIRI(LocalUri.PREFIX + "nanopub");
×
117
        }
118
        add(linkComp);
×
119
        if (template.isIntroducedResource(iri) || template.isEmbeddedResource(iri)) {
×
120
            linkComp.add(AttributeAppender.append("class", "introduced"));
×
121
        }
122
    }
×
123

124
    private static ValueFactory vf = SimpleValueFactory.getInstance();
×
125

126
    /**
127
     * {@inheritDoc}
128
     */
129
    @Override
130
    public void removeFromContext() {
131
        // Nothing to be done here.
132
    }
×
133

134
    /**
135
     * {@inheritDoc}
136
     */
137
    @Override
138
    public boolean isUnifiableWith(Value v) {
139
        if (!(v instanceof IRI)) return false;
×
140
        // TODO: Check that template URIs don't have regex characters:
141
        String iriS = iri.stringValue().replaceFirst("^" + context.getTemplateId() + "[#/]?", LocalUri.PREFIX);
×
142
        if (context.isReadOnly()) {
×
143
            return iriS.equals(v.stringValue().replaceFirst("^" + context.getExistingNanopub().getUri() + "[#/]?", LocalUri.PREFIX));
×
144
        } else {
145
            return iriS.equals(v.stringValue());
×
146
        }
147
    }
148

149
    /**
150
     * {@inheritDoc}
151
     */
152
    @Override
153
    public void unifyWith(Value v) throws UnificationException {
154
        if (!isUnifiableWith(v)) throw new UnificationException(v.stringValue());
×
155
        // Nothing left to be done here.
156
    }
×
157

158
    /**
159
     * {@inheritDoc}
160
     */
161
    @Override
162
    public void fillFinished() {
163
    }
×
164

165
    /**
166
     * {@inheritDoc}
167
     */
168
    @Override
169
    public void finalizeValues() {
170
    }
×
171

172
    /**
173
     * {@inheritDoc}
174
     */
175
    @Override
176
    public String toString() {
177
        return "[IRI item: " + iri + "]";
×
178
    }
179

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