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

knowledgepixels / nanodash / 17380144000

01 Sep 2025 02:12PM UTC coverage: 12.03% (+0.05%) from 11.978%
17380144000

push

github

ashleycaselli
refactor: replace printStackTrace with logger.error for better error handling

330 of 3850 branches covered (8.57%)

Branch coverage included in aggregate %.

958 of 6857 relevant lines covered (13.97%)

0.62 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
import org.eclipse.rdf4j.model.vocabulary.XSD;
22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

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

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

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

69
            private static final long serialVersionUID = 1L;
70

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

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

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

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

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

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

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

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

179
        getTextComponent().setModelObject(v.stringValue());
×
180
    }
×
181

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

189
    /**
190
     * {@inheritDoc}
191
     */
192
    @Override
193
    public void finalizeValues() {
194
        Value defaultValue = context.getTemplate().getDefault(iri);
×
195
        if (isUnifiableWith(defaultValue)) {
×
196
            try {
197
                unifyWith(defaultValue);
×
198
            } catch (UnificationException ex) {
×
199
                logger.error("Could not unify with default value.", ex);
×
200
            }
×
201
        }
202
    }
×
203

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

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