1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.mediapackage;
23
24 import static org.apache.commons.io.IOUtils.toInputStream;
25
26 import org.opencastproject.util.XmlSafeParser;
27
28 import org.xml.sax.SAXException;
29
30 import java.io.IOException;
31 import java.io.StringWriter;
32 import java.util.Collection;
33 import java.util.Iterator;
34 import java.util.LinkedList;
35 import java.util.List;
36
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.Marshaller;
39 import javax.xml.bind.Unmarshaller;
40
41
42
43
44 public final class MediaPackageElementParser {
45
46
47
48
49 private MediaPackageElementParser() {
50
51 }
52
53
54
55
56
57
58
59
60
61
62 public static String getAsXml(MediaPackageElement element) throws MediaPackageException {
63 if (element == null)
64 throw new IllegalArgumentException("Mediapackage element must not be null");
65 StringWriter writer = new StringWriter();
66 Marshaller m = null;
67 try {
68 m = MediaPackageImpl.context.createMarshaller();
69 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
70 m.marshal(element, writer);
71 return writer.toString();
72 } catch (JAXBException e) {
73 throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e);
74 }
75 }
76
77
78
79
80
81
82
83
84
85
86 public static MediaPackageElement getFromXml(String xml) throws MediaPackageException {
87 Unmarshaller m = null;
88 try {
89 m = MediaPackageImpl.context.createUnmarshaller();
90 return (MediaPackageElement) m.unmarshal(XmlSafeParser.parse(toInputStream(xml)));
91 } catch (JAXBException e) {
92 throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e);
93 } catch (IOException | SAXException e) {
94 throw new MediaPackageException(e);
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107 public static String getArrayAsXml(Collection<? extends MediaPackageElement> elements) throws MediaPackageException {
108
109 if (elements == null || elements.isEmpty()) return "";
110 try {
111 StringBuilder builder = new StringBuilder();
112 Iterator<? extends MediaPackageElement> it = elements.iterator();
113 builder.append(getAsXml(it.next()));
114 while (it.hasNext()) {
115 builder.append("###");
116 builder.append(getAsXml(it.next()));
117 }
118 return builder.toString();
119 } catch (Exception e) {
120 if (e instanceof MediaPackageException) {
121 throw (MediaPackageException) e;
122 } else {
123 throw new MediaPackageException(e);
124 }
125 }
126 }
127
128
129
130
131
132
133
134
135
136
137 public static List<? extends MediaPackageElement> getArrayFromXml(String xml) throws MediaPackageException {
138
139 try {
140 List<MediaPackageElement> elements = new LinkedList<MediaPackageElement>();
141 String[] xmlArray = xml.split("###");
142 for (String xmlElement : xmlArray) {
143 if ("".equals(xmlElement.trim())) continue;
144 elements.add(getFromXml(xmlElement.trim()));
145 }
146 return elements;
147 } catch (Exception e) {
148 if (e instanceof MediaPackageException) {
149 throw (MediaPackageException) e;
150 } else {
151 throw new MediaPackageException(e);
152 }
153 }
154 }
155
156
157
158
159
160
161
162
163
164
165
166 public static List<? extends MediaPackageElement> getArrayFromXmlUnchecked(String xml) {
167 try {
168 return getArrayFromXml(xml);
169 } catch (MediaPackageException e) {
170 throw new MediaPackageRuntimeException(e);
171 }
172 }
173
174 }