@mos-connection/helper
Version:
Helper functions for the MOS-connection library
378 lines (376 loc) • 18.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XMLMosExternalMetaData = exports.XMLObjectPaths = exports.XMLMosObject = exports.XMLMosObjects = exports.XMLMosAck = void 0;
const XMLBuilder = require("xmlbuilder");
const model_1 = require("@mos-connection/model");
const lib_1 = require("../lib");
const ensureMethods_1 = require("../../utils/ensureMethods");
const Utils_1 = require("../../utils/Utils");
const parseMosTypes_1 = require("../parseMosTypes");
const ParseError_1 = require("../ParseError");
/* eslint-disable @typescript-eslint/no-namespace */
var XMLMosAck;
(function (XMLMosAck) {
function fromXML(path, xml, strict) {
try {
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const ack = {
ID: mosTypes.mosString128.createRequired(xml.objID, 'objID'),
Revision: mosTypes.number.createOptional(xml.objRev, 'objRev') ?? 0,
Status: mosTypes.stringEnum.createOptional({ value: xml.status, enum: model_1.IMOSAckStatus }, 'status') ||
model_1.IMOSAckStatus.ACK,
Description: mosTypes.mosString128.createRequired(xml.statusDescription || '', 'statusDescription'),
};
(0, lib_1.omitUndefined)(ack);
return ack;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosAck.fromXML = fromXML;
})(XMLMosAck = exports.XMLMosAck || (exports.XMLMosAck = {}));
var XMLMosObjects;
(function (XMLMosObjects) {
function fromXML(path, xml, strict) {
try {
const mosObj = (0, ensureMethods_1.isXMLObject)(xml) && xml.mosObj ? xml.mosObj : xml;
const mosObjs = [];
for (const xmlObj of (0, ensureMethods_1.ensureArray)(mosObj)) {
mosObjs.push(XMLMosObject.fromXML(`mosObj`, xmlObj, strict));
}
(0, lib_1.omitUndefined)(mosObjs);
return mosObjs;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosObjects.fromXML = fromXML;
function toXML(xml, objs, strict) {
for (const obj of objs) {
const xmlMosObj = XMLBuilder.create('mosObj');
XMLMosObject.toXML(xmlMosObj, obj, strict);
xml.importDocument(xmlMosObj);
}
}
XMLMosObjects.toXML = toXML;
})(XMLMosObjects = exports.XMLMosObjects || (exports.XMLMosObjects = {}));
var XMLMosObject;
(function (XMLMosObject) {
function fromXML(path, xml, strict) {
try {
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
xml = (0, ensureMethods_1.ensureXMLObject)(xml, strict);
const mosObj = (0, lib_1.literal)({
ID: mosTypes.mosString128.createOptional(xml.objID, 'objID'),
Slug: mosTypes.mosString128.createRequired(xml.objSlug, 'objSlug'),
MosAbstract: xml.mosAbstract,
MosAbstractStr: (0, lib_1.flattenXMLText)(xml.mosAbstract, strict),
Group: mosTypes.string.createOptional(xml.objGroup, 'objGroup'),
Type: mosTypes.stringEnum.createOptional({ value: xml.objType, enum: model_1.IMOSObjectType }, 'objType') ||
model_1.IMOSObjectType.OTHER,
TimeBase: mosTypes.number.createRequired(xml.objTB, 'objTB'),
Revision: mosTypes.number.createOptional(xml.objRev, 'objRev'),
Duration: mosTypes.number.createRequired(xml.objDur, 'objDur'),
Status: mosTypes.stringEnum.createOptional({ value: xml.status, enum: model_1.IMOSObjectStatus }, 'status'),
AirStatus: mosTypes.stringEnum.createOptional({ value: xml.objAir, enum: model_1.IMOSObjectAirStatus }, 'objAir'),
Paths: XMLObjectPaths.fromXML('objPaths', xml.objPaths, strict),
CreatedBy: mosTypes.mosString128.createOptional(xml.createdBy, 'createdBy'),
Created: mosTypes.mosTime.createOptional(xml.created, 'created'),
ChangedBy: mosTypes.mosString128.createOptional(xml.changedBy, 'changedBy'),
Changed: mosTypes.mosTime.createOptional(xml.changed, 'changed'),
Description: xml.description,
DescriptionStr: mosTypes.string.createOptional((0, lib_1.flattenXMLText)(xml.description, strict), 'description'),
MosItemEditorProgID: mosTypes.mosString128.createOptional(xml.mosItemEditorProgID, 'mosItemEditorProgID'),
MosExternalMetaData: (0, lib_1.has)(xml, 'mosExternalMetadata')
? XMLMosExternalMetaData.fromXML('mosExternalMetadata', xml.mosExternalMetadata, strict)
: undefined,
});
(0, lib_1.omitUndefined)(mosObj);
return mosObj;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosObject.fromXML = fromXML;
function toXML(xml, obj, strict) {
if (obj.ID)
(0, Utils_1.addTextElementInternal)(xml, 'objID', obj.ID, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'objSlug', obj.Slug, undefined, strict);
if (obj.MosAbstract)
(0, Utils_1.addTextElementInternal)(xml, 'mosAbstract', obj.MosAbstract, undefined, strict);
if (obj.Group)
(0, Utils_1.addTextElementInternal)(xml, 'objGroup', obj.Group, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'objType', obj.Type, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'objTB', obj.TimeBase, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'objRev', obj.Revision, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'objDur', obj.Duration, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'status', obj.Status, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'objAir', obj.AirStatus, undefined, strict);
if (obj.Paths)
XMLObjectPaths.toXML(xml, obj.Paths, strict);
(0, Utils_1.addTextElementInternal)(xml, 'createdBy', obj.CreatedBy, undefined, strict);
(0, Utils_1.addTextElementInternal)(xml, 'created', obj.Created, undefined, strict);
if (obj.ChangedBy)
(0, Utils_1.addTextElementInternal)(xml, 'changedBy', obj.ChangedBy, undefined, strict);
if (obj.Changed)
(0, Utils_1.addTextElementInternal)(xml, 'changed', obj.Changed, undefined, strict);
if (obj.Description)
(0, Utils_1.addTextElementInternal)(xml, 'description', obj.Description, undefined, strict); // not handled (todo)
if (obj.MosItemEditorProgID)
(0, Utils_1.addTextElementInternal)(xml, 'mosItemEditorProgID', obj.MosItemEditorProgID, undefined, strict);
if (obj.MosExternalMetaData)
XMLMosExternalMetaData.toXML(xml, obj.MosExternalMetaData);
}
XMLMosObject.toXML = toXML;
})(XMLMosObject = exports.XMLMosObject || (exports.XMLMosObject = {}));
var XMLObjectPaths;
(function (XMLObjectPaths) {
function fromXML(path, xmlObjPaths, strict) {
try {
if (!xmlObjPaths) {
if (strict)
throw new Error(`objPaths: Expected an object or array, got: ${xmlObjPaths}`);
}
const parseMosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const xmlToArray = (obj) => {
const paths = [];
obj = (0, ensureMethods_1.ensureXMLObject)(obj, strict);
// Possible inputs:
/*
Single obj path
{
objPath: '41cfc641849c417eaa10e4f3c377f181',
techDescription: 'VIDEO'
}
{
objPath: {
text: '41cfc641849c417eaa10e4f3c377f181',
techDescription: 'VIDEO'
},
objProxyPath: {
text: 'http://server/proxy/clipe.wmv',
techDescription: 'WM9 750Kbps'
}
}
{
objPath: { attributes: { techDescription: 'asdfasdf' }, text: 'asdfasdf' },
objMetadataPath: { attributes: { techDescription: 'skdjhfb' }, text: '8372h4fv' }
}
{
objPath: [
{
'$name': 'objPath',
'$type': 'text',
text: '41cfc641849c417eaa10e4f3c377f181',
techDescription: 'VIDEO'
},
{
'$name': 'objPath',
'$type': 'text',
text: 'abc123.mp4',
techDescription: 'MYVIDEO'
}
],
objProxyPath: {
'$name': 'objProxyPath',
'$type': 'text',
text: 'http://server/proxy/clipe.wmv',
techDescription: 'WM9 750Kbps'
}
}
*/
const tmpPaths = [];
// When there is only one: -------------------------------------------
if (typeof obj.objPath === 'string' && obj.techDescription) {
// A single objPath
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PATH,
techDescription: obj.techDescription,
Target: obj.objPath,
});
}
if (typeof obj.objProxyPath === 'string' && obj.techDescription) {
// A single objProxyPath
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PROXY_PATH,
techDescription: obj.techDescription,
Target: obj.objProxyPath,
});
}
if (typeof obj.objMetadataPath === 'string' && obj.techDescription) {
// A single objMetadataPath
tmpPaths.push({
Type: model_1.IMOSObjectPathType.METADATA_PATH,
techDescription: obj.techDescription,
Target: obj.objMetadataPath,
});
}
// When there are multiple: -------------------------------------------
if (Array.isArray(obj.objPath)) {
for (const objPath of (0, ensureMethods_1.ensureXMLObjectArray)(obj.objPath, strict)) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PATH,
techDescription: objPath.techDescription,
Target: objPath.text,
});
}
}
else if ((0, ensureMethods_1.isXMLObject)(obj.objPath) && (0, ensureMethods_1.isXMLObject)(obj.objPath.attributes)) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PATH,
techDescription: obj.objPath.attributes.techDescription,
Target: obj.objPath.text,
});
}
else if ((0, ensureMethods_1.isXMLObject)(obj.objPath) && obj.objPath.text) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PATH,
techDescription: obj.objPath.techDescription,
Target: obj.objPath.text,
});
}
if (Array.isArray(obj.objProxyPath)) {
for (const objProxyPath of (0, ensureMethods_1.ensureXMLObjectArray)(obj.objProxyPath, strict)) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PROXY_PATH,
techDescription: objProxyPath.techDescription,
Target: objProxyPath.text,
});
}
}
else if ((0, ensureMethods_1.isXMLObject)(obj.objProxyPath) && (0, ensureMethods_1.isXMLObject)(obj.objProxyPath.attributes)) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PROXY_PATH,
techDescription: obj.objProxyPath.attributes.techDescription,
Target: obj.objProxyPath.text,
});
}
else if ((0, ensureMethods_1.isXMLObject)(obj.objProxyPath) && obj.objProxyPath.text) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.PROXY_PATH,
techDescription: obj.objProxyPath.techDescription,
Target: obj.objProxyPath.text,
});
}
if (Array.isArray(obj.objMetadataPath)) {
for (const objMetadataPath of (0, ensureMethods_1.ensureXMLObjectArray)(obj.objMetadataPath, strict)) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.METADATA_PATH,
techDescription: objMetadataPath.techDescription,
Target: objMetadataPath.text,
});
}
}
else if ((0, ensureMethods_1.isXMLObject)(obj.objMetadataPath) && (0, ensureMethods_1.isXMLObject)(obj.objMetadataPath.attributes)) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.METADATA_PATH,
techDescription: obj.objMetadataPath.attributes.techDescription,
Target: obj.objMetadataPath.text,
});
}
else if ((0, ensureMethods_1.isXMLObject)(obj.objMetadataPath) && obj.objMetadataPath.text) {
tmpPaths.push({
Type: model_1.IMOSObjectPathType.METADATA_PATH,
techDescription: obj.objMetadataPath.techDescription,
Target: obj.objMetadataPath.text,
});
}
for (const tmpPath of tmpPaths) {
paths.push({
Type: tmpPath.Type,
Description: parseMosTypes.string.createRequired(tmpPath.techDescription, 'techDescription'),
Target: parseMosTypes.string.createRequired(tmpPath.Target, 'target'),
});
}
return paths;
};
const xmlPaths = xmlToArray(xmlObjPaths);
(0, lib_1.omitUndefined)(xmlPaths);
return xmlPaths;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLObjectPaths.fromXML = fromXML;
function toXML(xmlItem, paths, strict) {
if (paths) {
const xmlObjPaths = (0, Utils_1.addTextElementInternal)(xmlItem, 'objPaths', undefined, undefined, strict);
paths.forEach((path) => {
if (path.Type === model_1.IMOSObjectPathType.PATH) {
(0, Utils_1.addTextElementInternal)(xmlObjPaths, 'objPath', path.Target, {
techDescription: path.Description,
}, strict);
}
else if (path.Type === model_1.IMOSObjectPathType.PROXY_PATH) {
(0, Utils_1.addTextElementInternal)(xmlObjPaths, 'objProxyPath', path.Target, {
techDescription: path.Description,
}, strict);
}
else if (path.Type === model_1.IMOSObjectPathType.METADATA_PATH) {
(0, Utils_1.addTextElementInternal)(xmlObjPaths, 'objMetadataPath', path.Target, {
techDescription: path.Description,
}, strict);
}
});
}
}
XMLObjectPaths.toXML = toXML;
})(XMLObjectPaths = exports.XMLObjectPaths || (exports.XMLObjectPaths = {}));
var XMLMosExternalMetaData;
(function (XMLMosExternalMetaData) {
function fromXML(path, xml, strict) {
try {
const mosTypes = (0, parseMosTypes_1.getParseMosTypes)(strict);
const metadata = [];
for (const xmlMetadata of (0, ensureMethods_1.ensureXMLObjectArray)(xml, strict)) {
metadata.push({
MosScope: mosTypes.stringEnum.createRequired({ value: xmlMetadata.mosScope, enum: model_1.IMOSScope }, 'mosScope'),
MosSchema: mosTypes.string.createRequired(xmlMetadata.mosSchema, 'mosSchema'),
MosPayload: fixXMLMosPayload(xmlMetadata.mosPayload),
});
}
(0, lib_1.omitUndefined)(metadata);
return metadata;
}
catch (e) {
throw ParseError_1.ParseError.handleCaughtError(path, e);
}
}
XMLMosExternalMetaData.fromXML = fromXML;
function toXML(xml, metadatas) {
for (const metadata of metadatas) {
const xmlMetadata = XMLBuilder.create({
mosExternalMetadata: {
mosScope: metadata.MosScope,
mosSchema: metadata.MosSchema,
mosPayload: metadata.MosPayload,
},
});
xml.importDocument(xmlMetadata);
}
}
XMLMosExternalMetaData.toXML = toXML;
})(XMLMosExternalMetaData = exports.XMLMosExternalMetaData || (exports.XMLMosExternalMetaData = {}));
function fixXMLMosPayload(value) {
if (typeof value === 'object') {
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
value[i] = fixXMLMosPayload(value[i]);
}
}
else {
if ((0, lib_1.isEmpty)(value))
return ''; // In xml, an empty tag results in an empty object
for (const key of Object.keys(value)) {
value[key] = fixXMLMosPayload(value[key]);
}
}
}
return value;
}
//# sourceMappingURL=xmlConversion.js.map