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  
23  package org.opencastproject.silencedetection.api;
24  
25  import org.opencastproject.util.XmlSafeParser;
26  
27  import org.xml.sax.InputSource;
28  import org.xml.sax.SAXException;
29  
30  import java.io.IOException;
31  import java.io.StringReader;
32  import java.io.StringWriter;
33  import java.util.List;
34  
35  import javax.xml.bind.JAXBContext;
36  import javax.xml.bind.JAXBException;
37  import javax.xml.bind.Marshaller;
38  import javax.xml.bind.Unmarshaller;
39  import javax.xml.bind.annotation.XmlAccessType;
40  import javax.xml.bind.annotation.XmlAccessorType;
41  import javax.xml.bind.annotation.XmlElement;
42  import javax.xml.bind.annotation.XmlElementWrapper;
43  import javax.xml.bind.annotation.XmlRootElement;
44  
45  /**
46   * Wrapper class for holding many {@link MediaSegment}s and implements XML serialization methods.
47   */
48  @XmlRootElement(name = "media-segments")
49  @XmlAccessorType(XmlAccessType.NONE)
50  public class MediaSegments {
51  
52    /** Track ID */
53    @XmlElement(name = "trackId", required = true)
54    private String trackId;
55  
56    /** File path to a media file */
57    @XmlElement(name = "filePath")
58    private String filePath;
59  
60    /** List with media segments, that holds start and stop positions */
61    @XmlElementWrapper(name = "segments", required = true, nillable = false)
62    @XmlElement(name = "segment")
63    private List<MediaSegment> mediaSegments;
64  
65    public MediaSegments() {
66      this(null, null, null);
67    }
68  
69    public MediaSegments(String trackId, String filePath, List<MediaSegment> mediaSegments) {
70      this.trackId = trackId;
71      this.filePath = filePath;
72      this.mediaSegments = mediaSegments;
73    }
74  
75    /**
76     * Returns a list with media segments, that holds start and stop positions or null.
77     * @return list with {@link MediaSegment}s or null
78     */
79    public List<MediaSegment> getMediaSegments() {
80      return mediaSegments;
81    }
82  
83    /**
84     * Returns the Track ID.
85     * @return Track ID
86     */
87    public String getTrackId() {
88      return trackId;
89    }
90  
91    /**
92     * Returns file Path of media file.
93     * @return file path of media file
94     */
95    public String getFilePath() {
96      return filePath;
97    }
98  
99    /**
100    * Serialize to XML.
101    * @return XML as String
102    * @throws JAXBException if an error occures
103    */
104   public String toXml() throws JAXBException {
105     StringWriter sw = new StringWriter();
106     JAXBContext jctx = JAXBContext.newInstance(MediaSegments.class);
107     Marshaller mediaSegmentsMarshaller = jctx.createMarshaller();
108     mediaSegmentsMarshaller.marshal(this, sw);
109     return sw.toString();
110   }
111 
112   /**
113    * Deserialize from XML.
114    * @param mediaSegmentsXml {@link MediaSegments} XML as String
115    * @return deserialized {@link MediaSegments}
116    * @throws JAXBException if an error occures
117    */
118   public static MediaSegments fromXml(String mediaSegmentsXml) throws JAXBException {
119     MediaSegments mediaSegments = null;
120     JAXBContext jctx = JAXBContext.newInstance(MediaSegments.class);
121     Unmarshaller unmarshaller = jctx.createUnmarshaller();
122     try (StringReader sr = new StringReader(mediaSegmentsXml)) {
123       InputSource is = new InputSource(sr);
124       mediaSegments = (MediaSegments) unmarshaller.unmarshal(XmlSafeParser.parse(is));
125     } catch (IOException | SAXException e) {
126       throw new JAXBException(e);
127     }
128     return mediaSegments;
129   }
130 }