• 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/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.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 AbstractContextComponent {
32

33
    private TextField<String> textfield;
34
    private IRI iri;
35
    private static final Logger logger = LoggerFactory.getLogger(ValueTextfieldItem.class);
×
36

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

69
            @Override
70
            protected void onUpdate(AjaxRequestTarget target) {
71
                for (Component c : context.getComponents()) {
×
72
                    if (c == textfield) continue;
×
73
                    if (c.getDefaultModel() == textfield.getModel()) {
×
74
                        c.modelChanged();
×
75
                        target.add(c);
×
76
                    }
77
                }
×
78
            }
×
79

80
        });
81
        add(textfield);
×
82
    }
×
83

84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    public void removeFromContext() {
89
        context.getComponents().remove(textfield);
×
90
    }
×
91

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

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

132

133
    /**
134
     * Validator class for validating the text field input.
135
     */
136
    protected static class Validator extends InvalidityHighlighting implements IValidator<String> {
137

138
//                private IRI iri;
139
//                private Template template;
140

141
        /**
142
         * Constructor for the validator.
143
         *
144
         * @param iri      the IRI associated with the value
145
         * @param template the template containing the context
146
         */
147
        public Validator(IRI iri, Template template) {
×
148
//                        this.iri = iri;
149
//                        this.template = template;
150
        }
×
151

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

175
    }
176

177
    /**
178
     * {@inheritDoc}
179
     */
180
    @Override
181
    public void fillFinished() {
182
    }
×
183

184
    /**
185
     * {@inheritDoc}
186
     */
187
    @Override
188
    public void finalizeValues() {
189
        Value defaultValue = context.getTemplate().getDefault(iri);
×
190
        if (isUnifiableWith(defaultValue)) {
×
191
            try {
192
                unifyWith(defaultValue);
×
193
            } catch (UnificationException ex) {
×
194
                logger.error("Unification with default value failed: {}", ex.getMessage());
×
195
            }
×
196
        }
197
    }
×
198

199
    /**
200
     * <p>toString.</p>
201
     *
202
     * @return a {@link java.lang.String} object
203
     */
204
    public String toString() {
205
        return "[value textfield item: " + iri + "]";
×
206
    }
207

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