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.DEFAULT_ENCODING;
22 import static org.mycore.common.MCRConstants.XLINK_NAMESPACE;
23 import static org.mycore.common.MCRConstants.XSI_NAMESPACE;
24
25 import java.io.IOException;
26 import java.net.URI;
27
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.jdom2.Document;
31 import org.jdom2.Element;
32 import org.mycore.common.MCRCoreVersion;
33 import org.mycore.common.MCRException;
34 import org.mycore.common.config.MCRConfiguration2;
35 import org.mycore.common.content.MCRByteContent;
36 import org.mycore.common.content.MCRURLContent;
37 import org.mycore.common.xml.MCRXMLParserFactory;
38 import org.xml.sax.SAXParseException;
39
40 import com.google.gson.JsonObject;
41
42
43
44
45
46
47
48
49 public abstract class MCRBase {
50
51 protected static final String MCR_ENCODING;
52
53
54 protected Document jdomDocument = null;
55
56
57 protected MCRObjectID mcrId = null;
58
59 protected String mcrVersion = null;
60
61 protected String mcrSchema = null;
62
63 protected final MCRObjectService mcrService;
64
65
66 protected static final String NL;
67
68 protected static final String SLASH;
69
70 protected boolean importMode = false;
71
72
73 private static final Logger LOGGER = LogManager.getLogger();
74
75 static {
76 NL = System.getProperty("line.separator");
77 SLASH = System.getProperty("file.separator");
78
79 MCR_ENCODING = MCRConfiguration2.getString("MCR.Metadata.DefaultEncoding").orElse(DEFAULT_ENCODING);
80 if (LOGGER.isDebugEnabled()) {
81 LOGGER.debug("Encoding = {}", MCR_ENCODING);
82 }
83 }
84
85
86
87
88
89
90
91
92 public MCRBase() throws MCRException {
93 mcrVersion = MCRCoreVersion.getVersion();
94 mcrSchema = "";
95
96
97 mcrService = new MCRObjectService();
98 }
99
100 protected void setFromJDOM(Document doc) {
101 jdomDocument = doc;
102 setUp();
103 }
104
105 protected void setUp() {
106 if (jdomDocument == null) {
107 throw new MCRException("The JDOM document is null or empty.");
108 }
109
110 Element rootElement = jdomDocument.getRootElement();
111 setId(MCRObjectID.getInstance(rootElement.getAttributeValue("ID")));
112 setVersion(rootElement.getAttributeValue("version"));
113 setSchema(rootElement.getAttribute("noNamespaceSchemaLocation", XSI_NAMESPACE).getValue());
114
115
116 Element serviceElement = rootElement.getChild("service");
117 if (serviceElement != null) {
118 mcrService.setFromDOM(serviceElement);
119 }
120 }
121
122
123
124
125
126
127 public final MCRObjectID getId() {
128 return mcrId;
129 }
130
131
132
133
134
135
136 public final String getVersion() {
137 return mcrVersion;
138 }
139
140
141
142
143
144
145 public final String getSchema() {
146 return mcrSchema;
147 }
148
149
150
151
152
153
154
155 public final MCRObjectService getService() {
156 return mcrService;
157 }
158
159
160
161
162
163
164
165
166
167
168 protected final void setFromURI(URI uri) throws MCRException, SAXParseException, IOException {
169 Document jdom = MCRXMLParserFactory.getParser().parseXML(new MCRURLContent(uri.toURL()));
170 setFromJDOM(jdom);
171 }
172
173
174
175
176
177
178
179
180
181
182 protected final void setFromXML(byte[] xml, boolean valid) throws MCRException, SAXParseException {
183 Document jdom = MCRXMLParserFactory.getParser(valid).parseXML(new MCRByteContent(xml));
184 setFromJDOM(jdom);
185 }
186
187
188
189
190
191
192
193 public void setId(MCRObjectID id) {
194 mcrId = id;
195 }
196
197
198
199
200 public final void setVersion(String version) {
201 mcrVersion = version != null ? version : MCRCoreVersion.getVersion();
202 }
203
204
205
206
207
208
209
210 public final void setSchema(String schema) {
211 if (schema == null) {
212 mcrSchema = "";
213
214 return;
215 }
216
217 mcrSchema = schema.trim();
218 }
219
220
221
222
223
224
225
226
227 public Document createXML() throws MCRException {
228 validate();
229 Element elm = new Element(getRootTagName());
230 Document doc = new Document(elm);
231 elm.addNamespaceDeclaration(XSI_NAMESPACE);
232 elm.addNamespaceDeclaration(XLINK_NAMESPACE);
233 elm.setAttribute("noNamespaceSchemaLocation", mcrSchema, XSI_NAMESPACE);
234 elm.setAttribute("ID", mcrId.toString());
235 elm.setAttribute("version", mcrVersion);
236 return doc;
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250 public JsonObject createJSON() {
251 JsonObject object = new JsonObject();
252 object.addProperty("id", mcrId.toString());
253 object.addProperty("version", mcrVersion);
254 return object;
255 }
256
257 protected abstract String getRootTagName();
258
259
260
261
262
263
264
265
266
267
268
269 public boolean isValid() {
270 try {
271 validate();
272 return true;
273 } catch (MCRException exc) {
274 LOGGER.warn("The content of this object '{}' is invalid.", mcrId, exc);
275 }
276 return false;
277 }
278
279
280
281
282
283
284
285
286
287
288
289 public void validate() throws MCRException {
290 if (mcrId == null) {
291 throw new MCRException("MCRObjectID is undefined.");
292 }
293 if (getSchema() == null || getSchema().length() == 0) {
294 throw new MCRException("XML Schema of '" + mcrId + "' is undefined.");
295 }
296 MCRObjectService service = getService();
297 if (service == null) {
298 throw new MCRException("The <service> part of '" + mcrId + "' is undefined.");
299 }
300 try {
301 service.validate();
302 } catch (MCRException exc) {
303 throw new MCRException("The <service> part of '" + mcrId + "' is invalid.", exc);
304 }
305 }
306
307 public boolean isImportMode() {
308 return importMode;
309 }
310
311 public void setImportMode(boolean importMode) {
312 this.importMode = importMode;
313 }
314
315 @Override
316 public String toString() {
317 return this.mcrId.toString();
318 }
319 }