0
|
1 package de.mpiwg.itgroup.eSciDoc.utils;
|
|
2 import java.io.ByteArrayInputStream;
|
|
3 import java.io.File;
|
|
4 import java.io.FilenameFilter;
|
|
5 import java.io.IOException;
|
|
6 import java.io.InputStream;
|
|
7 import java.io.StringBufferInputStream;
|
|
8 import java.io.StringWriter;
|
|
9 import java.io.UnsupportedEncodingException;
|
|
10 import java.io.Writer;
|
|
11 import java.net.URL;
|
|
12 import java.nio.ByteBuffer;
|
|
13 import java.util.ArrayList;
|
|
14 import java.util.HashMap;
|
|
15 import java.util.Iterator;
|
|
16 import java.util.List;
|
|
17 import java.util.Set;
|
|
18
|
|
19
|
|
20
|
|
21 import org.w3c.dom.Attr;
|
|
22
|
|
23 import org.xml.sax.SAXException;
|
|
24
|
|
25 //import com.sun.org.apache.xml.internal.serialize.OutputFormat;
|
|
26 //import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
|
|
27 import org.jdom.Attribute;
|
|
28 import org.jdom.Document;
|
|
29 import org.jdom.Element;
|
|
30 import org.jdom.JDOMException;
|
|
31 import org.jdom.Namespace;
|
|
32 import org.jdom.input.DOMBuilder;
|
|
33 import org.jdom.input.SAXBuilder;
|
|
34 import org.jdom.output.XMLOutputter;
|
|
35 import org.jdom.xpath.XPath;
|
|
36
|
|
37 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
|
|
38 import de.mpiwg.itgroup.eSciDoc.Tools.MyFileNameFilter;
|
3
|
39 import de.mpiwg.itgroup.eSciDoc.exceptions.ESciDocXmlObjectException;
|
0
|
40
|
|
41 public class eSciDocXmlObject {
|
|
42
|
|
43
|
|
44 private Document dom;
|
|
45 private XPath xpath;
|
|
46 private String pid;
|
|
47 private DOMBuilder parser;
|
|
48
|
3
|
49 public eSciDocXmlObject(String pid, InputStream template) throws ESciDocXmlObjectException {
|
0
|
50
|
|
51 SAXBuilder builder = new SAXBuilder();
|
|
52
|
3
|
53 try {
|
|
54 dom = builder.build(template);
|
|
55
|
8
|
56 if (pid!=null){
|
|
57 xpath = EScidocTools.getESciDocXpath("//prop:pid");
|
0
|
58
|
8
|
59 Element test = (Element) xpath.selectSingleNode(dom);
|
3
|
60
|
8
|
61 test.setText(pid);
|
|
62 this.pid=pid;
|
|
63 }
|
3
|
64 } catch (JDOMException e) {
|
|
65 // TODO Auto-generated catch block
|
|
66 e.printStackTrace();
|
|
67 throw new ESciDocXmlObjectException();
|
|
68 } catch (IOException e) {
|
|
69 // TODO Auto-generated catch block
|
|
70 e.printStackTrace();
|
|
71 throw new ESciDocXmlObjectException();
|
|
72 }
|
0
|
73
|
|
74 }
|
|
75
|
3
|
76 public eSciDocXmlObject(URL url) throws ESciDocXmlObjectException {
|
0
|
77
|
|
78 SAXBuilder builder = new SAXBuilder();
|
|
79
|
3
|
80 try {
|
|
81 dom = builder.build(url);
|
|
82
|
|
83 xpath = EScidocTools.getESciDocXpath("//prop:pid");
|
0
|
84
|
3
|
85 Element test = (Element) xpath.selectSingleNode(dom);
|
|
86
|
|
87 this.pid= test.getTextTrim();
|
|
88 } catch (JDOMException e) {
|
|
89 // TODO Auto-generated catch block
|
|
90 e.printStackTrace();
|
|
91 throw new ESciDocXmlObjectException();
|
|
92 } catch (IOException e) {
|
|
93 // TODO Auto-generated catch block
|
|
94 e.printStackTrace();
|
|
95 throw new ESciDocXmlObjectException();
|
|
96 }
|
0
|
97
|
|
98 }
|
|
99
|
3
|
100 public eSciDocXmlObject(Element el) throws ESciDocXmlObjectException {
|
0
|
101
|
3
|
102 try {
|
|
103 dom = new Document((Element)el.clone());
|
|
104
|
|
105 xpath = EScidocTools.getESciDocXpath("//prop:pid");
|
|
106
|
|
107 Element test = (Element) xpath.selectSingleNode(dom);
|
|
108 if (test!=null)
|
|
109 this.pid= test.getTextTrim();
|
|
110 } catch (JDOMException e) {
|
|
111 // TODO Auto-generated catch block
|
|
112 e.printStackTrace();
|
|
113 throw new ESciDocXmlObjectException();
|
|
114 }
|
0
|
115 }
|
|
116
|
3
|
117 public eSciDocXmlObject(InputStream content) throws ESciDocXmlObjectException{
|
2
|
118 SAXBuilder builder = new SAXBuilder();
|
|
119
|
3
|
120 try {
|
|
121 dom = builder.build(content);
|
|
122
|
|
123 xpath = EScidocTools.getESciDocXpath("//prop:pid");
|
2
|
124
|
3
|
125 Element test = (Element) xpath.selectSingleNode(dom);
|
|
126
|
|
127 this.pid= test.getTextTrim();
|
|
128 } catch (JDOMException e) {
|
|
129 // TODO Auto-generated catch block
|
|
130 e.printStackTrace();
|
|
131 throw new ESciDocXmlObjectException();
|
|
132 } catch (IOException e) {
|
|
133 // TODO Auto-generated catch block
|
|
134 e.printStackTrace();
|
|
135 throw new ESciDocXmlObjectException();
|
|
136 }
|
2
|
137
|
|
138 }
|
|
139
|
0
|
140 public void insertDC(Element mdDc) throws Exception{
|
|
141 //NodeList dcList = mdDc.getFirstChild().getChildNodes();
|
|
142 XPath dcXPath = EScidocTools.getESciDocXpath("//dc-md"); //todo check path to dc in escddoc templates
|
|
143
|
|
144 Element dcContainer = (Element) dcXPath.selectSingleNode(dom);
|
|
145
|
|
146 if (dcContainer==null)
|
|
147 throw new Exception();
|
|
148
|
|
149 for(Object ct: mdDc.getChildren())
|
|
150 {
|
|
151 if (Element.class.isInstance(ct)){
|
|
152 Element e = (Element) ct;
|
|
153 String name = e.getName();
|
|
154 Element content = new Element(name, EScidocTools.DC);
|
|
155 content.setText(e.getText());
|
|
156 dcContainer.addContent(content);
|
|
157 }
|
|
158 }
|
|
159 }
|
|
160
|
|
161 public void insertDC(HashMap<String,String> dc) throws Exception{
|
|
162 Set<String> dcList = dc.keySet();
|
|
163
|
|
164 XPath dcXPath = EScidocTools.getESciDocXpath("//dc-md"); //todo check path to dc in escddoc templates
|
|
165
|
|
166 Element dcContainer = (Element) dcXPath.selectSingleNode(dom);
|
|
167
|
|
168 for (String name : dcList){
|
|
169 Element newChild = new Element(name,EScidocTools.DC);
|
|
170 newChild.setText(dc.get(name));
|
|
171 dcContainer.addContent(newChild);
|
|
172 }
|
|
173
|
|
174 }
|
|
175
|
|
176 public void insertMeta(Element meta) throws Exception{
|
|
177 if (meta==null)
|
|
178 return;
|
|
179
|
|
180 XPath xp = EScidocTools.getESciDocXpath("//metadata-records:md-record[@name='mpiwg-index_meta']");
|
|
181 Element dcContainer = (Element) xp.selectSingleNode(dom);
|
|
182
|
|
183 dcContainer.addContent((Element)meta.clone());
|
|
184
|
|
185 }
|
|
186
|
|
187
|
|
188 public void setTitle(String title) throws Exception{
|
|
189 XPath xp = EScidocTools.getESciDocXpath("//foxml:xmlContent/oai_dc:dc");
|
|
190 Element dcContainer = (Element) xp.selectSingleNode(dom);
|
|
191
|
|
192
|
|
193
|
|
194 Element newChild = new Element("title",EScidocTools.DC);
|
|
195 newChild.setText(title);
|
|
196
|
|
197
|
|
198 dcContainer.addContent(newChild);
|
|
199
|
|
200
|
|
201
|
|
202
|
|
203 }
|
|
204
|
|
205 public void setReferenceFolder(String folderpath) throws Exception{
|
|
206 XPath xp = EScidocTools.getESciDocXpath("//mpiwg:bundle/mpiwg:bundle-root/@path");
|
|
207 Element container = (Element) xp.selectSingleNode(dom);
|
|
208
|
|
209 container.setText(folderpath);
|
|
210
|
|
211 }
|
|
212
|
|
213 public void setVLPId(String id) throws Exception{
|
|
214 XPath xp = EScidocTools.getESciDocXpath("//vlp:admin/vlp:identifier");
|
|
215 Element container = (Element) xp.selectSingleNode(dom);
|
|
216
|
|
217 container.setText(id);
|
|
218 }
|
|
219 /**
|
|
220 * @param file Pfad mit den Grundordner des Dokument
|
|
221 * @throws Exception
|
|
222 */
|
|
223 public void generateXMLIndex(File file) throws Exception{
|
|
224
|
|
225
|
|
226 File[] files = file.listFiles();
|
|
227
|
|
228 //first try to find the image path
|
|
229 File imagefolder = null;
|
|
230 for (File f: files){
|
|
231
|
|
232 if (f.getName().equals("pageimg"))
|
|
233 imagefolder = f;
|
|
234 else if (f.getName().equals("pages"))
|
|
235 imagefolder = f;
|
|
236 }
|
|
237 if (imagefolder==null)
|
|
238 return;
|
|
239
|
|
240 FilenameFilter filter = new MyFileNameFilter();
|
|
241
|
|
242 ArrayList<ArrayList<String>> tupels = new ArrayList<ArrayList<String>>();
|
|
243 ArrayList<String> tmp;
|
|
244 for (File image : imagefolder.listFiles(filter))
|
|
245 {
|
|
246 tmp = new ArrayList<String>();
|
|
247
|
|
248 tmp.add(calculateName(image));
|
|
249 tmp.add(image.getName());
|
|
250
|
|
251 tupels.add(tmp);
|
|
252 }
|
|
253 generateXMLIndex(tupels);
|
|
254 }
|
|
255
|
|
256 private void generateXMLIndex(ArrayList<ArrayList<String>> tupels) throws Exception {
|
|
257 // DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
258 //factory.setNamespaceAware(true);
|
|
259 //DocumentBuilder db =factory.newDocumentBuilder();
|
|
260
|
|
261 XPath xp = EScidocTools.getESciDocXpath("//vlp:index");
|
|
262 Element index = (Element) xp.selectSingleNode(dom);
|
|
263
|
|
264
|
|
265 for (ArrayList<String> tuple: tupels ){
|
|
266
|
|
267 Element newChild = new Element("page",EScidocTools.VLP);
|
|
268 newChild.setText(tuple.get(0));
|
|
269
|
|
270 newChild.setAttribute("nr", tuple.get(1));
|
|
271 index.addContent(newChild);
|
|
272
|
|
273 }
|
|
274
|
|
275
|
|
276 }
|
|
277
|
|
278 private String calculateName(File f)
|
|
279 {
|
|
280 String name = f.getName();
|
|
281 String[] splitted = name.split("[.]");
|
|
282 String n = splitted[0];
|
|
283 String name2 = n.substring(1);
|
|
284 char letter = n.charAt(0);
|
|
285 return letter+": "+name2;
|
|
286 };
|
|
287
|
|
288
|
|
289 public void setRelationship(String collection) throws Exception{
|
|
290 XPath xp = EScidocTools.getESciDocXpath("//foxml:datastream[@ID='RELS-EXT']/foxml:datastreamVersion/foxml:xmlContent");
|
|
291 Element xmlContent = (Element) xp.selectSingleNode(dom);
|
|
292
|
|
293
|
|
294 XPath rdfxp = EScidocTools.getESciDocXpath("//rdf:RDF");
|
|
295 Element rdf = (Element) rdfxp.selectSingleNode(dom);
|
|
296
|
|
297
|
|
298 if (rdf==null)
|
|
299 {
|
|
300 rdf = new Element("RDF",EScidocTools.RDF);
|
|
301
|
|
302 xmlContent.addContent(rdf);
|
|
303 }
|
|
304
|
|
305 rdf.setAttribute("rdf", EScidocTools.RDF);
|
|
306 rdf.setAttribute("fedora", EScidocTools.FEDORA);
|
|
307
|
|
308 Element rdfDescription = new Element("Description",EScidocTools.RDF);
|
|
309
|
|
310 Namespace ns = Namespace.getNamespace("rdf",EScidocTools.RDF);
|
|
311 rdfDescription.setAttribute("about","info:fedora/"+pid, ns);
|
|
312
|
|
313 Element rdfMember = new Element("isMemberOfCollection",EScidocTools.FEDORA);
|
|
314 rdfDescription.setAttribute("resource", collection, ns);
|
|
315
|
|
316 rdfDescription.addContent(rdfMember);
|
|
317
|
|
318 rdf.addContent(rdfDescription);
|
|
319
|
|
320
|
|
321
|
|
322
|
|
323 }
|
|
324 public void addWebUrl(String url) throws Exception
|
|
325 {
|
|
326 XPath xp = EScidocTools.getESciDocXpath("//escidocComponents:content[@xlink:title='web_page']");
|
|
327 Element xmlContent = (Element) xp.selectSingleNode(dom);
|
|
328
|
|
329
|
|
330 Namespace ns = Namespace.getNamespace("xlink",EScidocTools.xlink);
|
|
331
|
|
332 xmlContent.setAttribute("href", url,ns);
|
|
333 }
|
|
334
|
|
335 public void addOrigUrlToMPIWGMetaData(String url) throws Exception
|
|
336 {
|
|
337 XPath xp = EScidocTools.getESciDocXpath("//mpiwg:admin/mpiwg:orig-ref");
|
|
338 Element xmlContent = (Element) xp.selectSingleNode(dom);
|
|
339
|
|
340
|
|
341 Namespace ns = Namespace.getNamespace("xlink",EScidocTools.xlink);
|
|
342
|
|
343 xmlContent.setAttribute("href", url,ns);
|
|
344
|
|
345 }
|
|
346
|
|
347 public void addIndexMetaUrl(String url) throws Exception
|
|
348 {
|
|
349 if (url.equals(""))
|
|
350 return;
|
|
351
|
|
352 InputStream componentXML=getClass().getResourceAsStream("/de/mpiwg/itgroup/eSciDoc/xmlTemplates/component.xml");
|
|
353 addComponent(url,componentXML,"index_meta","text/xml","internal-managed");
|
|
354
|
|
355
|
|
356 }
|
|
357
|
|
358
|
|
359
|
3
|
360 /**
|
|
361 * @return Object in eScidoc XML formatting
|
|
362 * @throws IOException
|
|
363 */
|
0
|
364 public String printXML() throws IOException{
|
|
365 XMLOutputter out = new XMLOutputter();
|
|
366
|
|
367 String string = out.outputString(dom);
|
|
368 return string;
|
|
369
|
|
370 }
|
|
371
|
|
372 public void addFullText(String url) throws Exception {
|
|
373 addFullText(url,getClass().getResourceAsStream("/de/mpiwg/itgroup/eSciDoc/xmlTemplates/component.xml"));
|
|
374
|
|
375 }
|
|
376
|
|
377 public void addFullText(String url, InputStream inputStream) throws Exception {
|
|
378 addComponent(url, inputStream,"text","text/xml","internal-managed");
|
|
379 }
|
|
380
|
|
381
|
|
382 public void addComponent(String url, InputStream inputStream, String category, String mimeType, String storageType) throws JDOMException, IOException{
|
|
383
|
|
384 SAXBuilder builder = new SAXBuilder();
|
|
385
|
|
386 Document dom2 = builder.build(inputStream);
|
|
387
|
|
388
|
|
389 XPath xp = EScidocTools.getESciDocXpath("//escidocComponents:properties/prop:content-category");
|
|
390 Element item = (Element)xp.selectSingleNode(dom2);
|
|
391 item.setText(category);
|
|
392
|
|
393 xp = EScidocTools.getESciDocXpath("//escidocComponents:properties/prop:mime-type");
|
|
394 item = (Element)xp.selectSingleNode(dom2);
|
|
395 item.setText(mimeType);
|
|
396
|
|
397 xp = EScidocTools.getESciDocXpath("//escidocComponents:content");
|
|
398 item = (Element)xp.selectSingleNode(dom2);
|
|
399
|
|
400 Namespace ns = Namespace.getNamespace("xlink",EScidocTools.xlink);
|
|
401
|
|
402 item.setAttribute("href", url,ns);
|
|
403 item.setAttribute("storage",storageType);
|
|
404
|
|
405
|
|
406 xp = EScidocTools.getESciDocXpath("//escidocComponents:components");
|
|
407 item = (Element)xp.selectSingleNode(dom);
|
|
408
|
|
409
|
|
410 //Node newNode = dom.importNode(dom2.getFirstChild(), true);
|
|
411 item.addContent((Element)dom2.getRootElement().clone());
|
|
412 }
|
|
413
|
|
414 public void addImageFolderPath(String imageFolder) throws Exception {
|
|
415
|
|
416 XPath xp = EScidocTools.getESciDocXpath("//mpiwg:admin");
|
|
417 Element item = (Element)xp.selectSingleNode(dom);
|
|
418
|
|
419 Element images= new Element("imageFolder",EScidocTools.MPIWG);
|
|
420 images.setText(imageFolder);
|
|
421
|
|
422 item.addContent(images);
|
|
423
|
|
424
|
|
425 }
|
|
426
|
|
427 public void addArchiveFolderPath(String archivePath) throws JDOMException {
|
|
428 XPath xp = EScidocTools.getESciDocXpath("//mpiwg:admin");
|
|
429 Element item = (Element)xp.selectSingleNode(dom);
|
|
430
|
|
431 Element images= new Element("archivePath",EScidocTools.MPIWG);
|
|
432 images.setText(archivePath);
|
|
433
|
|
434 item.addContent(images);
|
|
435
|
|
436 }
|
|
437
|
|
438 public Boolean upDateFromXML(String xml) {
|
|
439 SAXBuilder builder = new SAXBuilder();
|
|
440
|
|
441
|
|
442 ByteArrayInputStream in;
|
|
443 try {
|
|
444 in = new ByteArrayInputStream(xml.getBytes("utf-8"));
|
|
445
|
|
446 dom = builder.build(in);
|
|
447
|
|
448 } catch (UnsupportedEncodingException e) {
|
|
449 // TODO Auto-generated catch block
|
|
450 e.printStackTrace();
|
|
451 return false;
|
|
452 } catch (JDOMException e) {
|
|
453 // TODO Auto-generated catch block
|
|
454 e.printStackTrace();
|
|
455 return false;
|
|
456 } catch (IOException e) {
|
|
457 // TODO Auto-generated catch block
|
|
458 e.printStackTrace();
|
|
459 return false;
|
|
460 }
|
|
461
|
|
462 return true;
|
|
463
|
|
464
|
|
465
|
|
466 }
|
|
467
|
|
468 public void addDisplayUrl(String link) throws JDOMException, IOException {
|
|
469
|
|
470 if (link==null || link.equals(""))
|
|
471 return;
|
|
472
|
|
473 InputStream componentXML=getClass().getResourceAsStream("/de/mpiwg/itgroup/eSciDoc/xmlTemplates/component.xml");
|
|
474 addComponent(link,componentXML,"display_url","text/html","external-url");
|
|
475
|
|
476
|
|
477
|
|
478
|
|
479
|
|
480 }
|
|
481
|
|
482 public String getESciDocId() throws JDOMException {
|
|
483
|
|
484 Element el = dom.getRootElement();
|
|
485
|
|
486 XPath xp = EScidocTools.getESciDocXpath("./@xlink:href");
|
|
487 Attribute href = (Attribute) xp.selectSingleNode(el);
|
8
|
488
|
|
489
|
|
490 return href.getValue();
|
|
491
|
|
492 }
|
|
493
|
|
494 public String getESciDocObjId() throws JDOMException {
|
|
495
|
|
496 Element el = dom.getRootElement();
|
|
497
|
|
498 XPath xp = EScidocTools.getESciDocXpath("./@objid");
|
|
499 Attribute href = (Attribute) xp.selectSingleNode(el);
|
|
500
|
|
501
|
|
502 return href.getValue();
|
|
503
|
0
|
504 }
|
|
505
|
|
506 public void setContentModel(String href) throws JDOMException {
|
|
507
|
|
508 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:properties/srel:content-model/@xlink:href");
|
|
509 Attribute item = (Attribute)xp.selectSingleNode(dom);
|
|
510 item.setValue(href);
|
|
511
|
|
512
|
|
513 }
|
|
514
|
|
515 public void setContext(String context) throws JDOMException {
|
|
516 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:properties/srel:context/@xlink:href");
|
|
517 Attribute item = (Attribute)xp.selectSingleNode(dom);
|
|
518 item.setValue(context);
|
|
519
|
|
520
|
|
521 }
|
|
522
|
|
523 public Document getDocument(){
|
|
524 return dom;
|
|
525 }
|
|
526
|
3
|
527 public void setLastModificationDate(String lastModificationDateOld) throws ESciDocXmlObjectException {
|
|
528 Attribute item;
|
|
529 try {
|
|
530 XPath xp = EScidocTools.getESciDocXpath(".//escidocItem:item/@last-modification-date");
|
|
531 item = (Attribute)xp.selectSingleNode(dom);
|
8
|
532 if (item==null){
|
|
533 xp = EScidocTools.getESciDocXpath(".//escidocItem:item");
|
|
534 Element element = (Element)xp.selectSingleNode(dom);
|
|
535 element.setAttribute("last-modification-date", lastModificationDateOld);
|
|
536 }
|
|
537 else {
|
|
538 item.setValue(lastModificationDateOld);
|
|
539 }
|
3
|
540
|
|
541 } catch (JDOMException e) {
|
|
542 e.printStackTrace();
|
|
543 throw new ESciDocXmlObjectException();
|
|
544 }
|
8
|
545
|
|
546
|
3
|
547
|
|
548
|
|
549 }
|
|
550
|
0
|
551
|
|
552 }
|