• 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/ValueTextfieldItem.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 org.apache.wicket.AttributeModifier;
8
import org.apache.wicket.Component;
9
import org.apache.wicket.ajax.AjaxRequestTarget;
10
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
11
import org.apache.wicket.markup.html.form.TextField;
12
import org.apache.wicket.markup.html.panel.Panel;
13
import org.apache.wicket.model.IModel;
14
import org.apache.wicket.model.Model;
15
import org.apache.wicket.validation.IValidatable;
16
import org.apache.wicket.validation.IValidator;
17
import org.apache.wicket.validation.Validatable;
18
import org.apache.wicket.validation.ValidationError;
19
import org.eclipse.rdf4j.common.net.ParsedIRI;
20
import org.eclipse.rdf4j.model.IRI;
21
import org.eclipse.rdf4j.model.Literal;
22
import org.eclipse.rdf4j.model.Value;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

26
import java.net.URISyntaxException;
27

28
/**
29
 * A text field component for entering values in a template.
30
 */
31
public class ValueTextfieldItem extends Panel implements ContextComponent {
32

33
    private static final long serialVersionUID = 1L;
34

35
    private TemplateContext context;
36
    private TextField<String> textfield;
37
    private IRI iri;
38
    private static final Logger logger = LoggerFactory.getLogger(ValueTextfieldItem.class);
×
39

40
    /**
41
     * Constructor for creating a text field item.
42
     *
43
     * @param id       the component ID
44
     * @param parentId the parent component ID
45
     * @param iriP     the IRI associated with this text field
46
     * @param optional whether the field is optional
47
     * @param context  the template context containing models and components
48
     */
49
    public ValueTextfieldItem(String id, String parentId, final IRI iriP, boolean optional, final TemplateContext context) {
50
        super(id);
×
51
        this.context = context;
×
52
        this.iri = iriP;
×
53
        final Template template = context.getTemplate();
×
54
        IModel<String> model = context.getComponentModels().get(iri);
×
55
        if (model == null) {
×
56
            model = Model.of("");
×
57
            context.getComponentModels().put(iri, model);
×
58
        }
59
        String postfix = Utils.getUriPostfix(iri);
×
60
        if (context.hasParam(postfix)) {
×
61
            model.setObject(context.getParam(postfix));
×
62
        }
63
        textfield = new TextField<>("textfield", model);
×
64
        if (!optional) textfield.setRequired(true);
×
65
        textfield.add(new Validator(iri, template));
×
66
        context.getComponents().add(textfield);
×
67
        if (template.getLabel(iri) != null) {
×
68
            textfield.add(new AttributeModifier("placeholder", template.getLabel(iri)));
×
69
            textfield.setLabel(Model.of(template.getLabel(iri)));
×
70
        }
71
        textfield.add(new OnChangeAjaxBehavior() {
×
72

73
            private static final long serialVersionUID = 1L;
74

75
            @Override
76
            protected void onUpdate(AjaxRequestTarget target) {
77
                for (Component c : context.getComponents()) {
×
78
                    if (c == textfield) continue;
×
79
                    if (c.getDefaultModel() == textfield.getModel()) {
×
80
                        c.modelChanged();
×
81
                        target.add(c);
×
82
                    }
83
                }
×
84
            }
×
85

86
        });
87
        add(textfield);
×
88
    }
×
89

90
    /**
91
     * {@inheritDoc}
92
     */
93
    @Override
94
    public void removeFromContext() {
95
        context.getComponents().remove(textfield);
×
96
    }
×
97

98
    /**
99
     * {@inheritDoc}
100
     */
101
    @Override
102
    public boolean isUnifiableWith(Value v) {
103
        if (v == null) return true;
×
104
        String vs = v.stringValue();
×
105
        if (v instanceof Literal vL) vs = Utils.getSerializedLiteral(vL);
×
106
        if (vs.startsWith("local:")) vs = vs.replaceFirst("^local:", "");
×
107
        Validatable<String> validatable = new Validatable<>(vs);
×
108
        if (v instanceof IRI && context.getTemplate().isLocalResource(iri) && !Utils.isUriPostfix(vs)) {
×
109
            vs = Utils.getUriPostfix(vs);
×
110
        }
111
        new Validator(iri, context.getTemplate()).validate(validatable);
×
112
        if (!validatable.isValid()) {
×
113
            return false;
×
114
        }
115
        if (textfield.getModelObject().isEmpty()) {
×
116
            return true;
×
117
        }
118
        return vs.equals(textfield.getModelObject());
×
119
    }
120

121
    /**
122
     * {@inheritDoc}
123
     */
124
    @Override
125
    public void unifyWith(Value v) throws UnificationException {
126
        if (v == null) return;
×
127
        if (!isUnifiableWith(v)) throw new UnificationException(v.stringValue());
×
128
        String vs = v.stringValue();
×
129
        if (vs.startsWith("local:")) {
×
130
            textfield.setModelObject(vs.replaceFirst("^local:", ""));
×
131
        } else if (v instanceof Literal vL) {
×
132
            textfield.setModelObject(Utils.getSerializedLiteral(vL));
×
133
        } else {
134
            textfield.setModelObject(vs);
×
135
        }
136
    }
×
137

138

139
    /**
140
     * Validator class for validating the text field input.
141
     */
142
    protected static class Validator extends InvalidityHighlighting implements IValidator<String> {
143

144
        private static final long serialVersionUID = 1L;
145

146
//                private IRI iri;
147
//                private Template template;
148

149
        /**
150
         * Constructor for the validator.
151
         *
152
         * @param iri      the IRI associated with the value
153
         * @param template the template containing the context
154
         */
155
        public Validator(IRI iri, Template template) {
×
156
//                        this.iri = iri;
157
//                        this.template = template;
158
        }
×
159

160
        @Override
161
        public void validate(IValidatable<String> s) {
162
            if (s.getValue().startsWith("\"")) {
×
163
                if (!Utils.isValidLiteralSerialization(s.getValue())) {
×
164
                    s.error(new ValidationError("Invalid literal value"));
×
165
                }
166
                return;
×
167
            }
168
            String p = "";
×
169
            if (s.getValue().matches("[^:# ]+")) p = "local:";
×
170
            try {
171
                ParsedIRI piri = new ParsedIRI(p + s.getValue());
×
172
                if (!piri.isAbsolute()) {
×
173
                    s.error(new ValidationError("IRI not well-formed"));
×
174
                }
175
                if (p.isEmpty() && !s.getValue().startsWith("local:") && !(s.getValue()).matches("https?://.+")) {
×
176
                    s.error(new ValidationError("Only http(s):// IRIs are allowed here"));
×
177
                }
178
            } catch (URISyntaxException ex) {
×
179
                s.error(new ValidationError("IRI not well-formed"));
×
180
            }
×
181
        }
×
182

183
    }
184

185
    /**
186
     * {@inheritDoc}
187
     */
188
    @Override
189
    public void fillFinished() {
190
    }
×
191

192
    /**
193
     * {@inheritDoc}
194
     */
195
    @Override
196
    public void finalizeValues() {
197
        Value defaultValue = context.getTemplate().getDefault(iri);
×
198
        if (isUnifiableWith(defaultValue)) {
×
199
            try {
200
                unifyWith(defaultValue);
×
201
            } catch (UnificationException ex) {
×
202
                logger.error("Unification with default value failed: {}", ex.getMessage());
×
203
            }
×
204
        }
205
    }
×
206

207
    /**
208
     * <p>toString.</p>
209
     *
210
     * @return a {@link java.lang.String} object
211
     */
212
    public String toString() {
213
        return "[value textfield item: " + iri + "]";
×
214
    }
215

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