1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
47
48 @XmlRootElement(name = "media-segments")
49 @XmlAccessorType(XmlAccessType.NONE)
50 public class MediaSegments {
51
52
53 @XmlElement(name = "trackId", required = true)
54 private String trackId;
55
56
57 @XmlElement(name = "filePath")
58 private String filePath;
59
60
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
77
78
79 public List<MediaSegment> getMediaSegments() {
80 return mediaSegments;
81 }
82
83
84
85
86
87 public String getTrackId() {
88 return trackId;
89 }
90
91
92
93
94
95 public String getFilePath() {
96 return filePath;
97 }
98
99
100
101
102
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
114
115
116
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 }