Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.mediapackage.MediaPackageSupport.Filters.presentations;
import static org.opencastproject.util.data.Monadics.mlist;
import org.opencastproject.mediapackage.MediaPackageElement.Type;
import org.opencastproject.mediapackage.identifier.Id;
import org.opencastproject.mediapackage.identifier.IdImpl;
import org.opencastproject.util.DateTimeSupport;
import org.opencastproject.util.IoSupport;
import org.opencastproject.util.XmlSafeParser;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.transform.stream.StreamSource;
/**
* Default implementation for a media media package.
*/
@XmlType(name = "mediapackage", namespace = "http://mediapackage.opencastproject.org", propOrder = { "title", "series",
"seriesTitle", "creators", "contributors", "subjects", "license", "language", "tracks", "catalogs",
"attachments", "publications" })
@XmlRootElement(name = "mediapackage", namespace = "http://mediapackage.opencastproject.org")
@XmlAccessorType(XmlAccessType.NONE)
public final class MediaPackageImpl implements MediaPackage {
/** the logging facility provided by log4j */
private static final Logger logger = LoggerFactory.getLogger(MediaPackageImpl.class.getName());
/**
* The prefix indicating that a tag should be excluded from a search for elements using
* {@link #getElementsByTags(Collection)}
*/
public static final String NEGATE_TAG_PREFIX = "-";
/** Context for serializing and deserializing */
static final JAXBContext context;
/** The media package element builder, may remain null */
private MediaPackageElementBuilder mediaPackageElementBuilder = null;
@XmlElement(name = "title")
private String title = null;
@XmlElement(name = "seriestitle")
private String seriesTitle = null;
@XmlElement(name = "language")
private String language = null;
@XmlElement(name = "series")
private String series = null;
@XmlElement(name = "license")
private String license = null;
@XmlElementWrapper(name = "creators")
@XmlElement(name = "creator")
private Set creators = null;
@XmlElementWrapper(name = "contributors")
@XmlElement(name = "contributor")
private Set contributors = null;
@XmlElementWrapper(name = "subjects")
@XmlElement(name = "subject")
private Set subjects = null;
/** The media package's identifier */
private Id identifier = null;
/** The start date and time */
private long startTime = 0L;
/** The media package duration */
private Long duration = null;
/** The media package's other (uncategorized) files */
private final List elements = new ArrayList();
/** Number of tracks */
private int tracks = 0;
/** Number of metadata catalogs */
private int catalogs = 0;
/** Number of attachments */
private int attachments = 0;
/** Numer of unclassified elements */
private int others = 0;
static {
try {
context = JAXBContext.newInstance("org.opencastproject.mediapackage", MediaPackageImpl.class.getClassLoader());
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
/**
* Creates a media package object.
*/
MediaPackageImpl() {
this(IdImpl.fromUUID());
}
/**
* Creates a media package object with the media package identifier.
*
* @param id
* the media package identifier
*/
MediaPackageImpl(Id id) {
this.identifier = id;
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getIdentifier()
*/
@XmlAttribute(name = "id")
@Override
public Id getIdentifier() {
return identifier;
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#setIdentifier(org.opencastproject.mediapackage.identifier.Id)
*/
@Override
public void setIdentifier(Id identifier) {
this.identifier = identifier;
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getDuration()
*/
@XmlAttribute(name = "duration")
@Override
public Long getDuration() {
if (duration == null && hasTracks()) {
recalculateDuration();
}
return duration;
}
/**
* The duration of the media package is the duration of the longest track
*/
private void recalculateDuration() {
duration = null;
for (Track t : getTracks()) {
if (t.getDuration() != null) {
if (duration == null || duration < t.getDuration())
duration = t.getDuration();
}
}
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#setDuration(Long)
*/
@Override
public void setDuration(Long duration) throws IllegalStateException {
if (hasTracks())
throw new IllegalStateException(
"The duration is determined by the length of the tracks and cannot be set manually");
this.duration = duration;
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getDate()
*/
@Override
public Date getDate() {
return new Date(startTime);
}
/**
* Returns the recording time in utc format.
*
* @return the recording time
*/
@XmlAttribute(name = "start")
public String getStartDateAsString() {
if (startTime == 0)
return null;
return DateTimeSupport.toUTC(startTime);
}
/**
* Sets the date and time of recording in utc format.
*
* @param startTime
* the start time
*/
public void setStartDateAsString(String startTime) {
if (startTime != null && !"0".equals(startTime) && !startTime.isEmpty()) {
try {
this.startTime = DateTimeSupport.fromUTC(startTime);
} catch (Exception e) {
logger.info("Unable to parse start time {}", startTime);
}
} else {
this.startTime = 0;
}
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#elements()
*/
@Override
public Iterable elements() {
return Arrays.asList(getElements());
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getElements()
*/
@Override
public MediaPackageElement[] getElements() {
return elements.toArray(new MediaPackageElement[elements.size()]);
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getElementByReference(org.opencastproject.mediapackage.MediaPackageReference)
*/
@Override
public MediaPackageElement getElementByReference(MediaPackageReference reference) {
for (MediaPackageElement e : this.elements) {
if (!reference.getType().equalsIgnoreCase(e.getElementType().toString()))
continue;
if (reference.getIdentifier().equals(e.getIdentifier()))
return e;
}
return null;
}
/**
* @see org.opencastproject.mediapackage.MediaPackage#getElementById(java.lang.String)
*/
@Override
public MediaPackageElement getElementById(String id) {
for (MediaPackageElement element : getElements()) {
if (id.equals(element.getIdentifier()))
return element;
}
return null;
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getElementsByTags(java.util.Collection)
*/
@Override
public MediaPackageElement[] getElementsByTags(Collection tags) {
if (tags == null || tags.isEmpty())
return getElements();
Set keep = new HashSet();
Set lose = new HashSet();
for (String tag : tags) {
if (StringUtils.isBlank(tag))
continue;
if (tag.startsWith(NEGATE_TAG_PREFIX)) {
lose.add(tag.substring(NEGATE_TAG_PREFIX.length()));
} else {
keep.add(tag);
}
}
List result = new ArrayList<>();
for (MediaPackageElement element : getElements()) {
boolean add = false;
for (String elementTag : element.getTags()) {
if (lose.contains(elementTag)) {
add = false;
break;
} else if (keep.contains(elementTag)) {
add = true;
}
}
if (add) {
result.add(element);
}
}
return result.toArray(new MediaPackageElement[result.size()]);
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getCatalogsByTags(java.util.Collection)
*/
@Override
public Catalog[] getCatalogsByTags(Collection tags) {
MediaPackageElement[] matchingElements = getElementsByTags(tags);
List catalogs = new ArrayList<>();
for (MediaPackageElement element : matchingElements) {
if (Catalog.TYPE.equals(element.getElementType())) {
catalogs.add((Catalog) element);
}
}
return catalogs.toArray(new Catalog[catalogs.size()]);
}
/**
* {@inheritDoc}
*
* @see org.opencastproject.mediapackage.MediaPackage#getTracksByTags(java.util.Collection)
*/
@Override
public Track[] getTracksByTags(Collection tags) {
MediaPackageElement[] matchingElements = getElementsByTags(tags);
List