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 }
66 StringWriter writer = new StringWriter();
67 Marshaller m = null;
68 try {
69 m = MediaPackageImpl.context.createMarshaller();
70 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
71 m.marshal(element, writer);
72 return writer.toString();
73 } catch (JAXBException e) {
74 throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e);
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87 public static MediaPackageElement getFromXml(String xml) throws MediaPackageException {
88 Unmarshaller m = null;
89 try {
90 m = MediaPackageImpl.context.createUnmarshaller();
91 return (MediaPackageElement) m.unmarshal(XmlSafeParser.parse(toInputStream(xml)));
92 } catch (JAXBException e) {
93 throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e);
94 } catch (IOException | SAXException e) {
95 throw new MediaPackageException(e);
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108 public static String getArrayAsXml(Collection<? extends MediaPackageElement> elements) throws MediaPackageException {
109
110 if (elements == null || elements.isEmpty()) {
111 return "";
112 }
113 try {
114 StringBuilder builder = new StringBuilder();
115 Iterator<? extends MediaPackageElement> it = elements.iterator();
116 builder.append(getAsXml(it.next()));
117 while (it.hasNext()) {
118 builder.append("###");
119 builder.append(getAsXml(it.next()));
120 }
121 return builder.toString();
122 } catch (Exception e) {
123 if (e instanceof MediaPackageException) {
124 throw (MediaPackageException) e;
125 } else {
126 throw new MediaPackageException(e);
127 }
128 }
129 }
130
131
132
133
134
135
136
137
138
139
140 public static List<? extends MediaPackageElement> getArrayFromXml(String xml) throws MediaPackageException {
141
142 try {
143 List<MediaPackageElement> elements = new LinkedList<MediaPackageElement>();
144 String[] xmlArray = xml.split("###");
145 for (String xmlElement : xmlArray) {
146 if ("".equals(xmlElement.trim())) {
147 continue;
148 }
149 elements.add(getFromXml(xmlElement.trim()));
150 }
151 return elements;
152 } catch (Exception e) {
153 if (e instanceof MediaPackageException) {
154 throw (MediaPackageException) e;
155 } else {
156 throw new MediaPackageException(e);
157 }
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171 public static List<? extends MediaPackageElement> getArrayFromXmlUnchecked(String xml) {
172 try {
173 return getArrayFromXml(xml);
174 } catch (MediaPackageException e) {
175 throw new MediaPackageRuntimeException(e);
176 }
177 }
178
179 }