/** * Unmarshals XML representation of a MediaPackage via JAXB. * * @param xml * the serialized xml string * @return the deserialized media package * @throws MediaPackageException */ public static MediaPackageImpl valueOf(String xml) throws MediaPackageException { try { return MediaPackageImpl.valueOf(IOUtils.toInputStream(xml, "UTF-8")); } catch (IOException e) { throw new MediaPackageException(e); } }
/** * {@inheritDoc} * * @see org.opencastproject.mediapackage.MediaPackageBuilder#loadFromXml(java.lang.String) */ @Override public MediaPackage loadFromXml(String xml) throws MediaPackageException { InputStream in = null; try { in = IOUtils.toInputStream(xml, "UTF-8"); return loadFromXml(in); } catch (IOException e) { throw new MediaPackageException(e); } finally { IOUtils.closeQuietly(in); } }
/** * Parses the serialized media package element list. * * @param xml * String to be parsed * @return parsed media package element list * @throws MediaPackageException * if de-serialization fails */ public static List<? extends MediaPackageElement> getArrayFromXml(String xml) throws MediaPackageException { // TODO write real deserialization function try { List<MediaPackageElement> elements = new LinkedList<MediaPackageElement>(); String[] xmlArray = xml.split("###"); for (String xmlElement : xmlArray) { if ("".equals(xmlElement.trim())) continue; elements.add(getFromXml(xmlElement.trim())); } return elements; } catch (Exception e) { if (e instanceof MediaPackageException) { throw (MediaPackageException) e; } else { throw new MediaPackageException(e); } } }
/** * Parses the serialized media package list. * * @param xml * String to be parsed * @return parsed media package list * @throws MediaPackageException * if de-serialization fails */ public static List<MediaPackage> getArrayFromXml(String xml) throws MediaPackageException { try { List<MediaPackage> mediaPackages = new LinkedList<MediaPackage>(); if (StringUtils.isBlank(xml)) return mediaPackages; String[] xmlArray = xml.split("###"); for (String xmlElement : xmlArray) { mediaPackages.add(getFromXml(xmlElement.trim())); } return mediaPackages; } catch (Exception e) { if (e instanceof MediaPackageException) { throw (MediaPackageException) e; } else { throw new MediaPackageException(e); } } }
/** * Parses the serialized media package element and returns its object representation. * * @param xml * the serialized element * @return the media package element instance * @throws MediaPackageException * if de-serializing the element fails */ public static MediaPackageElement getFromXml(String xml) throws MediaPackageException { Unmarshaller m = null; try { m = MediaPackageImpl.context.createUnmarshaller(); return (MediaPackageElement) m.unmarshal(new InputSource(toInputStream(xml))); } catch (JAXBException e) { throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e); } }
/** * Serializes the media package element to a string. * * @param element * the element * @return the serialized media package element * @throws MediaPackageException * if serialization failed */ public static String getAsXml(MediaPackageElement element) throws MediaPackageException { if (element == null) throw new IllegalArgumentException("Mediapackage element must not be null"); StringWriter writer = new StringWriter(); Marshaller m = null; try { m = MediaPackageImpl.context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); m.marshal(element, writer); return writer.toString(); } catch (JAXBException e) { throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e); } }
/** * Serializes media package list to a string. * * @param mediaPackages * media package list to be serialized * @return serialized media package list * @throws MediaPackageException * if serialization fails */ public static String getArrayAsXml(List<MediaPackage> mediaPackages) throws MediaPackageException { try { StringBuilder builder = new StringBuilder(); if (mediaPackages.isEmpty()) return builder.toString(); builder.append(getAsXml(mediaPackages.get(0))); for (int i = 1; i < mediaPackages.size(); i++) { builder.append("###"); builder.append(getAsXml(mediaPackages.get(i))); } return builder.toString(); } catch (Exception e) { if (e instanceof MediaPackageException) { throw (MediaPackageException) e; } else { throw new MediaPackageException(e); } } }
/** * Serializes media package element list to a string. * * @param elements * element list to be serialized * @return serialized media package element list * @throws MediaPackageException * if serialization fails */ public static String getArrayAsXml(Collection<? extends MediaPackageElement> elements) throws MediaPackageException { // TODO write real serialization function if (elements == null || elements.isEmpty()) return ""; try { StringBuilder builder = new StringBuilder(); Iterator<? extends MediaPackageElement> it = elements.iterator(); builder.append(getAsXml(it.next())); while (it.hasNext()) { builder.append("###"); builder.append(getAsXml(it.next())); } return builder.toString(); } catch (Exception e) { if (e instanceof MediaPackageException) { throw (MediaPackageException) e; } else { throw new MediaPackageException(e); } } }
/** * Reads the media package from an xml node. * * @param xml * the node * @return the deserialized media package */ public static MediaPackageImpl valueOf(Node xml) throws MediaPackageException { InputStream in = null; ByteArrayOutputStream out = null; try { Unmarshaller unmarshaller = context.createUnmarshaller(); // Serialize the media package DOMSource domSource = new DOMSource(xml); out = new ByteArrayOutputStream(); StreamResult result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(domSource, result); in = new ByteArrayInputStream(out.toByteArray()); return unmarshaller.unmarshal(new StreamSource(in), MediaPackageImpl.class).getValue(); } catch (Exception e) { throw new MediaPackageException("Error deserializing media package node", e); } finally { IoSupport.closeQuietly(in); IoSupport.closeQuietly(out); } }
/** * Writes an xml representation of this MediaPackage to a stream. * * @param mediaPackage * the mediaPackage * @param out * The output stream * @param format * Whether to format the output for readability, or not (false gives better performance) * @throws MediaPackageException * if serializing or reading from a serialized media package fails */ public static void getAsXml(MediaPackage mediaPackage, OutputStream out, boolean format) throws MediaPackageException { try { Marshaller marshaller = MediaPackageImpl.context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format); marshaller.marshal(mediaPackage, out); } catch (JAXBException e) { throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e); } }
/** * Reads the media package from the input stream. * * @param xml * the input stream * @return the deserialized media package */ public static MediaPackageImpl valueOf(InputStream xml) throws MediaPackageException { try { Unmarshaller unmarshaller = context.createUnmarshaller(); return unmarshaller.unmarshal(new StreamSource(xml), MediaPackageImpl.class).getValue(); } catch (JAXBException e) { throw new MediaPackageException(e.getLinkedException() != null ? e.getLinkedException() : e); } finally { IoSupport.closeQuietly(xml); } }
/** * {@inheritDoc} * * @see org.opencastproject.mediapackage.MediaPackageBuilder#loadFromXml(java.io.InputStream) */ public MediaPackage loadFromXml(InputStream is) throws MediaPackageException { if (serializer != null) { // FIXME This code runs if *any* serializer is present, regardless of the serializer implementation try { Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); rewriteUrls(xml, serializer); return MediaPackageImpl.valueOf(xml); } catch (Exception e) { throw new MediaPackageException("Error deserializing paths in media package", e); } } else { return MediaPackageImpl.valueOf(is); } }
urlValue = (serializer != null) ? serializer.encodeURI(uri).toString() : uri.toString(); } catch (URISyntaxException e) { throw new MediaPackageException(e);
@Override public MediaPackage loadFromXml(Node xml) throws MediaPackageException { if (serializer != null) { // FIXME This code runs if *any* serializer is present, regardless of the serializer implementation try { rewriteUrls(xml, serializer); return MediaPackageImpl.valueOf(xml); } catch (Exception e) { throw new MediaPackageException("Error deserializing paths in media package", e); } } else { return MediaPackageImpl.valueOf(xml); } }
docBuilder = docBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e1) { throw new MediaPackageException(e1);
throw new MediaPackageException("Track " + track + " does not have a duration");
dest.add(e); } else if (MergeMode.Fail == mode) { throw new MediaPackageException("Target media package " + dest + " already contains element with id " + e.getIdentifier()); throw new MediaPackageException(e);