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      }
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     * Parses the serialized media package element and returns its object representation.
80     *
81     * @param xml
82     *         the serialized element
83     * @return the media package element instance
84     * @throws MediaPackageException
85     *         if de-serializing the element fails
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    * Serializes media package element list to a string.
101    *
102    * @param elements
103    *         element list to be serialized
104    * @return serialized media package element list
105    * @throws MediaPackageException
106    *         if serialization fails
107    */
108   public static String getArrayAsXml(Collection<? extends MediaPackageElement> elements) throws MediaPackageException {
109     // TODO write real serialization function
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    * Parses the serialized media package element list.
133    *
134    * @param xml
135    *         String to be parsed
136    * @return parsed media package element list
137    * @throws MediaPackageException
138    *         if de-serialization fails
139    */
140   public static List<? extends MediaPackageElement> getArrayFromXml(String xml) throws MediaPackageException {
141     // TODO write real deserialization function
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    * Same as getArrayFromXml(), but throwing a RuntimeException instead of a checked exception. Useful in streams.
163    *
164    * @param xml
165    *         String to be parsed
166    * @return parsed media package element list
167    *
168    * @throws MediaPackageRuntimeException
169    *         if de-serialization fails
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 }