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

knowledgepixels / nanodash / 17263397398

27 Aug 2025 09:52AM UTC coverage: 12.405% (-0.05%) from 12.455%
17263397398

push

github

tkuhn
Support for literal datatypes/language tags in literal placeholders

332 of 3820 branches covered (8.69%)

Branch coverage included in aggregate %.

989 of 6829 relevant lines covered (14.48%)

0.64 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.markup.html.basic.Label;
9
import org.apache.wicket.markup.html.form.AbstractTextComponent;
10
import org.apache.wicket.markup.html.form.TextField;
11
import org.apache.wicket.markup.html.panel.Panel;
12
import org.apache.wicket.model.IModel;
13
import org.apache.wicket.model.Model;
14
import org.apache.wicket.validation.IValidatable;
15
import org.apache.wicket.validation.IValidator;
16
import org.apache.wicket.validation.ValidationError;
17
import org.eclipse.rdf4j.model.IRI;
18
import org.eclipse.rdf4j.model.Literal;
19
import org.eclipse.rdf4j.model.Value;
20
import org.eclipse.rdf4j.model.util.Literals;
21

22
/**
23
 * A component that represents a text field for entering literal values.
24
 */
25
public class LiteralTextfieldItem extends Panel implements ContextComponent {
26

27
    private static final long serialVersionUID = 1L;
28
    private TemplateContext context;
29
    private AbstractTextComponent<String> textfield;
30
    private Label languageComp, datatypeComp;
31
    private IModel<String> languageModel, datatypeModel;
32
    private final String regex;
33
    private final IRI iri;
34

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

65
            private static final long serialVersionUID = 1L;
66

67
            @Override
68
            public void validate(IValidatable<String> s) {
69
                if (regex != null) {
×
70
                    if (!s.getValue().matches(regex)) {
×
71
                        s.error(new ValidationError("Value '" + s.getValue() + "' doesn't match the pattern '" + regex + "'"));
×
72
                    }
73
                }
74
            }
×
75

76
        });
77
        context.getComponentModels().put(iri, tc.getModel());
×
78
        context.getComponents().add(tc);
×
79
        tc.add(new ValueItem.KeepValueAfterRefreshBehavior());
×
80
        tc.add(new InvalidityHighlighting());
×
81
        add(tc);
×
82

83
        languageModel = Model.of("");
×
84
        languageComp = new Label("language", languageModel);
×
85
        datatypeModel = Model.of("");
×
86
        datatypeComp = new Label("datatype", datatypeModel);
×
87
        if (template.getLanguageTag(iri) != null) {
×
88
            languageModel.setObject("(" + template.getLanguageTag(iri) + ")");
×
89
            datatypeComp.setVisible(false);
×
90
        } else if (template.getDatatype(iri) != null && !template.getDatatype(iri).stringValue().equals("http://www.w3.org/2001/XMLSchema#string")) {
×
91
            datatypeModel.setObject("(" + template.getDatatype(iri).stringValue().replace("http://www.w3.org/2001/XMLSchema#", "xsd:") + ")");
×
92
            languageComp.setVisible(false);
×
93
        } else {
94
            datatypeComp.setVisible(false);
×
95
            languageComp.setVisible(false);
×
96
        }
97
        add(languageComp);
×
98
        add(datatypeComp);
×
99
    }
×
100

101
    /**
102
     * <p>initTextComponent.</p>
103
     *
104
     * @param model a {@link org.apache.wicket.model.IModel} object
105
     * @return a {@link org.apache.wicket.markup.html.form.AbstractTextComponent} object
106
     */
107
    protected AbstractTextComponent<String> initTextComponent(IModel<String> model) {
108
        textfield = new TextField<>("textfield", model);
×
109
        return textfield;
×
110
    }
111

112
    /**
113
     * <p>getTextComponent.</p>
114
     *
115
     * @return a {@link org.apache.wicket.markup.html.form.AbstractTextComponent} object
116
     */
117
    protected AbstractTextComponent<String> getTextComponent() {
118
        return textfield;
×
119
    }
120

121
    /**
122
     * {@inheritDoc}
123
     */
124
    @Override
125
    public void removeFromContext() {
126
        context.getComponents().remove(getTextComponent());
×
127
    }
×
128

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

158
    /**
159
     * {@inheritDoc}
160
     */
161
    @Override
162
    public void unifyWith(Value v) throws UnificationException {
163
        if (v == null) return;
×
164
        if (!isUnifiableWith(v)) throw new UnificationException(v.stringValue());
×
165
        Literal vL = (Literal) v;
×
166
        getTextComponent().setModelObject(vL.stringValue());
×
167
        if (vL.getLanguage().isPresent()) {
×
168
            languageModel.setObject("(" + vL.getLanguage().get().toLowerCase() + ")");
×
169
            languageComp.setVisible(true);
×
170
        } else if (!vL.getDatatype().stringValue().equals("http://www.w3.org/2001/XMLSchema#string")) {
×
171
            datatypeModel.setObject("(" + vL.getDatatype().stringValue().replace("http://www.w3.org/2001/XMLSchema#", "xsd:") + ")");
×
172
            datatypeComp.setVisible(true);
×
173
        }
174

175
        getTextComponent().setModelObject(v.stringValue());
×
176
    }
×
177

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

185
    /**
186
     * {@inheritDoc}
187
     */
188
    @Override
189
    public void finalizeValues() {
190
        Value defaultValue = context.getTemplate().getDefault(iri);
×
191
        if (isUnifiableWith(defaultValue)) {
×
192
            try {
193
                unifyWith(defaultValue);
×
194
            } catch (UnificationException ex) {
×
195
                ex.printStackTrace();
×
196
            }
×
197
        }
198
    }
×
199

200
    /**
201
     * {@inheritDoc}
202
     */
203
    @Override
204
    public String toString() {
205
        return "[Literal textfield item]";
×
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