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

knowledgepixels / nanodash / 17319343703

29 Aug 2025 08:53AM UTC coverage: 12.007% (-0.3%) from 12.355%
17319343703

push

github

tkuhn
Fix forcedGet(...) also catching RuntimeExceptions

330 of 3844 branches covered (8.58%)

Branch coverage included in aggregate %.

949 of 6808 relevant lines covered (13.94%)

0.61 hits per line

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

10.42
src/main/java/com/knowledgepixels/nanodash/component/IriItem.java
1
package com.knowledgepixels.nanodash.component;
2

3
import com.knowledgepixels.nanodash.Utils;
4
import com.knowledgepixels.nanodash.component.StatementItem.RepetitionGroup;
5
import com.knowledgepixels.nanodash.page.ExplorePage;
6
import com.knowledgepixels.nanodash.template.ContextType;
7
import com.knowledgepixels.nanodash.template.Template;
8
import com.knowledgepixels.nanodash.template.TemplateContext;
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.apache.wicket.markup.html.panel.Panel;
14
import org.eclipse.rdf4j.model.IRI;
15
import org.eclipse.rdf4j.model.Value;
16
import org.eclipse.rdf4j.model.ValueFactory;
17
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
18
import org.eclipse.rdf4j.model.vocabulary.PROV;
19
import org.nanopub.vocabulary.NTEMPLATE;
20

21
import java.net.URLDecoder;
22
import java.net.URLEncoder;
23

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

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

31
    private static final long serialVersionUID = 1L;
32

33
    private IRI iri;
34
    private TemplateContext context;
35

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

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

110
        String href = null;
×
111
        if (iriString.startsWith("local:")) {
×
112
            href = "";
×
113
        } else {
114
            href = ExplorePage.MOUNT_PATH + "?id=" + URLEncoder.encode(iriString, UTF_8);
×
115
        }
116
        ExternalLink linkComp = new ExternalLink("link", href, labelString.replaceFirst(" - .*$", ""));
×
117
        if (iri.equals(NTEMPLATE.ASSERTION_PLACEHOLDER)) {
×
118
            linkComp.add(new AttributeAppender("class", " this-assertion "));
×
119
            iri = vf.createIRI("local:assertion");
×
120
        } else if (iri.equals(NTEMPLATE.NANOPUB_PLACEHOLDER)) {
×
121
            linkComp.add(new AttributeAppender("class", " this-nanopub "));
×
122
            iri = vf.createIRI("local:nanopub");
×
123
        }
124
        add(linkComp);
×
125
        if (template.isIntroducedResource(iri) || template.isEmbeddedResource(iri)) {
×
126
            linkComp.add(AttributeAppender.append("class", "introduced"));
×
127
        }
128
    }
×
129

130
    private static ValueFactory vf = SimpleValueFactory.getInstance();
3✔
131

132
    // TODO Merge with Utils.getShortNameFromURI
133

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

157
    /**
158
     * {@inheritDoc}
159
     */
160
    @Override
161
    public void removeFromContext() {
162
        // Nothing to be done here.
163
    }
×
164

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

180
    /**
181
     * {@inheritDoc}
182
     */
183
    @Override
184
    public void unifyWith(Value v) throws UnificationException {
185
        if (!isUnifiableWith(v)) throw new UnificationException(v.stringValue());
×
186
        // Nothing left to be done here.
187
    }
×
188

189
    /**
190
     * {@inheritDoc}
191
     */
192
    @Override
193
    public void fillFinished() {
194
    }
×
195

196
    /**
197
     * {@inheritDoc}
198
     */
199
    @Override
200
    public void finalizeValues() {
201
    }
×
202

203
    /**
204
     * {@inheritDoc}
205
     */
206
    @Override
207
    public String toString() {
208
        return "[IRI item: " + iri + "]";
×
209
    }
210

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