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.mediapackage.track;
23  
24  import org.opencastproject.mediapackage.MediaPackageSerializer;
25  import org.opencastproject.mediapackage.SubtitleStream;
26  
27  import org.apache.commons.lang3.StringUtils;
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.Node;
31  
32  import java.util.UUID;
33  
34  import javax.xml.bind.annotation.XmlAccessType;
35  import javax.xml.bind.annotation.XmlAccessorType;
36  import javax.xml.bind.annotation.XmlType;
37  import javax.xml.xpath.XPath;
38  import javax.xml.xpath.XPathConstants;
39  import javax.xml.xpath.XPathException;
40  
41  /**
42   * Implementation of {@link org.opencastproject.mediapackage.SubtitleStream}.
43   */
44  @XmlAccessorType(XmlAccessType.NONE)
45  @XmlType(name = "subtitle", namespace = "http://mediapackage.opencastproject.org")
46  public class SubtitleStreamImpl extends AbstractStreamImpl implements SubtitleStream {
47    public SubtitleStreamImpl() {
48      this(UUID.randomUUID().toString());
49    }
50  
51    public SubtitleStreamImpl(String identifier) {
52      super(identifier);
53    }
54  
55    /**
56     * Create a subtitle stream from the XML manifest.
57     *
58     * @param streamIdHint
59     *          stream ID that has to be used if the manifest does not provide one. This is the case when reading an old
60     *          manifest.
61     */
62    public static SubtitleStreamImpl fromManifest(String streamIdHint, Node node, XPath xpath)
63        throws IllegalStateException, XPathException {
64      // Create stream
65      String sid = (String) xpath.evaluate("@id", node, XPathConstants.STRING);
66      if (StringUtils.isEmpty(sid)) {
67        sid = streamIdHint;
68      }
69      SubtitleStreamImpl ss = new SubtitleStreamImpl(sid);
70      partialFromManifest(ss, node, xpath);
71  
72      return ss;
73    }
74  
75    /**
76     * @see org.opencastproject.mediapackage.ManifestContributor#toManifest(org.w3c.dom.Document,
77     *      org.opencastproject.mediapackage.MediaPackageSerializer)
78     */
79    @Override
80    public Node toManifest(Document document, MediaPackageSerializer serializer) {
81      Element node = document.createElement("subtitle");
82      addCommonManifestElements(node, document, serializer);
83      return node;
84    }
85  }