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 import org.opencastproject.util.data.Function;
28
29 import org.xml.sax.SAXException;
30
31 import java.io.IOException;
32 import java.io.StringWriter;
33 import java.util.Collection;
34 import java.util.Iterator;
35 import java.util.LinkedList;
36 import java.util.List;
37
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.Marshaller;
40 import javax.xml.bind.Unmarshaller;
41
42
43
44
45 public final class MediaPackageElementParser {
46
47
48
49
50 private MediaPackageElementParser() {
51
52 }
53
54
55
56
57
58
59
60
61
62
63 public static String getAsXml(MediaPackageElement element) throws MediaPackageException {
64 if (element == null)
65 throw new IllegalArgumentException("Mediapackage element must not be null");
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 public static <A extends MediaPackageElement> Function<A, String> getAsXml() {
80 return new Function.X<A, String>() {
81 @Override protected String xapply(MediaPackageElement elem) throws Exception {
82 return getAsXml(elem);
83 }
84 };
85 }
86
87
88
89
90
91
92
93
94
95
96 public static MediaPackageElement getFromXml(String xml) throws MediaPackageException {
97 Unmarshaller m = null;
98 try {
99 m = MediaPackageImpl.context.createUnmarshaller();
100 return (MediaPackageElement) m.unmarshal(XmlSafeParser.parse(toInputStream(xml)));
101 } catch (JAXBException e) {
102 throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e);
103 } catch (IOException | SAXException e) {
104 throw new MediaPackageException(e);
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117 public static String getArrayAsXml(Collection<? extends MediaPackageElement> elements) throws MediaPackageException {
118
119 if (elements == null || elements.isEmpty()) return "";
120 try {
121 StringBuilder builder = new StringBuilder();
122 Iterator<? extends MediaPackageElement> it = elements.iterator();
123 builder.append(getAsXml(it.next()));
124 while (it.hasNext()) {
125 builder.append("###");
126 builder.append(getAsXml(it.next()));
127 }
128 return builder.toString();
129 } catch (Exception e) {
130 if (e instanceof MediaPackageException) {
131 throw (MediaPackageException) e;
132 } else {
133 throw new MediaPackageException(e);
134 }
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147 public static List<? extends MediaPackageElement> getArrayFromXml(String xml) throws MediaPackageException {
148
149 try {
150 List<MediaPackageElement> elements = new LinkedList<MediaPackageElement>();
151 String[] xmlArray = xml.split("###");
152 for (String xmlElement : xmlArray) {
153 if ("".equals(xmlElement.trim())) continue;
154 elements.add(getFromXml(xmlElement.trim()));
155 }
156 return elements;
157 } catch (Exception e) {
158 if (e instanceof MediaPackageException) {
159 throw (MediaPackageException) e;
160 } else {
161 throw new MediaPackageException(e);
162 }
163 }
164 }
165
166
167
168
169
170
171
172
173
174
175
176 public static List<? extends MediaPackageElement> getArrayFromXmlUnchecked(String xml) {
177 try {
178 return getArrayFromXml(xml);
179 } catch (MediaPackageException e) {
180 throw new MediaPackageRuntimeException(e);
181 }
182 }
183
184 }