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

knowledgepixels / nanodash / 17380144000

01 Sep 2025 02:12PM UTC coverage: 12.03% (+0.05%) from 11.978%
17380144000

push

github

ashleycaselli
refactor: replace printStackTrace with logger.error for better error handling

330 of 3850 branches covered (8.57%)

Branch coverage included in aggregate %.

958 of 6857 relevant lines covered (13.97%)

0.62 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.Utils;
4
import com.knowledgepixels.nanodash.template.Template;
5
import com.knowledgepixels.nanodash.template.TemplateContext;
6
import com.knowledgepixels.nanodash.template.UnificationException;
7
import net.trustyuri.TrustyUriUtils;
8
import org.apache.wicket.AttributeModifier;
9
import org.apache.wicket.Component;
10
import org.apache.wicket.ajax.AjaxRequestTarget;
11
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
12
import org.apache.wicket.behavior.AttributeAppender;
13
import org.apache.wicket.markup.html.basic.Label;
14
import org.apache.wicket.markup.html.form.TextField;
15
import org.apache.wicket.markup.html.link.ExternalLink;
16
import org.apache.wicket.markup.html.panel.Panel;
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 Panel implements ContextComponent {
37

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

40
    private static final long serialVersionUID = 1L;
41

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

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

110
            private static final long serialVersionUID = 1L;
111

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

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

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

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

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

188
    /**
189
     * Validator class for validating IRI text fields.
190
     */
191
    protected static class Validator extends InvalidityHighlighting implements IValidator<String> {
192

193
        private static final long serialVersionUID = 1L;
194

195
        private IRI iri;
196
        private Template template;
197
        private String prefix;
198
        private TemplateContext context;
199

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

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

273
    }
274

275
    /**
276
     * {@inheritDoc}
277
     */
278
    @Override
279
    public void fillFinished() {
280
    }
×
281

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

297
    /**
298
     * <p>toString.</p>
299
     *
300
     * @return a {@link java.lang.String} object
301
     */
302
    public String toString() {
303
        return "[IRI textfield item: " + iri + "]";
×
304
    }
305

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