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

knowledgepixels / nanodash / 17760079101

16 Sep 2025 08:42AM UTC coverage: 13.879% (+0.08%) from 13.799%
17760079101

push

github

ashleycaselli
refactor: replace hardcoded local URI prefix with LocalUri constant and checks with Utils methods

443 of 4012 branches covered (11.04%)

Branch coverage included in aggregate %.

1126 of 7293 relevant lines covered (15.44%)

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

27
import java.net.URISyntaxException;
28

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

34
    private static final long serialVersionUID = 1L;
35

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

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

74
            private static final long serialVersionUID = 1L;
75

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

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

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

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

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

139

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

145
        private static final long serialVersionUID = 1L;
146

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

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

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

184
    }
185

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

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

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

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