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

knowledgepixels / nanodash / 17265760757

27 Aug 2025 11:45AM UTC coverage: 12.362% (-0.04%) from 12.401%
17265760757

push

github

tkuhn
Some minor refactoring and improvements of literal handling

332 of 3834 branches covered (8.66%)

Branch coverage included in aggregate %.

989 of 6852 relevant lines covered (14.43%)

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 org.apache.wicket.AttributeModifier;
4
import org.apache.wicket.markup.html.basic.Label;
5
import org.apache.wicket.markup.html.form.AbstractTextComponent;
6
import org.apache.wicket.markup.html.form.TextField;
7
import org.apache.wicket.markup.html.panel.Panel;
8
import org.apache.wicket.model.IModel;
9
import org.apache.wicket.model.Model;
10
import org.apache.wicket.validation.IValidatable;
11
import org.apache.wicket.validation.IValidator;
12
import org.apache.wicket.validation.ValidationError;
13
import org.eclipse.rdf4j.model.IRI;
14
import org.eclipse.rdf4j.model.Literal;
15
import org.eclipse.rdf4j.model.Value;
16
import org.eclipse.rdf4j.model.base.CoreDatatype.XSD;
17
import org.eclipse.rdf4j.model.util.Literals;
18

19
import com.knowledgepixels.nanodash.Utils;
20
import com.knowledgepixels.nanodash.template.Template;
21
import com.knowledgepixels.nanodash.template.TemplateContext;
22
import com.knowledgepixels.nanodash.template.UnificationException;
23

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

29
    private static final long serialVersionUID = 1L;
30
    private TemplateContext context;
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

37
    /**
38
     * Constructs a LiteralTextfieldItem with the specified ID, IRI, optional flag, and template context.
39
     *
40
     * @param id       the component ID
41
     * @param iri      the IRI associated with this text field
42
     * @param optional whether this field is optional
43
     * @param context  the template context containing models and parameters
44
     */
45
    public LiteralTextfieldItem(String id, final IRI iri, boolean optional, TemplateContext context) {
46
        super(id);
×
47
        this.context = context;
×
48
        final Template template = context.getTemplate();
×
49
        this.iri = iri;
×
50
        regex = template.getRegex(iri);
×
51
        IModel<String> model = 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(new IValidator<String>() {
×
66

67
            private static final long serialVersionUID = 1L;
68

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

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

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

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

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

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

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

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

177
        getTextComponent().setModelObject(v.stringValue());
×
178
    }
×
179

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

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

202
    /**
203
     * {@inheritDoc}
204
     */
205
    @Override
206
    public String toString() {
207
        return "[Literal textfield item]";
×
208
    }
209

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