1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.metadata;
20
21 import java.util.Objects;
22
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.jdom2.Element;
26 import org.mycore.common.MCRException;
27 import org.mycore.common.MCRUtils;
28
29 import com.google.gson.JsonObject;
30
31
32
33
34
35
36
37
38
39 public class MCRMetaLangText extends MCRMetaDefault {
40
41 protected String text;
42
43 protected String form;
44
45 private static final Logger LOGGER = LogManager.getLogger();
46
47
48
49
50
51
52 public MCRMetaLangText() {
53 super();
54 text = "";
55 form = "plain";
56 }
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public MCRMetaLangText(String subtag, String lang, String type, int inherted, String form, String text)
78 throws MCRException {
79 super(subtag, lang, type, inherted);
80 this.text = "";
81
82 if (text != null) {
83 this.text = text.trim();
84 }
85
86 this.form = "plain";
87
88 if (form != null) {
89 this.form = form.trim();
90 }
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public final void set(String lang, String type, String form, String text) {
105 setLang(lang);
106 setType(type);
107
108 if (text != null) {
109 this.text = text.trim();
110 }
111
112 this.form = "plain";
113
114 if (form != null) {
115 this.form = form.trim();
116 }
117 }
118
119
120
121
122
123
124
125 public final void setText(String text) {
126 if (text != null) {
127 this.text = text.trim();
128 }
129 }
130
131
132
133
134
135
136
137 public final void setForm(String form) {
138 if (form != null) {
139 text = form.trim();
140 }
141 }
142
143
144
145
146
147
148 public final String getText() {
149 return text;
150 }
151
152
153
154
155
156
157 public final String getForm() {
158 return form;
159 }
160
161
162
163
164
165
166
167
168 @Override
169 public void setFromDOM(Element element) {
170 super.setFromDOM(element);
171
172 String tempText = element.getText();
173
174 if (tempText == null) {
175 tempText = "";
176 }
177
178 text = tempText.trim();
179
180 String tempForm = element.getAttributeValue("form");
181
182 if (tempForm == null) {
183 tempForm = "plain";
184 }
185
186 form = tempForm.trim();
187 }
188
189
190
191
192
193
194
195
196
197 @Override
198 public Element createXML() throws MCRException {
199 Element elm = super.createXML();
200 MCRUtils.filterTrimmedNotEmpty(form)
201 .ifPresent(s -> elm.setAttribute("form", s));
202 elm.addContent(text);
203
204 return elm;
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219 @Override
220 public JsonObject createJSON() {
221 JsonObject obj = super.createJSON();
222 if (getForm() != null) {
223 obj.addProperty("form", getForm());
224 }
225 obj.addProperty("text", getText());
226 return obj;
227 }
228
229
230
231
232
233
234
235
236
237
238
239
240 public void validate() throws MCRException {
241 super.validate();
242 text = MCRUtils.filterTrimmedNotEmpty(text)
243 .orElseThrow(() -> new MCRException(getSubTag() + ": text is null or empty"));
244 form = MCRUtils.filterTrimmedNotEmpty(form)
245 .orElse("plain");
246 }
247
248
249
250
251
252
253
254
255 @Override
256 public MCRMetaLangText clone() {
257 MCRMetaLangText clone = (MCRMetaLangText) super.clone();
258
259 clone.form = this.form;
260 clone.text = this.text;
261
262 return clone;
263 }
264
265
266
267
268 @Override
269 public final void debug() {
270 if (LOGGER.isDebugEnabled()) {
271 super.debugDefault();
272 LOGGER.debug("Format = {}", form);
273 LOGGER.debug("Text = {}", text);
274 LOGGER.debug(" ");
275 }
276 }
277
278
279
280
281
282
283
284 @Override
285 public boolean equals(Object obj) {
286 if (!super.equals(obj)) {
287 return false;
288 }
289 final MCRMetaLangText other = (MCRMetaLangText) obj;
290 return Objects.equals(this.text, other.text) && Objects.equals(this.form, other.form);
291 }
292 }