• 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/LiteralTextfieldItem.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.basic.Label;
12
import org.apache.wicket.markup.html.form.AbstractTextComponent;
13
import org.apache.wicket.markup.html.form.TextField;
14
import org.apache.wicket.model.IModel;
15
import org.apache.wicket.model.Model;
16
import org.apache.wicket.validation.IValidator;
17
import org.apache.wicket.validation.ValidationError;
18
import org.eclipse.rdf4j.model.IRI;
19
import org.eclipse.rdf4j.model.Literal;
20
import org.eclipse.rdf4j.model.Value;
21
import org.eclipse.rdf4j.model.util.Literals;
22
import org.eclipse.rdf4j.model.vocabulary.XSD;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

26
/**
27
 * A component that represents a text field for entering literal values.
28
 */
29
public class LiteralTextfieldItem extends AbstractContextComponent {
30

31
    private AbstractTextComponent<String> textfield;
32
    private Label languageComp, datatypeComp;
33
    private IModel<String> languageModel, datatypeModel;
34
    private final String regex;
35
    private final IRI iri;
36
    private final static Logger logger = LoggerFactory.getLogger(LiteralTextfieldItem.class);
×
37

38
    /**
39
     * Constructs a LiteralTextfieldItem with the specified ID, IRI, optional flag, and template context.
40
     *
41
     * @param id       the component ID
42
     * @param iri      the IRI associated with this text field
43
     * @param optional whether this field is optional
44
     * @param context  the template context containing models and parameters
45
     */
46
    public LiteralTextfieldItem(String id, final IRI iri, boolean optional, TemplateContext context) {
47
        super(id, context);
×
48
        final Template template = context.getTemplate();
×
49
        this.iri = iri;
×
50
        regex = template.getRegex(iri);
×
51
        IModel<String> model = (IModel<String>) context.getComponentModels().get(iri);
×
52
        if (model == null) {
×
53
            model = Model.of("");
×
54
            context.getComponentModels().put(iri, model);
×
55
        }
56
        String postfix = Utils.getUriPostfix(iri);
×
57
        if (context.hasParam(postfix)) {
×
58
            model.setObject(context.getParam(postfix));
×
59
        }
60
        AbstractTextComponent<String> tc = initTextComponent(model);
×
61
        if (!optional) tc.setRequired(true);
×
62
        if (context.getTemplate().getLabel(iri) != null) {
×
63
            tc.add(new AttributeModifier("placeholder", context.getTemplate().getLabel(iri)));
×
64
        }
65
        tc.add((IValidator<String>) s -> {
×
66
            if (regex != null) {
×
67
                if (!s.getValue().matches(regex)) {
×
68
                    s.error(new ValidationError("Value '" + s.getValue() + "' doesn't match the pattern '" + regex + "'"));
×
69
                }
70
            }
71
        });
×
72

73
        tc.add(new OnChangeAjaxBehavior() {
×
74
            @Override
75
            protected void onUpdate(AjaxRequestTarget target) {
76
                for (Component c : context.getComponents()) {
×
77
                    if (c == tc) continue;
×
78
                    if (c.getDefaultModel() == tc.getModel()) {
×
79
                        c.modelChanged();
×
80
                        target.add(c);
×
81
                    }
82
                }
×
83
            }
×
84
        });
85
        context.getComponentModels().put(iri, tc.getModel());
×
86
        context.getComponents().add(tc);
×
87
        tc.add(new ValueItem.KeepValueAfterRefreshBehavior());
×
88
        tc.add(new InvalidityHighlighting());
×
89
        add(tc);
×
90

91
        languageModel = Model.of("");
×
92
        languageComp = new Label("language", languageModel);
×
93
        datatypeModel = Model.of("");
×
94
        datatypeComp = new Label("datatype", datatypeModel);
×
95
        if (template.getLanguageTag(iri) != null) {
×
96
            languageModel.setObject("(" + template.getLanguageTag(iri) + ")");
×
97
            datatypeComp.setVisible(false);
×
98
        } else if (template.getDatatype(iri) != null && !template.getDatatype(iri).equals(XSD.STRING)) {
×
99
            datatypeModel.setObject("(" + template.getDatatype(iri).stringValue().replace(XSD.NAMESPACE, "xsd:") + ")");
×
100
            languageComp.setVisible(false);
×
101
        } else {
102
            datatypeComp.setVisible(false);
×
103
            languageComp.setVisible(false);
×
104
        }
105
        add(languageComp);
×
106
        add(datatypeComp);
×
107
    }
×
108

109
    /**
110
     * <p>initTextComponent.</p>
111
     *
112
     * @param model a {@link org.apache.wicket.model.IModel} object
113
     * @return a {@link org.apache.wicket.markup.html.form.AbstractTextComponent} object
114
     */
115
    protected AbstractTextComponent<String> initTextComponent(IModel<String> model) {
116
        textfield = new TextField<>("textfield", model);
×
117
        return textfield;
×
118
    }
119

120
    /**
121
     * <p>getTextComponent.</p>
122
     *
123
     * @return a {@link org.apache.wicket.markup.html.form.AbstractTextComponent} object
124
     */
125
    protected AbstractTextComponent<String> getTextComponent() {
126
        return textfield;
×
127
    }
128

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

137
    /**
138
     * {@inheritDoc}
139
     */
140
    @Override
141
    public boolean isUnifiableWith(Value v) {
142
        if (v == null) return true;
×
143
        if (v instanceof Literal vL) {
×
144
            if (regex != null && !vL.stringValue().matches(regex)) {
×
145
                return false;
×
146
            }
147
            if (getTextComponent().getModelObject().isEmpty()) {
×
148
                return true;
×
149
            }
150
            String languagetag = context.getTemplate().getLanguageTag(iri);
×
151
            IRI datatype = context.getTemplate().getDatatype(iri);
×
152
            if (languagetag != null) {
×
153
                if (!vL.getLanguage().isPresent() || !Literals.normalizeLanguageTag(vL.getLanguage().get()).equals(languagetag)) {
×
154
                    return false;
×
155
                }
156
            } else if (datatype != null) {
×
157
                if (!vL.getDatatype().equals(datatype)) {
×
158
                    return false;
×
159
                }
160
            }
161
            return vL.stringValue().equals(getTextComponent().getModelObject());
×
162
        }
163
        return false;
×
164
    }
165

166
    /**
167
     * {@inheritDoc}
168
     */
169
    @Override
170
    public void unifyWith(Value v) throws UnificationException {
171
        if (v == null) return;
×
172
        if (!isUnifiableWith(v)) throw new UnificationException(v.stringValue());
×
173
        Literal vL = (Literal) v;
×
174
        getTextComponent().setModelObject(vL.stringValue());
×
175
        if (context.getTemplate().getLanguageTag(iri) == null && vL.getLanguage().isPresent()) {
×
176
            languageModel.setObject("(" + vL.getLanguage().get().toLowerCase() + ")");
×
177
            languageComp.setVisible(true);
×
178
        } else if (context.getTemplate().getDatatype(iri) == null && !vL.getDatatype().equals(XSD.STRING)) {
×
179
            datatypeModel.setObject("(" + vL.getDatatype().stringValue().replace(XSD.NAMESPACE, "xsd:") + ")");
×
180
            datatypeComp.setVisible(true);
×
181
        }
182

183
        getTextComponent().setModelObject(v.stringValue());
×
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("Could not unify with default value.", ex);
×
204
            }
×
205
        }
206
    }
×
207

208
    /**
209
     * {@inheritDoc}
210
     */
211
    @Override
212
    public String toString() {
213
        return "[Literal textfield item]";
×
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