1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.classifications2.utils;
20
21 import static org.mycore.common.MCRConstants.XLINK_NAMESPACE;
22
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.SortedSet;
28 import java.util.TreeSet;
29
30 import org.jdom2.Document;
31 import org.jdom2.Element;
32 import org.jdom2.Namespace;
33 import org.mycore.common.MCRException;
34 import org.mycore.datamodel.classifications2.MCRCategory;
35 import org.mycore.datamodel.classifications2.MCRCategoryID;
36 import org.mycore.datamodel.classifications2.MCRLabel;
37 import org.mycore.datamodel.classifications2.impl.MCRCategoryImpl;
38
39 public class MCRXMLTransformer {
40
41 public static MCRCategory getCategory(Document xml) throws URISyntaxException {
42 MCRCategoryImpl category = new MCRCategoryImpl();
43 category.setRoot(category);
44 final String classID = xml.getRootElement().getAttributeValue("ID");
45 category.setLevel(0);
46 category.setId(MCRCategoryID.rootID(classID));
47 setURL(xml.getRootElement(), category);
48
49
50 category.setChildren(new ArrayList<>());
51 buildChildCategories(classID, xml.getRootElement().getChild("categories").getChildren("category"), category);
52 try {
53 category.setLabels(getLabels(xml.getRootElement().getChildren("label")));
54 } catch (NullPointerException | IllegalArgumentException ex) {
55 throw new MCRException("Error while adding labels to classification: " + classID, ex);
56 }
57 return category;
58 }
59
60 public static MCRCategory buildCategory(String classID, Element e, MCRCategory parent) throws URISyntaxException {
61 MCRCategoryImpl category = new MCRCategoryImpl();
62
63 category.setId(new MCRCategoryID(classID, e.getAttributeValue("ID")));
64 category.setRoot(parent.getRoot());
65 category.setChildren(new ArrayList<>());
66 category.setParent(parent);
67 try {
68 category.setLabels(getLabels(e.getChildren("label")));
69 } catch (NullPointerException | IllegalArgumentException ex) {
70 throw new MCRException("Error while adding labels to category: " + category.getId(), ex);
71 }
72 category.setLevel(parent.getLevel() + 1);
73 setURL(e, category);
74 buildChildCategories(classID, e.getChildren("category"), category);
75 return category;
76 }
77
78 private static List<MCRCategory> buildChildCategories(String classID, List<Element> elements, MCRCategory parent)
79 throws URISyntaxException {
80 List<MCRCategory> children = new ArrayList<>(elements.size());
81 for (Object o : elements) {
82 children.add(buildCategory(classID, (Element) o, parent));
83 }
84 return children;
85 }
86
87 public static SortedSet<MCRLabel> getLabels(List<Element> elements)
88 throws NullPointerException, IllegalArgumentException {
89 SortedSet<MCRLabel> labels = new TreeSet<MCRLabel>();
90 for (Element labelElement : elements) {
91 MCRLabel label = getLabel(labelElement);
92 labels.add(label);
93 }
94 return labels;
95 }
96
97 public static MCRLabel getLabel(Element labelElement) throws NullPointerException, IllegalArgumentException {
98 String lang = labelElement.getAttributeValue("lang", Namespace.XML_NAMESPACE);
99 return new MCRLabel(lang, labelElement.getAttributeValue("text"),
100 labelElement.getAttributeValue("description"));
101 }
102
103 private static void setURL(Element e, MCRCategory category) throws URISyntaxException {
104 if (e.getChild("url") != null) {
105 final String uri = e.getChild("url").getAttributeValue("href", XLINK_NAMESPACE);
106 if (uri != null) {
107 category.setURI(new URI(uri));
108 }
109 }
110 }
111
112 }