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  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   * Convenience implementation that supports serializing and deserializing media package elements.
43   */
44  public final class MediaPackageElementParser {
45  
46    /**
47     * Private constructor to prohibit instances of this static utility class.
48     */
49    private MediaPackageElementParser() {
50      // Nothing to do
51    }
52  
53    /**
54     * Serializes the media package element to a string.
55     *
56     * @param element
57     *         the element
58     * @return the serialized media package element
59     * @throws MediaPackageException
60     *         if serialization failed
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     * Parses the serialized media package element and returns its object representation.
79     *
80     * @param xml
81     *         the serialized element
82     * @return the media package element instance
83     * @throws MediaPackageException
84     *         if de-serializing the element fails
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     * Serializes media package element list to a string.
100    *
101    * @param elements
102    *         element list to be serialized
103    * @return serialized media package element list
104    * @throws MediaPackageException
105    *         if serialization fails
106    */
107   public static String getArrayAsXml(Collection<? extends MediaPackageElement> elements) throws MediaPackageException {
108     // TODO write real serialization function
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    * Parses the serialized media package element list.
130    *
131    * @param xml
132    *         String to be parsed
133    * @return parsed media package element list
134    * @throws MediaPackageException
135    *         if de-serialization fails
136    */
137   public static List<? extends MediaPackageElement> getArrayFromXml(String xml) throws MediaPackageException {
138     // TODO write real deserialization function
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    * Same as getArrayFromXml(), but throwing a RuntimeException instead of a checked exception. Useful in streams.
158    *
159    * @param xml
160    *         String to be parsed
161    * @return parsed media package element list
162    *
163    * @throws MediaPackageRuntimeException
164    *         if de-serialization fails
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 }