Java tutorial
/** * 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.metadata.dublincore; import static org.opencastproject.metadata.dublincore.DublinCore.ELEMENTS_1_1_NS_PREFIX; import static org.opencastproject.metadata.dublincore.DublinCore.ELEMENTS_1_1_NS_URI; import static org.opencastproject.metadata.dublincore.DublinCore.TERMS_NS_PREFIX; import static org.opencastproject.metadata.dublincore.DublinCore.TERMS_NS_URI; import org.opencastproject.mediapackage.EName; import org.opencastproject.mediapackage.MediaPackageElements; import org.opencastproject.util.XmlNamespaceBinding; import org.opencastproject.util.XmlNamespaceContext; import org.apache.commons.io.IOUtils; import java.io.IOException; import java.io.InputStream; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; import javax.xml.XMLConstants; /** * Factory for DublinCore catalogs. */ @ParametersAreNonnullByDefault public final class DublinCores { /** * Namespace name of Dublin Core metadata generated by matterhorn. By default this namespace is the default namespace * of xml documents generated by this class. */ public static final String OC_DC_CATALOG_NS_URI = "http://www.opencastproject.org/xsd/1.0/dublincore/"; /** The dc root element of Opencast DublinCore catalogs. */ public static final EName OC_DC_CATALOG_ROOT_ELEMENT = new EName(OC_DC_CATALOG_NS_URI, "dublincore"); /** Namespace URI for Opencast properties. */ public static final String OC_PROPERTY_NS_URI = "http://www.opencastproject.org/matterhorn/"; /** Prefix for Opencast properties. */ public static final String OC_PROPERTY_NS_PREFIX = "oc"; /** * Opencast property: The timezone of the agent specified to be scheduled with an event. IE: "America/Chicago" */ public static final EName OC_PROPERTY_AGENT_TIMEZONE = new EName(OC_PROPERTY_NS_URI, "agentTimezone"); /** * This is a string defining a recurrence pattern as specified in RFC 2445. See <a * href="http://tools.ietf.org/html/rfc2445#section-4.8.5.4">http://tools.ietf.org/html/rfc2445#section-4.8.5.4</a> */ public static final EName OC_PROPERTY_RECURRENCE = new EName(OC_PROPERTY_NS_URI, "recurrence"); public static final EName OC_PROPERTY_ANNOTATION = new EName(OC_PROPERTY_NS_URI, "annotation"); public static final EName OC_PROPERTY_ADVERTISED = new EName(OC_PROPERTY_NS_URI, "advertised"); public static final EName OC_PROPERTY_PROMOTED = new EName(OC_PROPERTY_NS_URI, "promoted"); private DublinCores() { } /** * Create a new empty Opencast DublinCore metadata catalog with * {@link org.opencastproject.mediapackage.MediaPackageElements#EPISODE} flavor. * Register all necessary namespaces and set the root tag to * {@link #OC_DC_CATALOG_ROOT_ELEMENT}. */ @Nonnull public static DublinCoreCatalog mkOpencast() { final DublinCoreCatalog dc = new DublinCoreCatalog(); dc.setFlavor(MediaPackageElements.EPISODE); dc.addBindings(XmlNamespaceContext.mk(XmlNamespaceBinding.mk(ELEMENTS_1_1_NS_PREFIX, ELEMENTS_1_1_NS_URI), XmlNamespaceBinding.mk(TERMS_NS_PREFIX, TERMS_NS_URI), // Opencast property namespace XmlNamespaceBinding.mk(OC_PROPERTY_NS_PREFIX, OC_PROPERTY_NS_URI), // Opencast root tag XmlNamespaceBinding.mk(XMLConstants.DEFAULT_NS_PREFIX, OC_DC_CATALOG_NS_URI))); dc.setRootTag(OC_DC_CATALOG_ROOT_ELEMENT); return dc; } /** * Create a new empty catalog suitable to take properties from the standard DublinCore * namespaces {@link DublinCore#ELEMENTS_1_1_NS_URI} and {@link DublinCore#TERMS_NS_URI}. * <p/> * Please note that neither a flavor nor a root tag is set. */ @Nonnull public static DublinCoreCatalog mkStandard() { final DublinCoreCatalog dc = new DublinCoreCatalog(); dc.addBindings(XmlNamespaceContext.mk(XmlNamespaceBinding.mk(ELEMENTS_1_1_NS_PREFIX, ELEMENTS_1_1_NS_URI), XmlNamespaceBinding.mk(TERMS_NS_PREFIX, TERMS_NS_URI))); return dc; } /** Create a new empty catalog with no special namespace registered, no root tag and no flavor. */ @Nonnull public static DublinCoreCatalog mkSimple() { return new DublinCoreCatalog(); } /** * Read a DublinCore catalog from a stream containing either JSON or XML. The method is * capable of detecting the used format. * <p/> * <strong>Implementation note:</strong> In order to detect the format the whole stream is read into memory first. If you * know upfront whether JSON or XML is used you may want to choose {@link DublinCoreJsonFormat#read(java.io.InputStream)} * or {@link DublinCoreXmlFormat#read(java.io.InputStream)} for performance reasons. */ @Nonnull public static DublinCoreCatalog read(InputStream in) { final String ser; try { ser = IOUtils.toString(in, "UTF-8"); } catch (IOException e) { throw new RuntimeException("Unable to read DublinCore from stream", e); } if (ser.startsWith("{")) { try { return DublinCoreJsonFormat.read(ser); } catch (Exception e) { throw new RuntimeException("Unable to read DublinCore catalog, JSON parsing failed.", e); } } else { try { return DublinCoreXmlFormat.read(ser); } catch (Exception e) { throw new RuntimeException("Unable to read DublinCore catalog, XML parsing failed.", e); } } } }