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

knowledgepixels / nanodash / 18939026354

30 Oct 2025 11:24AM UTC coverage: 14.184% (-0.1%) from 14.315%
18939026354

push

github

ashleycaselli
Merge branch 'master' of github.com:knowledgepixels/nanodash

507 of 4504 branches covered (11.26%)

Branch coverage included in aggregate %.

1322 of 8391 relevant lines covered (15.75%)

0.71 hits per line

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

10.49
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.URLDecoder;
21
import java.net.URLEncoder;
22

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

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

30
    private IRI iri;
31

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

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

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

125
    private static ValueFactory vf = SimpleValueFactory.getInstance();
3✔
126

127
    // TODO Merge with Utils.getShortNameFromURI
128

129
    /**
130
     * <p>getShortNameFromURI.</p>
131
     *
132
     * @param uri a {@link java.lang.String} object
133
     * @return a {@link java.lang.String} object
134
     */
135
    public static String getShortNameFromURI(String uri) {
136
        if (uri.startsWith("https://doi.org/")) return uri.replace("https://doi.org/", "doi:");
4!
137
        if (uri.startsWith("http://dx.doi.org/")) return uri.replace("http://dx.doi.org/", "doi:");
4!
138
        uri = uri.replaceFirst("\\?.*$", "");
5✔
139
        uri = uri.replaceFirst("[/#]$", "");
5✔
140
        uri = uri.replaceFirst("^.*[/#]([^/#]*)[/#]([0-9]+)$", "$1/$2");
5✔
141
        if (uri.contains("#")) {
4!
142
            uri = uri.replaceFirst("^.*#(.*[^0-9].*)$", "$1");
6✔
143
        } else {
144
            uri = uri.replaceFirst("^.*/([^/]*[^0-9/][^/]*)$", "$1");
×
145
        }
146
        uri = uri.replaceFirst("((^|[^A-Za-z0-9\\-_])RA[A-Za-z0-9\\-_]{8})[A-Za-z0-9\\-_]{35}$", "$1");
5✔
147
        uri = uri.replaceFirst("(^|[^A-Za-z0-9\\-_])RA[A-Za-z0-9\\-_]{43}[^A-Za-z0-9\\-_](.+)$", "$2");
5✔
148
        uri = URLDecoder.decode(uri, UTF_8);
4✔
149
        return uri;
2✔
150
    }
151

152
    /**
153
     * {@inheritDoc}
154
     */
155
    @Override
156
    public void removeFromContext() {
157
        // Nothing to be done here.
158
    }
×
159

160
    /**
161
     * {@inheritDoc}
162
     */
163
    @Override
164
    public boolean isUnifiableWith(Value v) {
165
        if (!(v instanceof IRI)) return false;
×
166
        // TODO: Check that template URIs don't have regex characters:
167
        String iriS = iri.stringValue().replaceFirst("^" + context.getTemplateId() + "[#/]?", LocalUri.PREFIX);
×
168
        if (context.isReadOnly()) {
×
169
            return iriS.equals(v.stringValue().replaceFirst("^" + context.getExistingNanopub().getUri() + "[#/]?", LocalUri.PREFIX));
×
170
        } else {
171
            return iriS.equals(v.stringValue());
×
172
        }
173
    }
174

175
    /**
176
     * {@inheritDoc}
177
     */
178
    @Override
179
    public void unifyWith(Value v) throws UnificationException {
180
        if (!isUnifiableWith(v)) throw new UnificationException(v.stringValue());
×
181
        // Nothing left to be done here.
182
    }
×
183

184
    /**
185
     * {@inheritDoc}
186
     */
187
    @Override
188
    public void fillFinished() {
189
    }
×
190

191
    /**
192
     * {@inheritDoc}
193
     */
194
    @Override
195
    public void finalizeValues() {
196
    }
×
197

198
    /**
199
     * {@inheritDoc}
200
     */
201
    @Override
202
    public String toString() {
203
        return "[IRI item: " + iri + "]";
×
204
    }
205

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