use of org.opencastproject.mediapackage.XMLCatalogImpl.CatalogEntry in project opencast by opencast.
the class DublinCoreXmlFormat method writeDocument.
public static Document writeDocument(DublinCoreCatalog dc) throws ParserConfigurationException, TransformerException, IOException {
// Create the DOM document
final Document doc;
{
final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
doc = docBuilderFactory.newDocumentBuilder().newDocument();
}
if (dc.getRootTag() != null) {
final Element rootElement = doc.createElementNS(dc.getRootTag().getNamespaceURI(), dc.toQName(dc.getRootTag()));
doc.appendChild(rootElement);
for (CatalogEntry element : dc.getEntriesSorted()) {
rootElement.appendChild(element.toXml(doc));
}
return doc;
} else {
throw new RuntimeException("DublinCore catalog does not have a root tag.");
}
}
use of org.opencastproject.mediapackage.XMLCatalogImpl.CatalogEntry in project opencast by opencast.
the class DublinCoreCatalogTest method testLoadNonOpencastDublinCore.
@Test
public void testLoadNonOpencastDublinCore() throws Exception {
final DublinCoreCatalog dc = read("/dublincore-non-oc.xml");
for (DublinCoreValue value : dc.getValuesFlat()) {
logger.debug("DublinCorevalue " + value.getValue());
}
assertEquals(9, dc.getValuesFlat().size());
assertEquals(2, dc.get(DublinCore.PROPERTY_TITLE).size());
assertEquals(Opt.some(EName.mk("http://lib.org/metadata-enc", "PlainTitle")), dc.get(DublinCore.PROPERTY_TITLE).get(0).getEncodingScheme());
for (CatalogEntry entry : dc.getEntriesSorted()) {
logger.debug(entry.getEName().toString() + " " + entry.getValue());
}
assertTrue("Property foo:id should be in the list of known properties.", dc.getProperties().contains(PROPERTY_FOO_ID));
assertEquals(fooId, dc.getFirstVal(PROPERTY_FOO_ID).getValue());
}
use of org.opencastproject.mediapackage.XMLCatalogImpl.CatalogEntry in project opencast by opencast.
the class XMLCatalogImplTest method testEqualityOfCatalogEntries.
/**
* Two catalog entries shall be considered equal if
* the have the same ename, the same value and the same <em>set</em> of attributes.
*/
@Test
public void testEqualityOfCatalogEntries() throws Exception {
final Map<EName, String> a1 = map(tuple(EName.mk("http://lang.org", "lang"), "en"), tuple(EName.mk("http://value.org", "value"), "value"), tuple(EName.mk("http://type.org", "type"), "string"));
final Map<EName, String> a2 = map(tuple(EName.mk("http://type.org", "type"), "string"), tuple(EName.mk("http://lang.org", "lang"), "en"), tuple(EName.mk("http://value.org", "value"), "value"));
final CatalogEntry c1 = new TestImpl().mkCatalogEntry(EName.mk("http://extron.com", "extron"), "value", a1);
final CatalogEntry c2 = new TestImpl().mkCatalogEntry(EName.mk("http://extron.com", "extron"), "value", a2);
assertEquals(c1, c2);
final CatalogEntry c3 = new TestImpl().mkCatalogEntry(EName.mk("http://extron.com", "extron2"), "value", a2);
assertNotEquals(c1, c3);
}
use of org.opencastproject.mediapackage.XMLCatalogImpl.CatalogEntry in project opencast by opencast.
the class DublinCoreXmlFormat method merge.
/**
* Merge values that are new, changed, or emptied from the "from" catalog into the existing "into" catalog.
*
* @param fromCatalog contains targeted new values (new elements, changed element values, emptied element values).
* @param intoCatalog is the existing catalog that merges in the targed changes.
* @return the merged catalog
*/
public static DublinCoreCatalog merge(DublinCoreCatalog fromCatalog, DublinCoreCatalog intoCatalog) {
// If one catalog is null, return the other.
if (fromCatalog == null) {
return intoCatalog;
}
if (intoCatalog == null) {
return fromCatalog;
}
DublinCoreCatalog mergedCatalog = (DublinCoreCatalog) intoCatalog.clone();
List<CatalogEntry> mergeEntries = fromCatalog.getEntriesSorted();
for (CatalogEntry mergeEntry : mergeEntries) {
// ignore root entry
if ((mergeEntry.getEName()).equals(intoCatalog.getRootTag()))
continue;
// if language is provided, only overwrite existing of same language
String lang = mergeEntry.getAttribute(XMLCatalogImpl.XML_LANG_ATTR);
if (StringUtils.isNotEmpty(lang)) {
// Passing a null value will remove the exiting value
mergedCatalog.set(mergeEntry.getEName(), StringUtils.trimToNull(mergeEntry.getValue()), lang);
} else {
mergedCatalog.set(mergeEntry.getEName(), StringUtils.trimToNull(mergeEntry.getValue()));
}
}
return mergedCatalog;
}
use of org.opencastproject.mediapackage.XMLCatalogImpl.CatalogEntry in project opencast by opencast.
the class DublinCoreUtil method calculateChecksum.
/**
* Calculate an MD5 checksum for a DublinCore catalog.
*/
public static Checksum calculateChecksum(DublinCoreCatalog dc) {
// Use 0 as a word separator. This is safe since none of the UTF-8 code points
// except \u0000 contains a null byte when converting to a byte array.
final byte[] sep = new byte[] { 0 };
final MessageDigest md = // consider all DublinCore properties
$(getPropertiesSorted(dc)).bind(new Fn<CatalogEntry, Stream<String>>() {
@Override
public Stream<String> apply(CatalogEntry entry) {
// get attributes, sorted and serialized as [name, value, name, value, ...]
final Stream<String> attributesSorted = $(entry.getAttributes().entrySet()).sort(new Comparator<Entry<EName, String>>() {
@Override
public int compare(Entry<EName, String> o1, Entry<EName, String> o2) {
return o1.getKey().compareTo(o2.getKey());
}
}).bind(new Fn<Entry<EName, String>, Stream<String>>() {
@Override
public Stream<String> apply(Entry<EName, String> attribute) {
return $(attribute.getKey().toString(), attribute.getValue());
}
});
return $(entry.getEName().toString(), entry.getValue()).append(attributesSorted);
}
}).append(Opt.nul(dc.getRootTag()).map(toString)).foldl(mkMd5MessageDigest(), new Fn2<MessageDigest, String, MessageDigest>() {
@Override
public MessageDigest apply(MessageDigest digest, String s) {
digest.update(s.getBytes(StandardCharsets.UTF_8));
// add separator byte (see definition above)
digest.update(sep);
return digest;
}
});
try {
return Checksum.create("md5", Checksum.convertToHex(md.digest()));
} catch (NoSuchAlgorithmException e) {
return chuck(e);
}
}
Aggregations