View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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   * Loads {@link Mpeg7Catalog}s
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  }