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 static org.mycore.common.MCRConstants.XLINK_NAMESPACE;
22
23 import java.util.Objects;
24
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.apache.xml.utils.XMLChar;
28 import org.jdom2.Element;
29 import org.mycore.common.MCRException;
30 import org.mycore.common.MCRUtils;
31
32 import com.google.gson.JsonObject;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class MCRMetaLink extends MCRMetaDefault {
48
49 protected String href;
50
51 protected String label;
52
53 protected String title;
54
55 protected String linktype;
56
57 protected String role;
58
59 protected String from;
60
61 protected String to;
62
63 private static final Logger LOGGER = LogManager.getLogger();
64
65
66
67
68 public MCRMetaLink() {
69 super();
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public MCRMetaLink(String subtag, int inherted) throws MCRException {
85 super(subtag, null, null, inherted);
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public void setReference(String href, String label, String title) throws MCRException {
101 linktype = "locator";
102
103 this.href = MCRUtils.filterTrimmedNotEmpty(href)
104 .orElseThrow(() -> new MCRException("The href value is null or empty."));
105 setXLinkLabel(label);
106 this.title = title;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121 public void setBiLink(String from, String to, String title) throws MCRException {
122 this.from = MCRUtils.filterTrimmedNotEmpty(from)
123 .orElseThrow(() -> new MCRException("The from value is null or empty."));
124 this.to = MCRUtils.filterTrimmedNotEmpty(to)
125 .orElseThrow(() -> new MCRException("The to value is null or empty."));
126 linktype = "arc";
127 this.title = title;
128 }
129
130
131
132
133
134
135 public final String getXLinkType() {
136 return linktype;
137 }
138
139
140
141
142
143
144 public final String getXLinkHref() {
145 return href;
146 }
147
148
149
150
151
152
153 public final String getXLinkLabel() {
154 return label;
155 }
156
157
158
159
160
161
162
163 public final void setXLinkLabel(String label) {
164 if (label != null && !XMLChar.isValidNCName(label)) {
165 throw new MCRException("xlink:label is not a valid NCName: " + label);
166 }
167 this.label = label;
168 }
169
170
171
172
173
174
175 public final String getXLinkTitle() {
176 return title;
177 }
178
179
180
181
182
183
184
185 public final void setXLinkTitle(String title) {
186 this.title = title;
187 }
188
189
190
191
192
193
194 public final String getXLinkFrom() {
195 return from;
196 }
197
198
199
200
201
202
203 public final String getXLinkTo() {
204 return to;
205 }
206
207
208
209
210
211 public void setXLinkRole(String role) {
212 this.role = role;
213 }
214
215
216
217
218
219 public String getXLinkRole() {
220 return role;
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 public final boolean compare(MCRMetaLink input) {
237 if (linktype.equals("locator")) {
238 if (linktype.equals(input.getXLinkType()) && href.equals(input.getXLinkHref())) {
239 return true;
240 }
241 }
242
243 if (linktype.equals("arc")) {
244 return linktype.equals(input.getXLinkType()) && from.equals(input.getXLinkFrom())
245 && to.equals(input.getXLinkTo());
246 }
247
248 return false;
249 }
250
251 @Override
252 public int hashCode() {
253 return Objects.hash(super.hashCode(), from, href, label, linktype, role, title, to);
254 }
255
256 @Override
257 public boolean equals(Object obj) {
258 if (!super.equals(obj)) {
259 return false;
260 }
261 MCRMetaLink other = (MCRMetaLink) obj;
262 if (!Objects.equals(from, other.from)) {
263 return false;
264 } else if (!Objects.equals(href, other.href)) {
265 return false;
266 } else if (!Objects.equals(label, other.label)) {
267 return false;
268 } else if (!Objects.equals(linktype, other.linktype)) {
269 return false;
270 } else if (!Objects.equals(role, other.role)) {
271 return false;
272 } else if (!Objects.equals(title, other.title)) {
273 return false;
274 } else {
275 return Objects.equals(to, other.to);
276 }
277 }
278
279
280
281
282
283
284
285
286
287 @Override
288 public void setFromDOM(Element element) throws MCRException {
289 super.setFromDOM(element);
290
291 String temp = element.getAttributeValue("type", XLINK_NAMESPACE);
292
293 if (temp != null && (temp.equals("locator") || temp.equals("arc"))) {
294 linktype = temp;
295 } else {
296 throw new MCRException("The xlink:type is not locator or arc.");
297 }
298
299 if (linktype.equals("locator")) {
300 String temp1 = element.getAttributeValue("href", XLINK_NAMESPACE);
301 String temp2 = element.getAttributeValue("label", XLINK_NAMESPACE);
302 String temp3 = element.getAttributeValue("title", XLINK_NAMESPACE);
303 setReference(temp1, temp2, temp3);
304 } else {
305 String temp1 = element.getAttributeValue("from", XLINK_NAMESPACE);
306 String temp2 = element.getAttributeValue("to", XLINK_NAMESPACE);
307 String temp3 = element.getAttributeValue("title", XLINK_NAMESPACE);
308 setBiLink(temp1, temp2, temp3);
309 }
310
311 setXLinkRole(element.getAttributeValue("role", XLINK_NAMESPACE));
312 }
313
314
315
316
317
318
319
320
321
322 @Override
323 public Element createXML() throws MCRException {
324 Element elm = super.createXML();
325 elm.setAttribute("type", linktype, XLINK_NAMESPACE);
326
327 if (title != null) {
328 elm.setAttribute("title", title, XLINK_NAMESPACE);
329 }
330
331 if (label != null) {
332 elm.setAttribute("label", label, XLINK_NAMESPACE);
333 }
334
335 if (role != null) {
336 elm.setAttribute("role", role, XLINK_NAMESPACE);
337 }
338
339 if (linktype.equals("locator")) {
340 elm.setAttribute("href", href, XLINK_NAMESPACE);
341 } else {
342 elm.setAttribute("from", from, XLINK_NAMESPACE);
343 elm.setAttribute("to", to, XLINK_NAMESPACE);
344 }
345
346 return elm;
347 }
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374 @Override
375 public JsonObject createJSON() {
376 JsonObject obj = super.createJSON();
377 if (title != null) {
378 obj.addProperty("title", title);
379 }
380 if (label != null) {
381 obj.addProperty("label", label);
382 }
383 if (role != null) {
384 obj.addProperty("role", role);
385 }
386 if (linktype.equals("locator")) {
387 obj.addProperty("href", href);
388 } else {
389 obj.addProperty("from", from);
390 obj.addProperty("to", to);
391 }
392 return obj;
393 }
394
395
396
397
398
399
400
401
402
403
404
405 public void validate() throws MCRException {
406 super.validate();
407 if (label != null && label.length() > 0) {
408 if (!XMLChar.isValidNCName(label)) {
409 throw new MCRException(getSubTag() + ": label is no valid NCName:" + label);
410 }
411 }
412 if (linktype == null) {
413 throw new MCRException(getSubTag() + ": linktype is null");
414 }
415 if (!linktype.equals("locator") && !linktype.equals("arc")) {
416 throw new MCRException(getSubTag() + ": linktype is unsupported: " + linktype);
417 }
418 if (linktype.equals("arc")) {
419 if (from == null || from.length() == 0) {
420 throw new MCRException(getSubTag() + ": from is null or empty");
421 } else if (!XMLChar.isValidNCName(from)) {
422 throw new MCRException(getSubTag() + ": from is no valid NCName:" + from);
423 }
424
425 if (to == null || to.length() == 0) {
426 throw new MCRException(getSubTag() + ": to is null or empty");
427 } else if (!XMLChar.isValidNCName(to)) {
428 throw new MCRException(getSubTag() + ": to is no valid NCName:" + to);
429 }
430 }
431 if (linktype.equals("locator")) {
432 if (href == null || href.length() == 0) {
433 throw new MCRException(getSubTag() + ": href is null or empty");
434 }
435 }
436 }
437
438
439
440
441
442
443
444
445 @Override
446 public MCRMetaLink clone() {
447 MCRMetaLink clone = (MCRMetaLink) super.clone();
448
449 clone.href = this.href;
450 clone.label = this.label;
451 clone.title = this.title;
452 clone.linktype = this.linktype;
453 clone.role = this.role;
454 clone.from = this.from;
455 clone.to = this.to;
456
457 return clone;
458 }
459
460
461
462
463 @Override
464 public final void debug() {
465 if (LOGGER.isDebugEnabled()) {
466 super.debugDefault();
467 LOGGER.debug("Link Type = {}", linktype);
468 LOGGER.debug("Label = {}", label);
469 LOGGER.debug("Title = {}", title);
470 LOGGER.debug("HREF = {}", href);
471 LOGGER.debug("Role = {}", role);
472 LOGGER.debug("From = {}", from);
473 LOGGER.debug("To = {}", to);
474 LOGGER.debug("");
475 }
476 }
477 }