1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.metadata.mpeg7;
23
24 import org.opencastproject.metadata.api.CatalogService;
25 import org.opencastproject.util.XmlSafeParser;
26
27 import org.apache.commons.io.output.ByteArrayOutputStream;
28 import org.osgi.service.component.annotations.Component;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33
34 import javax.xml.transform.Transformer;
35 import javax.xml.transform.dom.DOMSource;
36 import javax.xml.transform.stream.StreamResult;
37
38
39
40
41 @Component(
42 immediate = true,
43 service = { Mpeg7CatalogService.class,CatalogService.class },
44 property = {
45 "service.description=Mpeg7 Catalog Service"
46 }
47 )
48 public class Mpeg7CatalogService implements CatalogService<Mpeg7Catalog> {
49
50 public InputStream serialize(Mpeg7Catalog catalog) throws IOException {
51 try {
52 Transformer tf = XmlSafeParser.newTransformerFactory().newTransformer();
53 DOMSource xmlSource = new DOMSource(catalog.toXml());
54 ByteArrayOutputStream out = new ByteArrayOutputStream();
55 tf.transform(xmlSource, new StreamResult(out));
56 return new ByteArrayInputStream(out.toByteArray());
57 } catch (Exception e) {
58 throw new IOException(e);
59 }
60 }
61
62 public Mpeg7Catalog load(InputStream in) throws IOException {
63 if (in == null)
64 throw new IllegalArgumentException("Stream must not be null");
65 return new Mpeg7CatalogImpl(in);
66 }
67
68 public Mpeg7Catalog newInstance() {
69 return new Mpeg7CatalogImpl();
70 }
71
72 }