• 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

0.0
src/main/java/com/knowledgepixels/nanodash/component/IriTextfieldItem.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.template.Template;
6
import com.knowledgepixels.nanodash.template.TemplateContext;
7
import com.knowledgepixels.nanodash.template.UnificationException;
8
import net.trustyuri.TrustyUriUtils;
9
import org.apache.wicket.AttributeModifier;
10
import org.apache.wicket.Component;
11
import org.apache.wicket.ajax.AjaxRequestTarget;
12
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
13
import org.apache.wicket.behavior.AttributeAppender;
14
import org.apache.wicket.markup.html.basic.Label;
15
import org.apache.wicket.markup.html.form.TextField;
16
import org.apache.wicket.markup.html.link.ExternalLink;
17
import org.apache.wicket.model.IModel;
18
import org.apache.wicket.model.Model;
19
import org.apache.wicket.validation.IValidatable;
20
import org.apache.wicket.validation.IValidator;
21
import org.apache.wicket.validation.Validatable;
22
import org.apache.wicket.validation.ValidationError;
23
import org.eclipse.rdf4j.common.net.ParsedIRI;
24
import org.eclipse.rdf4j.model.IRI;
25
import org.eclipse.rdf4j.model.Value;
26
import org.nanopub.SimpleCreatorPattern;
27
import org.nanopub.vocabulary.NTEMPLATE;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

31
import java.net.URISyntaxException;
32

33
/**
34
 * A text field for entering IRIs, with a prefix label and validation.
35
 */
36
public class IriTextfieldItem extends AbstractContextComponent {
37

38
    private String prefix;
39
    private TextField<String> textfield;
40
    private IRI iri;
41
    private static final Logger logger = LoggerFactory.getLogger(IriTextfieldItem.class);
×
42

43
    /**
44
     * Constructor for creating an IRI text field item.
45
     *
46
     * @param id       the component ID
47
     * @param parentId the parent ID (e.g., "subj", "pred", "obj")
48
     * @param iriP     the IRI placeholder for this item
49
     * @param optional whether the field is optional
50
     * @param context  the template context containing models and components
51
     */
52
    public IriTextfieldItem(String id, String parentId, final IRI iriP, boolean optional, final TemplateContext context) {
53
        super(id, context);
×
54
        this.iri = iriP;
×
55
        final Template template = context.getTemplate();
×
56
        IModel<String> model = (IModel<String>) context.getComponentModels().get(iri);
×
57
        if (model == null) {
×
58
            model = Model.of("");
×
59
            context.getComponentModels().put(iri, model);
×
60
        }
61
        String postfix = Utils.getUriPostfix(iri);
×
62
        if (context.hasParam(postfix)) {
×
63
            model.setObject(context.getParam(postfix));
×
64
        }
65
        prefix = template.getPrefix(iri);
×
66
        if (prefix == null) prefix = "";
×
67
        if (template.isLocalResource(iri)) {
×
68
            prefix = Utils.getUriPrefix(iri);
×
69
        }
70
        String prefixLabel = template.getPrefixLabel(iri);
×
71
        Label prefixLabelComp;
72
        if (prefixLabel == null) {
×
73
            prefixLabelComp = new Label("prefix", "");
×
74
            prefixLabelComp.setVisible(false);
×
75
        } else {
76
            if (!prefixLabel.isEmpty() && parentId.equals("subj") && !prefixLabel.matches("https?://.*")) {
×
77
                // Capitalize first letter of label if at subject position:
78
                prefixLabel = prefixLabel.substring(0, 1).toUpperCase() + prefixLabel.substring(1);
×
79
            }
80
            prefixLabelComp = new Label("prefix", prefixLabel);
×
81
        }
82
        add(prefixLabelComp);
×
83
        String prefixTooltip = prefix;
×
84
        if (!prefix.isEmpty()) {
×
85
            prefixTooltip += "...";
×
86
            if (template.isLocalResource(iri)) {
×
87
                prefixTooltip = LocalUri.PREFIX + "...";
×
88
            }
89
        }
90
        add(new ExternalLink("prefixtooltiptext", "", prefixTooltip));
×
91
        textfield = new TextField<>("textfield", model);
×
92
        if (!optional) textfield.setRequired(true);
×
93
        if (template.isLocalResource(iri) || !prefix.isEmpty()) {
×
94
            textfield.add(new AttributeAppender("class", " short"));
×
95
        }
96
        textfield.add(new Validator(iri, template, prefix, context));
×
97
        context.getComponents().add(textfield);
×
98
        if (template.getLabel(iri) != null) {
×
99
            textfield.add(new AttributeModifier("placeholder", template.getLabel(iri).replaceFirst(" - .*$", "")));
×
100
            textfield.setLabel(Model.of(template.getLabel(iri)));
×
101
        }
102
        textfield.add(new OnChangeAjaxBehavior() {
×
103

104
            @Override
105
            protected void onUpdate(AjaxRequestTarget target) {
106
                for (Component c : context.getComponents()) {
×
107
                    if (c == textfield) continue;
×
108
                    if (c.getDefaultModel() == textfield.getModel()) {
×
109
                        c.modelChanged();
×
110
                        target.add(c);
×
111
                    }
112
                }
×
113
            }
×
114

115
        });
116
        if (template.isIntroducedResource(iri) || template.isEmbeddedResource(iri)) {
×
117
            textfield.add(AttributeAppender.append("class", "introduced"));
×
118
        }
119
        add(textfield);
×
120
    }
×
121

122
    /**
123
     * {@inheritDoc}
124
     */
125
    @Override
126
    public void removeFromContext() {
127
        context.getComponents().remove(textfield);
×
128
    }
×
129

130
    /**
131
     * {@inheritDoc}
132
     */
133
    @Override
134
    public boolean isUnifiableWith(Value v) {
135
        if (v == null) return true;
×
136
        if (v instanceof IRI) {
×
137
            String vs = v.stringValue();
×
138
            if (vs.startsWith(prefix)) vs = vs.substring(prefix.length());
×
139
            if (Utils.isLocalURI(vs)) vs = vs.replaceFirst("^" + LocalUri.PREFIX, "");
×
140
            if (context.getTemplate().isAutoEscapePlaceholder(iri)) {
×
141
                vs = Utils.urlDecode(vs);
×
142
            }
143
            Validatable<String> validatable = new Validatable<>(vs);
×
144
            if (context.getTemplate().isLocalResource(iri) && !Utils.isUriPostfix(vs)) {
×
145
                vs = Utils.getUriPostfix(vs);
×
146
            }
147
            new Validator(iri, context.getTemplate(), prefix, context).validate(validatable);
×
148
            if (!validatable.isValid()) {
×
149
                return false;
×
150
            }
151
            if (textfield.getModelObject().isEmpty()) {
×
152
                return true;
×
153
            }
154
            return vs.equals(textfield.getModelObject());
×
155
        }
156
        return false;
×
157
    }
158

159
    /**
160
     * {@inheritDoc}
161
     */
162
    @Override
163
    public void unifyWith(Value v) throws UnificationException {
164
        if (v == null) return;
×
165
        String vs = v.stringValue();
×
166
        if (!isUnifiableWith(v)) throw new UnificationException(vs);
×
167
        if (!prefix.isEmpty() && vs.startsWith(prefix)) {
×
168
            vs = vs.substring(prefix.length());
×
169
        } else if (Utils.isLocalURI(vs)) {
×
170
            vs = vs.replaceFirst("^" + LocalUri.PREFIX, "");
×
171
        } else if (context.getTemplate().isLocalResource(iri) && !Utils.isUriPostfix(vs)) {
×
172
            vs = Utils.getUriPostfix(vs);
×
173
        }
174
        if (context.getTemplate().isAutoEscapePlaceholder(iri)) {
×
175
            vs = Utils.urlDecode(vs);
×
176
        }
177
        textfield.setModelObject(vs);
×
178
    }
×
179

180
    /**
181
     * Validator class for validating IRI text fields.
182
     */
183
    protected static class Validator extends InvalidityHighlighting implements IValidator<String> {
184

185
        private IRI iri;
186
        private Template template;
187
        private String prefix;
188
        private TemplateContext context;
189

190
        /**
191
         * Constructor for creating a validator for an IRI text field.
192
         *
193
         * @param iri      the IRI placeholder for this item
194
         * @param template the template containing validation rules
195
         * @param prefix   the prefix to be used in validation
196
         * @param context  the template context containing models and components
197
         */
198
        public Validator(IRI iri, Template template, String prefix, TemplateContext context) {
×
199
            this.iri = iri;
×
200
            this.template = template;
×
201
            this.prefix = prefix;
×
202
            this.context = context;
×
203
        }
×
204

205
        @Override
206
        public void validate(IValidatable<String> s) {
207
            String sv = s.getValue();
×
208
            String p = prefix;
×
209
            if (template.isAutoEscapePlaceholder(iri)) {
×
210
                sv = Utils.urlEncode(sv);
×
211
            }
212
            if (sv.matches("https?://.+")) {
×
213
                p = "";
×
214
            } else if (sv.contains(":")) {
×
215
                s.error(new ValidationError("Colon character is not allowed in postfix"));
×
216
            }
217
            String iriString = p + sv;
×
218
            if (iriString.matches("[^:# ]+")) {
×
219
                p = LocalUri.PREFIX;
×
220
                iriString = p + sv;
×
221
            }
222
            try {
223
                ParsedIRI piri = new ParsedIRI(iriString);
×
224
                if (!piri.isAbsolute()) {
×
225
                    s.error(new ValidationError("IRI not well-formed"));
×
226
                }
227
                if (p.isEmpty() && !Utils.isLocalURI(sv) && !sv.matches("https?://.+")) {
×
228
                    s.error(new ValidationError("Only http(s):// IRIs are allowed here"));
×
229
                }
230
            } catch (URISyntaxException ex) {
×
231
                s.error(new ValidationError("IRI not well-formed"));
×
232
            }
×
233
            String regex = template.getRegex(iri);
×
234
            if (regex != null) {
×
235
                if (!sv.matches(regex)) {
×
236
                    s.error(new ValidationError("Value '" + sv + "' doesn't match the pattern '" + regex + "'"));
×
237
                }
238
            }
239
            if (template.isExternalUriPlaceholder(iri)) {
×
240
                if (!iriString.matches("https?://.+")) {
×
241
                    s.error(new ValidationError("Not an external IRI"));
×
242
                }
243
            }
244
            if (template.isTrustyUriPlaceholder(iri)) {
×
245
                if (!TrustyUriUtils.isPotentialTrustyUri(iriString)) {
×
246
                    s.error(new ValidationError("Not a trusty URI"));
×
247
                }
248
            }
249
            if (iri.equals(NTEMPLATE.CREATOR_PLACEHOLDER) && context.getExistingNanopub() != null) {
×
250
                boolean found = false;
×
251
                for (IRI creator : SimpleCreatorPattern.getCreators(context.getExistingNanopub())) {
×
252
                    if (creator.stringValue().equals(iriString)) {
×
253
                        found = true;
×
254
                        break;
×
255
                    }
256
                }
×
257
                if (!found) {
×
258
                    s.error(new ValidationError("Not a creator of nanopub"));
×
259
                }
260
            }
261
        }
×
262

263
    }
264

265
    /**
266
     * {@inheritDoc}
267
     */
268
    @Override
269
    public void fillFinished() {
270
    }
×
271

272
    /**
273
     * {@inheritDoc}
274
     */
275
    @Override
276
    public void finalizeValues() {
277
        Value defaultValue = context.getTemplate().getDefault(iri);
×
278
        if (isUnifiableWith(defaultValue)) {
×
279
            try {
280
                unifyWith(defaultValue);
×
281
            } catch (UnificationException ex) {
×
282
                logger.error("Could not unify default value {} with text field {}", defaultValue, this, ex);
×
283
            }
×
284
        }
285
    }
×
286

287
    /**
288
     * <p>toString.</p>
289
     *
290
     * @return a {@link java.lang.String} object
291
     */
292
    public String toString() {
293
        return "[IRI textfield item: " + iri + "]";
×
294
    }
295

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