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.ArrayList;
22 import java.util.List;
23 import java.util.stream.Collectors;
24
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.jdom2.Content;
28 import org.jdom2.Element;
29 import org.mycore.common.MCRException;
30 import org.mycore.common.xml.MCRXMLHelper;
31
32 import com.google.gson.JsonArray;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
35
36
37
38
39
40
41
42
43
44
45
46 public class MCRMetaXML extends MCRMetaDefault {
47 List<Content> content;
48
49 private static final Logger LOGGER = LogManager.getLogger();
50
51
52
53
54
55 public MCRMetaXML() {
56 super();
57 }
58
59 public MCRMetaXML(String subtag, String type, int inherited) throws MCRException {
60 super(subtag, null, type, inherited);
61 }
62
63
64
65
66
67
68
69
70 @Override
71 public void setFromDOM(Element element) {
72 super.setFromDOM(element);
73 content = element.cloneContent();
74 }
75
76 public void addContent(Content content) {
77 if (this.content == null) {
78 this.content = new ArrayList<>();
79 }
80
81 this.content.add(content);
82 }
83
84 public List<Content> getContent() {
85 return content;
86 }
87
88
89
90
91
92
93
94
95
96 @Override
97 public Element createXML() throws MCRException {
98 Element elm = super.createXML();
99 List<Content> addedContent = new ArrayList<>(content.size());
100 cloneListContent(addedContent, content);
101 elm.addContent(addedContent);
102
103 return elm;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 @Override
121 public JsonObject createJSON() {
122 JsonObject json = super.createJSON();
123 JsonArray jsonContentArray = new JsonArray();
124 getContent().forEach(content -> {
125 JsonElement jsonContent = MCRXMLHelper.jsonSerialize(content);
126 if (jsonContent == null) {
127 LOGGER.warn("Unable to serialize xml content '{}' to json.", content);
128 return;
129 }
130 jsonContentArray.add(jsonContent);
131 });
132 json.add("content", jsonContentArray);
133 return json;
134 }
135
136 private static void cloneListContent(List<Content> dest, List<Content> source) {
137 dest.clear();
138 for (Content c : source) {
139 dest.add(c.clone());
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 public void validate() throws MCRException {
155 super.validate();
156 if (content == null) {
157 throw new MCRException(getSubTag() + ": content is null or empty");
158 }
159 }
160
161
162
163
164 @Override
165 public MCRMetaXML clone() {
166 MCRMetaXML clone = (MCRMetaXML) super.clone();
167
168 clone.content = this.content.stream().map(Content::clone).collect(Collectors.toList());
169
170 return clone;
171 }
172
173
174
175
176 @Override
177 public void debug() {
178 if (LOGGER.isDebugEnabled()) {
179 super.debugDefault();
180 LOGGER.debug("Number of contents = \n{}", content.size());
181 }
182 }
183
184 @Override
185 public boolean equals(Object obj) {
186 if (!super.equals(obj)) {
187 return false;
188 }
189 final MCRMetaXML other = (MCRMetaXML) obj;
190 return MCRXMLHelper.deepEqual(this.createXML(), other.createXML());
191 }
192
193 }