All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencastproject.metadata.mpeg7.Mpeg7CatalogImpl Maven / Gradle / Ivy

There is a newer version: 16.4
Show newest version
/*
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License
 * at:
 *
 *   http://opensource.org/licenses/ecl2.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */


package org.opencastproject.metadata.mpeg7;

import org.opencastproject.util.XmlSafeParser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

/**
 * Implements the mpeg-7 metadata container.
 */
public class Mpeg7CatalogImpl implements Mpeg7Catalog {

  /** Serial version UID */
  private static final long serialVersionUID = 5521535164920498997L;

  /** The multimedia content list */
  private HashMap> multimediaContent = null;

  /** The default element namespace */
  public static final String NS = "mpeg7";

  /**
   * Creates a new mpeg-7 metadata container.
   */
  public Mpeg7CatalogImpl() {
    multimediaContent = new HashMap>();
  }

  /**
   * Creates a new mpeg-7 metadata container from the serialized stream.
   */
  public Mpeg7CatalogImpl(InputStream in) {
    this();
    loadCatalogData(in);
  }

  /**
   * Populates the catalog.
   *
   * @param in
   *          The input stream containing the content
   * @throws IllegalStateException
   *           if reading the catalog fails
   */
  private void loadCatalogData(InputStream in) throws IllegalStateException {
    Mpeg7Parser parser = new Mpeg7Parser(this);
    try {
      parser.parse(in);
    } catch (Exception e) {
      throw new IllegalStateException("Unable to load mpeg-7 catalog data:" + e.getMessage(), e);
    }
  }

  /**
   * Creates a new mpeg-7 metadata container file.
   *
   * @return the new mpeg-7 metadata container
   */
  public static Mpeg7CatalogImpl newInstance() {
    Mpeg7CatalogImpl mpeg7 = new Mpeg7CatalogImpl();
    return mpeg7;
  }

  /**
   * @see org.opencastproject.metadata.mpeg7.Mpeg7#multimediaContent()
   */
  public Iterator> multimediaContent() {
    List> result = new ArrayList>();
    for (MultimediaContent o : multimediaContent.values()) {
      result.add(o);
    }
    return result.iterator();
  }

  /**
   * @see org.opencastproject.metadata.mpeg7.Mpeg7#getMultimediaContent(org.opencastproject.mediapackage.mpeg7.MultimediaContent.Type)
   */
  public MultimediaContent getMultimediaContent(MultimediaContent.Type type) {
    return multimediaContent.get(type);
  }

  /**
   * Saves the mpeg-7 metadata container to disk.
   *
   * @throws ParserConfigurationException
   *           if the xml parser environment is not correctly configured
   * @throws TransformerException
   *           if serialization of the metadata document fails
   * @throws IOException
   *           if an error with catalog file handling occurs
   */
  public Document toXml() throws ParserConfigurationException, TransformerException, IOException {
    Document doc = createDocument();

    // Root element
    Element root = doc.getDocumentElement();

    // Description
    Element descriptionNode = doc.createElement("Description");
    descriptionNode.setAttribute("xsi:type", "ContentEntityType");
    root.appendChild(descriptionNode);

    // MultimediaContent
    for (MultimediaContent mc : multimediaContent.values()) {
      descriptionNode.appendChild(mc.toXml(doc));
    }

    return doc;
  }

  /**
   * Create a DOM representation of the Mpeg-7.
   */
  private Document createDocument() throws ParserConfigurationException {
    Document doc = XmlSafeParser.newDocumentBuilderFactory().newDocumentBuilder().newDocument();
    Element rootElement = doc.createElementNS("urn:mpeg:mpeg7:schema:2001", "Mpeg7");
    rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:mpeg7", "urn:mpeg7:schema:2001");
    rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi",
            "http://www.w3.org/2001/XMLSchema-instance");
    doc.appendChild(rootElement);
    return doc;
  }

  /**
   * @see org.opencastproject.metadata.mpeg7.Mpeg7#addAudioContent(java.lang.String,
   *      org.opencastproject.metadata.mpeg7.MediaTime, org.opencastproject.metadata.mpeg7.MediaLocator)
   */
  @SuppressWarnings("unchecked")
  public Audio addAudioContent(String id, MediaTime time, MediaLocator locator) {
    MultimediaContentImpl




© 2015 - 2024 Weber Informatics LLC | Privacy Policy