org.opencastproject.mediapackage.MediaPackageParser Maven / Gradle / Ivy
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you under the Educational
* Community License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License
* at:
*
* http://opensource.org/licenses/ecl2.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
*/
package org.opencastproject.mediapackage;
import static org.opencastproject.util.data.functions.Misc.chuck;
import org.opencastproject.util.DateTimeSupport;
import org.opencastproject.util.XmlSafeParser;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.mapped.Configuration;
import org.codehaus.jettison.mapped.MappedNamespaceConvention;
import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
/**
* Convenience implementation that supports serializing and deserializing media packages.
*/
public final class MediaPackageParser {
/**
* Private constructor to prohibit instances of this static utility class.
*/
private MediaPackageParser() {
// Nothing to do
}
/**
* Serializes the media package to a string.
*
* @param mediaPackage
* the media package
* @return the serialized media package
*/
public static String getAsXml(MediaPackage mediaPackage) {
if (mediaPackage == null)
throw new IllegalArgumentException("Mediapackage must not be null");
try {
Marshaller marshaller = MediaPackageImpl.context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
StringWriter writer = new StringWriter();
marshaller.marshal(mediaPackage, writer);
return writer.toString();
} catch (JAXBException e) {
throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
}
}
/**
* Serializes the media package to a JSON string.
*
* @param mediaPackage
* the media package
* @return the serialized media package
*/
public static String getAsJSON(MediaPackage mediaPackage) {
if (mediaPackage == null) {
throw new IllegalArgumentException("Mediapackage must not be null");
}
try {
Marshaller marshaller = MediaPackageImpl.context.createMarshaller();
Configuration config = new Configuration();
config.setSupressAtAttributes(true);
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
StringWriter writer = new StringWriter();
XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, writer) {
@Override
public void writeStartElement(String prefix, String local, String uri) throws XMLStreamException {
super.writeStartElement("", local, "");
}
@Override
public void writeStartElement(String uri, String local) throws XMLStreamException {
super.writeStartElement("", local, "");
}
@Override
public void setPrefix(String pfx, String uri) throws XMLStreamException {
}
@Override
public void setDefaultNamespace(String uri) throws XMLStreamException {
}
};
marshaller.marshal(mediaPackage, xmlStreamWriter);
return writer.toString();
} catch (JAXBException e) {
throw new IllegalStateException(e.getLinkedException() != null ? e.getLinkedException() : e);
}
}
/** Serializes a media package to a {@link Document} without any further processing. */
public static Document getAsXmlDocument(MediaPackage mp) {
try {
final Marshaller marshaller = MediaPackageImpl.context.createMarshaller();
final Document doc = newDocument();
marshaller.marshal(mp, doc);
return doc;
} catch (JAXBException e) {
return chuck(e);
}
}
/** Create a new DOM document. */
private static Document newDocument() {
final DocumentBuilderFactory docBuilderFactory = XmlSafeParser.newDocumentBuilderFactory();
docBuilderFactory.setNamespaceAware(true);
try {
return docBuilderFactory.newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
return chuck(e);
}
}
/**
* Serializes the media package to a {@link org.w3c.dom.Document}.
*
* todo Implementation is currently defective since it misses various properties. See
* http://opencast.jira.com/browse/MH-9489 Use {@link #getAsXmlDocument(MediaPackage)} instead if you do not need a
* serializer.
*
* @param mediaPackage
* the mediapackage
* @param serializer
* the serializer
* @return the serialized media package
* @throws MediaPackageException
* if serializing fails
*/
public static Document getAsXml(MediaPackage mediaPackage, MediaPackageSerializer serializer)
throws MediaPackageException {
DocumentBuilderFactory docBuilderFactory = XmlSafeParser.newDocumentBuilderFactory();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = null;
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
throw new MediaPackageException(e1);
}
Document doc = docBuilder.newDocument();
// Root element "mediapackage"
Element mpXml = doc.createElement("mediapackage");
doc.appendChild(mpXml);
// Handle
if (mediaPackage.getIdentifier() != null)
mpXml.setAttribute("id", mediaPackage.getIdentifier().toString());
// Start time
if (mediaPackage.getDate() != null && mediaPackage.getDate().getTime() > 0)
mpXml.setAttribute("start", DateTimeSupport.toUTC(mediaPackage.getDate().getTime()));
// Duration
if (mediaPackage.getDuration() != null)
mpXml.setAttribute("duration", Long.toString(mediaPackage.getDuration()));
// Separate the media package members
List