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

knowledgepixels / nanodash / 17267970789

27 Aug 2025 01:20PM UTC coverage: 12.355% (-0.007%) from 12.362%
17267970789

push

github

tkuhn
Add yellow border also for non-placeholder introduced resources

332 of 3838 branches covered (8.65%)

Branch coverage included in aggregate %.

989 of 6854 relevant lines covered (14.43%)

0.64 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.nanopub.SimpleCreatorPattern;
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 Panel implements ContextComponent {
29

30
    private static final long serialVersionUID = 1L;
31

32
    private IRI iri;
33
    private TemplateContext context;
34

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

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

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

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

131
    // TODO Merge with Utils.getShortNameFromURI
132

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

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

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

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

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

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

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

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