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

knowledgepixels / nanodash / 17837235071

18 Sep 2025 05:58PM UTC coverage: 13.87%. Remained the same
17837235071

push

github

tkuhn
chore: Remove serialVersionUIDs

443 of 4022 branches covered (11.01%)

Branch coverage included in aggregate %.

1133 of 7341 relevant lines covered (15.43%)

0.68 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.markup.html.panel.Panel;
18
import org.apache.wicket.model.IModel;
19
import org.apache.wicket.model.Model;
20
import org.apache.wicket.validation.IValidatable;
21
import org.apache.wicket.validation.IValidator;
22
import org.apache.wicket.validation.Validatable;
23
import org.apache.wicket.validation.ValidationError;
24
import org.eclipse.rdf4j.common.net.ParsedIRI;
25
import org.eclipse.rdf4j.model.IRI;
26
import org.eclipse.rdf4j.model.Value;
27
import org.nanopub.SimpleCreatorPattern;
28
import org.nanopub.vocabulary.NTEMPLATE;
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

32
import java.net.URISyntaxException;
33

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

39
    // TODO: Make ContextComponent an abstract class with superclass Panel, and move the common code of the form items there.
40

41
    private String prefix;
42
    private TemplateContext context;
43
    private TextField<String> textfield;
44
    private IRI iri;
45
    private static final Logger logger = LoggerFactory.getLogger(IriTextfieldItem.class);
×
46

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

109
            @Override
110
            protected void onUpdate(AjaxRequestTarget target) {
111
                for (Component c : context.getComponents()) {
×
112
                    if (c == textfield) continue;
×
113
                    if (c.getDefaultModel() == textfield.getModel()) {
×
114
                        c.modelChanged();
×
115
                        target.add(c);
×
116
                    }
117
                }
×
118
            }
×
119

120
        });
121
        if (template.isIntroducedResource(iri) || template.isEmbeddedResource(iri)) {
×
122
            textfield.add(AttributeAppender.append("class", "introduced"));
×
123
        }
124
        add(textfield);
×
125
    }
×
126

127
    /**
128
     * {@inheritDoc}
129
     */
130
    @Override
131
    public void removeFromContext() {
132
        context.getComponents().remove(textfield);
×
133
    }
×
134

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

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

185
    /**
186
     * Validator class for validating IRI text fields.
187
     */
188
    protected static class Validator extends InvalidityHighlighting implements IValidator<String> {
189

190
        private IRI iri;
191
        private Template template;
192
        private String prefix;
193
        private TemplateContext context;
194

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

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

268
    }
269

270
    /**
271
     * {@inheritDoc}
272
     */
273
    @Override
274
    public void fillFinished() {
275
    }
×
276

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

292
    /**
293
     * <p>toString.</p>
294
     *
295
     * @return a {@link java.lang.String} object
296
     */
297
    public String toString() {
298
        return "[IRI textfield item: " + iri + "]";
×
299
    }
300

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