-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsl_check.js
2618 lines (2445 loc) · 106 KB
/
sl_check.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* sl_check.js
*
* Check a service list
*/
import chalk from "chalk";
import { elementize, quote } from "./phlib/phlib.js";
import { tva, tvaEA } from "./TVA_definitions.js";
import { sats } from "./DVB_definitions.js";
import { dvbi, dvbiEC, dvbiEA, XMLdocumentType } from "./DVB-I_definitions.js";
import ErrorList, { WARNING, APPLICATION } from "./error_list.js";
import { isTAGURI } from "./URI_checks.js";
import { xPath, xPathM, isIn, unEntity, getElementByTagName, DuplicatedValue } from "./utils.js";
import { isPostcode, isASCII, isHTTPURL, isHTTPPathURL, isDomainName, isRTSPURL } from "./pattern_checks.js";
import { __dirname_linux } from "./data_locations.js";
import { checkValidLogos } from "./related_material_checks.js";
import { sl_InvalidHrefValue, InvalidURL, DeprecatedElement, keys } from "./common_errors.js";
import { mlLanguage, checkLanguage, checkXMLLangs, GetNodeLanguage } from "./multilingual_element.js";
import { checkAttributes, checkTopElementsAndCardinality, hasChild, SchemaCheck, SchemaVersionCheck, SchemaLoad } from "./schema_checks.js";
import writeOut from "./logger.js";
import { ValidateLanguage } from "./IANA_languages.js";
import {
LoadGenres,
LoadVideoCodecCS,
LoadAudioCodecCS,
LoadAccessibilityPurpose,
LoadAudioPurpose,
LoadSubtitleCarriages,
LoadSubtitleCodings,
LoadSubtitlePurposes,
LoadAudioConformanceCS,
LoadVideoConformanceCS,
LoadAudioPresentationCS,
LoadRecordingInfoCS,
LoadPictureFormatCS,
LoadColorimetryCS,
LoadServiceTypeCS,
LoadLanguages,
LoadCountries,
} from "./classification_scheme_loaders.js";
import CheckAccessibilityAttributes from "./accessibility_attributes_checks.js";
import { DASH_IF_Content_Protection_List, ContentProtectionIDs, CA_SYSTEM_ID_REGISTRY, CASystemIDs } from "./identifiers.js";
import {
GetSchema,
SchemaVersion,
SchemaSpecVersion,
ANY_NAMESPACE,
SCHEMA_r0,
SCHEMA_r1,
SCHEMA_r2,
SCHEMA_r3,
SCHEMA_r4,
SCHEMA_r5,
SCHEMA_r6,
SCHEMA_r7,
SCHEMA_unknown,
} from "./sl_data_versions.js";
import {
validServiceControlApplication,
validAgreementApplication,
validServiceInstanceControlApplication,
validServiceUnavailableApplication,
validDASHcontentType,
} from "./sl_data_versions.js";
import {
validOutScheduleHours,
validContentFinishedBanner,
validServiceListLogo,
validServiceAgreementApp,
validServiceLogo,
validServiceBanner,
validContentGuideSourceLogo,
} from "./sl_data_versions.js";
import { check_CMCD } from "./CMCD.js";
const LCN_TABLE_NO_TARGETREGION = "unspecifiedRegion",
LCN_TABLE_NO_SUBSCRIPTION = "unspecifiedPackage";
const SERVICE_LIST_RM = "service list";
const SERVICE_RM = "service";
const SERVICE_INSTANCE_RM = "service instance";
const CONTENT_GUIDE_RM = "content guide";
const EXTENSION_LOCATION_SERVICE_LIST_REGISTRY = 101,
EXTENSION_LOCATION_SERVICE_ELEMENT = 201,
EXTENSION_LOCATION_DASH_INSTANCE = 202,
EXTENSION_LOCATION_OTHER_DELIVERY = 203;
/**
* determines if the identifer provided complies with the requirements for a service identifier
* at this stage only IETF RFC 4151 TAG URIs are permitted
*
* @param {String} identifier The service identifier
* @returns {boolean} true if the service identifier complies with the specification otherwise false
*/
let validServiceIdentifier = (identifier) => isTAGURI(identifier);
let validServiceListIdentifier = (identifier) => isTAGURI(identifier);
/**
* determines if the identifer provided is unique against a list of known identifiers
*
* @param {String} identifier The service identifier
* @param {Array} identifiers The list of known service identifiers
* @returns {boolean} true if the service identifier is unique otherwise false
*/
let uniqueServiceIdentifier = (identifier, identifiers) => !isIn(identifiers, identifier);
/**
* Add an error message an incorrect country code is specified in transmission parameters
*
* @param {String} value The invalid country code
* @param {String} src The transmission mechanism
* @param {String} loc The location of the element
*/
let InvalidCountryCode = (value, src, loc) => `invalid country code ${value.quote()} ${src ? `for ${src} parameters ` : ""}in ${loc}`;
/**
* Create a label for the optional language and value provided
* @param {XMLnode} pkg
* @param {String} lang
* @returns {String}
*/
let localizedSubscriptionPackage = (pkg, lang = null) => `${pkg.text()}/lang=${lang ? lang : mlLanguage(pkg)}`;
/**
* Construct an error message an unspecifed target region is used
*
* @param {String} region The unspecified target region
* @param {String} loc The location of the element
* @param {String} errCode The error code to be reported
*/
let UnspecifiedTargetRegion = (region, loc, errCode) => ({
code: errCode,
message: `${loc} has an unspecified ${dvbi.e_TargetRegion.elementize()} ${region.quote()}`,
key: "target region",
});
/**
* Construct an error message for missing <xxxDeliveryParameters>
*
* @param {String} source The missing source type
* @param {String} serviceId The serviceId whose instance is missing delivery parameters
* @param {XMLnode} element The <SourceType> element for which delivery parameters are not specified
* @param {String} errCode The error code to be reported
*/
let NoDeliveryParams = (source, serviceId, element, errCode) => ({
code: errCode,
message: `${source} delivery parameters not specified for service instance in service ${serviceId.quote()}`,
fragment: element,
key: "no delivery params",
});
if (!Array.prototype.forEachSubElement) {
// based on the polyfill at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
/*
* alternate to Array.prototype.forEach that only returns XML tree nodes that are elements
*/
Array.prototype.forEachSubElement = function (callback, thisArg) {
if (this == null) {
throw new TypeError("Array.prototype.forEachSubElement called on null or undefined");
}
var T, k;
// 1. Let O be the result of calling toObject() passing the
// |this| value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get() internal
// method of O with the argument "length".
// 3. Let len be toUint32(lenValue).
var len = O.length >>> 0;
// 4. If isCallable(callback) is false, throw a TypeError exception.
// See: https://es5.github.com/#x9.11
if (typeof callback !== "function") {
throw new TypeError(`${callback} is not a function`);
}
// 5. If thisArg was supplied, let T be thisArg; else let
// T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty
// internal method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal
// method of O with argument Pk.
kValue = O[k];
// ii. Call the Call internal method of callback with T as
// the this value and argument list containing kValue, k, and O.
if (kValue.type() == "element") callback.call(T, kValue, k, O);
}
// d. Increase k by 1.
k++;
}
// 8. return undefined
};
}
export default class ServiceListCheck {
#numRequests;
#knownLanguages;
#allowedGenres;
#allowedVideoSchemes;
#allowedAudioSchemes;
#knownCountries;
#audioPresentations;
#accessibilityPurposes;
#audioPurposes;
#subtitleCarriages;
#subtitleCodings;
#subtitlePurposes;
#allowedPictureFormats;
#allowedColorimetry;
#allowedServiceTypes;
#allowedAudioConformancePoints;
#allowedVideoConformancePoints;
#RecordingInfoCSvalues;
constructor(useURLs, opts, async = true) {
this.#numRequests = 0;
this.#knownLanguages = opts?.languages ? opts.languages : LoadLanguages(useURLs, async);
this.#knownCountries = opts?.countries ? opts.countries : LoadCountries(useURLs, async);
console.log(chalk.yellow.underline("loading classification schemes..."));
this.#allowedGenres = opts?.genres ? opts.genres : LoadGenres(useURLs, async);
this.#allowedVideoSchemes = opts?.videofmts ? opts.videofmts : LoadVideoCodecCS(useURLs, async);
this.#allowedAudioSchemes = opts?.audiofmts ? opts.audiofmts : LoadAudioCodecCS(useURLs, async);
this.#audioPresentations = opts?.audiopres ? opts?.audiopres : LoadAudioPresentationCS(useURLs, async);
this.#accessibilityPurposes = opts?.accessibilities ? opts.accessibilities : LoadAccessibilityPurpose(useURLs, async);
this.#audioPurposes = opts?.audiopurp ? opts.audiopurp : LoadAudioPurpose(useURLs, async);
this.#subtitleCarriages = opts?.stcarriage ? opts.stcarriage : LoadSubtitleCarriages(useURLs, async);
this.#subtitleCodings = opts?.stcodings ? opts.stcodings : LoadSubtitleCodings(useURLs, async);
this.#subtitlePurposes = opts?.stpurposes ? opts.stpurposes : LoadSubtitlePurposes(useURLs, async);
this.#allowedPictureFormats = LoadPictureFormatCS(useURLs, async);
this.#allowedColorimetry = LoadColorimetryCS(useURLs, async);
this.#allowedServiceTypes = LoadServiceTypeCS(useURLs, async);
this.#allowedAudioConformancePoints = LoadAudioConformanceCS(useURLs, async);
this.#allowedVideoConformancePoints = LoadVideoConformanceCS(useURLs, async);
this.#RecordingInfoCSvalues = LoadRecordingInfoCS(useURLs, async);
}
stats() {
let res = {};
res.numRequests = this.#numRequests;
res.numAllowedGenres = this.#allowedGenres?.count();
res.numKnownCountries = this.#knownCountries?.count();
this.#knownLanguages?.stats(res);
res.numAllowedPictureFormats = this.#allowedPictureFormats?.count();
res.numAllowedColorimetry = this.#allowedColorimetry?.count();
res.numAllowedServiceTypes = this.#allowedServiceTypes?.count();
res.numAllowedAudioSchemes = this.#allowedAudioSchemes?.count();
res.numAllowedVideoSchemes = this.#allowedVideoSchemes?.count();
res.numAllowedVideoConformancePoints = this.#allowedVideoConformancePoints?.count();
res.numAllowedAudioConformancePoints = this.#allowedAudioConformancePoints?.count();
res.numAudioPresentations = this.#audioPresentations?.count();
res.numAudioPurporses = this.#audioPurposes?.count();
res.numRecordingInfoValues = this.#RecordingInfoCSvalues?.count();
return res;
}
/**
* parses the region element, checks the values and adds it and its children (through recursion) to the linear list of region ids
*
* @param {object} props Metadata of the XML document
* @param {XMLnode} Region The <Region> element to process
* @param {integer} depth The current depth in the hierarchial structure of regions
* @param {Array} knownRegionIDs The list of region IDs that have been found
* @param {Array} countries
* @param {Object} errs The class where errors and warnings relating to the service list processing are stored
*/
/*private*/ #addRegion(props, Region, depth, knownRegionIDs, countries, errs) {
if (!Region) {
errs.addError({
type: APPLICATION,
code: "AR000",
message: "addRegion() called with Region==null",
});
return;
}
let schemaVersion = SchemaVersion(props.namespace);
let regionID = Region.attr(dvbi.a_regionID) ? Region.attr(dvbi.a_regionID).value() : null;
let displayRegionID = regionID ? regionID.quote() : '"noID"';
let countriesSpecified = [],
countryCodesSpecified = Region.attr(dvbi.a_countryCodes);
// this check should not happen with schema version 5 or greater, as this becomes part of the syntax
if (depth != 0 && countryCodesSpecified)
errs.addError({
code: "AR032",
message: `${dvbi.a_countryCodes.attribute(Region.name())} not permitted for sub-region ${displayRegionID}`,
key: "ccode in subRegion",
line: Region.line(),
});
if (countryCodesSpecified) {
countriesSpecified = countryCodesSpecified.value().split(",");
if (countriesSpecified)
countriesSpecified.forEach((country) => {
if (!this.#knownCountries.isISO3166code(country))
errs.addError({
code: "AR033",
message: `invalid country code (${country}) for region ${displayRegionID}`,
key: keys.k_InvalidCountryCode,
line: Region.line(),
});
});
} else countriesSpecified = countries;
if (schemaVersion >= SCHEMA_r4) {
let selectable = Region.attr(dvbi.a_selectable) ? Region.attr(dvbi.a_selectable).value() == "true" : true;
if (!selectable && depth == dvbi.MAX_SUBREGION_LEVELS)
errs.addError({
code: "AR010",
message: "Tertiary (leaf) subregion must be selectable",
key: "not selectable",
line: Region.line(),
});
if (!selectable && !hasChild(Region, dvbi.e_Region))
errs.addError({
code: "AR011",
message: "leaf subregion must be selectable",
key: "not selectable",
line: Region.line(),
});
if (regionID) {
if (knownRegionIDs.find((r) => r.region == regionID) != undefined)
errs.addError({
code: "AR012",
message: `Duplicate ${dvbi.a_regionID.attribute()} ${displayRegionID}`,
key: `duplicate ${dvbi.a_regionID.attribute()}`,
line: Region.line(),
});
else
knownRegionIDs.push({
countries: countriesSpecified,
region: regionID,
selectable: selectable,
used: false,
line: Region.line(),
});
} else
errs.addError({
code: "AR013",
message: `${dvbi.a_regionID.attribute()} is required`,
key: `no ${dvbi.a_regionID.attribute()}`,
line: Region.line(),
});
} else {
if (regionID) {
if (knownRegionIDs.find((r) => r.region == regionID) != undefined)
errs.addError({
code: "AR021",
message: `Duplicate ${dvbi.a_regionID.attribute()} ${displayRegionID}`,
key: `duplicate ${dvbi.a_regionID.attribute()}`,
line: Region.line(),
});
else
knownRegionIDs.push({
countries: countriesSpecified,
region: regionID,
used: false,
line: Region.line(),
});
} else
errs.addError({
code: "AR020",
message: `${dvbi.a_regionID.attribute()} is required`,
key: `no ${dvbi.a_regionID.attribute()}`,
line: Region.line(),
});
}
if (depth > dvbi.MAX_SUBREGION_LEVELS)
errs.addError({
code: "AR031",
message: `${dvbi.e_Region.elementize()} depth exceeded (>${dvbi.MAX_SUBREGION_LEVELS}) for sub-region ${displayRegionID}`,
key: "region depth exceeded",
line: Region.line(),
});
checkXMLLangs(dvbi.e_RegionName, `${dvbi.a_regionID.attribute(dvbi.e_Region)}=${displayRegionID}`, Region, errs, "AR041", this.#knownLanguages);
// <Region><Postcode>
let pc = 0,
Postcode,
PostcodeErrorMessage = "invalid postcode";
while ((Postcode = Region.get(xPath(props.prefix, dvbi.e_Postcode, ++pc), props.schema)) != null)
if (!isPostcode(Postcode.text()))
errs.addError({
code: "AR051",
message: `${Postcode.text().quote()} is not a valid postcode`,
key: PostcodeErrorMessage,
fragment: Postcode,
});
let rc = 0,
RegionChild;
while ((RegionChild = Region.get(xPath(props.prefix, dvbi.e_Region, ++rc), props.schema)) != null)
this.#addRegion(props, RegionChild, depth + 1, knownRegionIDs, countriesSpecified, errs);
}
/**
* verifies if the specified application is valid according to specification
*
* @param {XMLnode} MediaLocator The <MediaLocator> subelement (a libxmls object tree) of the <RelatedMaterial> element
* @param {Object} errs The class where errors and warnings relating to the service list processing are stored
* @param {string} Location The printable name used to indicate the location of the <RelatedMaterial> element being checked. used for error reporting
* @param {string} AppType The type of application being checked, from HowRelated@href
*/
/*private*/ #checkSignalledApplication(MediaLocator, errs, Location, AppType) {
const validApplicationTypes = [dvbi.XML_AIT_CONTENT_TYPE, dvbi.HTML5_APP, dvbi.XHTML_APP];
let isValidApplicationType = (type) => validApplicationTypes.includes(type);
if (!MediaLocator)
errs.addError({
code: "SA001",
message: `${tva.e_MediaLocator.elementize()} not specified for application ${tva.e_RelatedMaterialr.elementize()} in ${Location}`,
key: `no ${tva.e_MediaUri}`,
});
else {
let hasMediaURI = false;
if (MediaLocator.childNodes())
MediaLocator.childNodes().forEachSubElement((child) => {
if (child.name() == tva.e_MediaUri) {
hasMediaURI = true;
if (child.attr(tva.a_contentType) && !isValidApplicationType(child.attr(tva.a_contentType).value()))
errs.addError({
code: "SA003",
message: `${tva.a_contentType.attribute()} ${child
.attr(tva.a_contentType)
.value()
.quote()} is not supported application type for ${tva.e_RelatedMaterial.elementize()}${tva.e_MediaLocator.elementize()} in ${Location}`,
fragment: child,
key: `invalid ${tva.a_contentType.attribute(tva.e_MediaUri)}`,
});
if (!isASCII(child.text()))
errs.addError({
code: "SA014",
message: `URL ${child.text().quote()} contains non-ASCII characters in ${child.name().elementize()}`,
fragment: child,
key: "invalid resource URL",
});
if (!isHTTPURL(child.text()))
errs.addError({
code: "SA004",
message: `invalid URL ${child.text().quote()} specified for ${child.name().elementize()}`,
fragment: child,
key: "invalid resource URL",
});
if (AppType == dvbi.APP_SERVICE_PROVIDER && child.attr(tva.a_contentType) && child.attr(tva.a_contentType).value() != dvbi.XML_AIT_CONTENT_TYPE)
errs.addError({
code: "SA006",
message: `invalid application type ${child.attr(tva.a_contentType).value().quote()} for Serivce Provider Application (only XMLAIT allowed)`,
fragment: child,
key: "invalid app type",
});
}
});
if (!hasMediaURI)
errs.addError({
code: "SA005",
message: `${tva.e_MediaUri.elementize()} not specified for application ${tva.e_MediaLocator.elementize()} in ${Location}`,
fragment: MediaLocator,
key: `no ${tva.e_MediaUri}`,
});
}
}
/**
* determines if the identifer provided refers to a valid application launching method
*
* @param {XMLnode} HowRelated The service identifier
* @param {integer} schemaVersion The schema version of the XML document
* @returns {boolean} true if this is a valid application launching method else false
*/
/*private*/ #validServiceApplication(HowRelated, schemaVersion) {
// return true if the HowRelated element has a valid CS value for Service Related Applications (A177 5.2.3)
// urn:dvb:metadata:cs:LinkedApplicationCS:2019
if (!HowRelated) return false;
let val = HowRelated.attr(dvbi.a_href) ? HowRelated.attr(dvbi.a_href).value() : null;
if (!val) return false;
return validServiceControlApplication(val, schemaVersion) || validServiceUnavailableApplication(val);
}
/**
* verifies if the specified RelatedMaterial element is valid according to specification (contents and location)
*
* @param {object} props Metadata of the XML document
* @param {XMLnode} RelatedMaterial The <RelatedMaterial> element (a libxmls object tree) to be checked
* @param {Object} errs The class where errors and warnings relating to the service list processing are stored
* @param {string} Location The printable name used to indicate the location of the <RelatedMaterial> element being checked. used for error reporting
* @param {string} LocationType The type of element containing the <RelatedMaterial> element. Different validation rules apply to different location types
* @param {string} errCode The prefix to use for any errors found
* @returns {string} an href value if valid, else ""
*/
/*private*/ #validateRelatedMaterial(props, RelatedMaterial, errs, Location, LocationType, errCode) {
let rc = "";
if (!RelatedMaterial) {
errs.addError({
type: APPLICATION,
code: "RM000",
message: "validateRelatedMaterial() called with RelatedMaterial==null",
key: "invalid args",
});
return rc;
}
checkTopElementsAndCardinality(
RelatedMaterial,
[{ name: tva.e_HowRelated }, { name: tva.e_MediaLocator, maxOccurs: Infinity }, { name: tva.e_AccessibilityAttributes, minOccurs: 0 }],
dvbiEC.RelatedMaterial,
false,
errs,
`${errCode}-1`
);
let HowRelated = null,
MediaLocator = [],
AccessibilityAttribiutes = [];
if (RelatedMaterial.childNodes())
RelatedMaterial.childNodes().forEachSubElement((elem) => {
switch (elem.name()) {
case tva.e_HowRelated:
HowRelated = elem;
break;
case tva.e_MediaLocator:
MediaLocator.push(elem);
break;
case tva.e_AccessibilityAttributes:
AccessibilityAttribiutes.push(elem);
break;
}
});
if (!HowRelated) {
errs.addError({
code: `${errCode}-2`,
message: `${tva.e_HowRelated.elementize()} not specified for ${tva.e_RelatedMaterial.elementize()} in ${Location}`,
line: RelatedMaterial.line(),
key: `no ${tva.e_HowRelated}`,
});
return rc;
}
checkAttributes(HowRelated, [dvbi.a_href], [], tvaEA.HowRelated, errs, `${errCode}-2`);
if (HowRelated.attr(dvbi.a_href)) {
let RMErrorDescription = (_code, _elem, _table) => ({
code: _code,
desciption: `The application type indicated by the specified ${dvbi.a_href.attribute()} value is not permitted in a ${_elem.elementize()}. Refer to the semantic defintiion of ${dvbi.e_RelatedMaterial.elementize()} in table ${_table} of A177.`
});
switch (LocationType) {
case SERVICE_LIST_RM:
if (validServiceListLogo(HowRelated, props.namespace)) {
rc = HowRelated.attr(dvbi.a_href).value();
checkValidLogos(RelatedMaterial, errs, `${errCode}-10`, Location, this.#knownLanguages);
} else if (validServiceAgreementApp(HowRelated, props.namespace)) {
rc = HowRelated.attr(dvbi.a_href).value();
MediaLocator.forEach((locator) => this.#checkSignalledApplication(locator, errs, Location, rc));
} else {
errs.addError(sl_InvalidHrefValue(HowRelated.attr(dvbi.a_href).value(), HowRelated, tva.e_RelatedMaterial.elementize(), Location, `${errCode}-11`));
errs.errorDescription(RMErrorDescription(`${errCode}-14`, dvbi.e_ServiceList, 14));
}
break;
case SERVICE_RM:
if (validContentFinishedBanner(HowRelated, ANY_NAMESPACE) && SchemaVersion(props.namespace) == SCHEMA_r0)
errs.addError({
code: `${errCode}-21`,
message: `${HowRelated.attr(dvbi.href).value().quote()} not permitted for ${props.namespace.quote()} in ${Location}`,
key: "invalid CS value",
fragment: HowRelated,
});
if (
validOutScheduleHours(HowRelated, props.namespace) ||
validContentFinishedBanner(HowRelated, props.namespace) ||
validServiceLogo(HowRelated, props.namespace) ||
validServiceBanner(HowRelated, props.namespace)
) {
rc = HowRelated.attr(dvbi.a_href).value();
checkValidLogos(RelatedMaterial, errs, `${errCode}-22`, Location, this.#knownLanguages);
} else if (this.#validServiceApplication(HowRelated, SchemaVersion(props.namespace))) {
rc = HowRelated.attr(dvbi.a_href).value();
MediaLocator.forEach((locator) => this.#checkSignalledApplication(locator, errs, Location, rc));
} else {
errs.addError(sl_InvalidHrefValue(HowRelated.attr(dvbi.a_href).value(), HowRelated, tva.e_RelatedMaterial.elementize(), Location, `${errCode}-24`));
errs.errorDescription(RMErrorDescription(`${errCode}-24`, dvbi.e_Service, 15));
}
break;
case SERVICE_INSTANCE_RM:
if (validContentFinishedBanner(HowRelated, ANY_NAMESPACE) && SchemaVersion(props.namespace) == SCHEMA_r0)
errs.addError({
code: `${errCode}-31`,
message: `${HowRelated.attr(dvbi.href).value().quote()} not permitted for ${props.namespace.quote()} in ${Location}`,
key: "invalid CS value",
fragment: HowRelated,
});
else if (validContentFinishedBanner(HowRelated, props.namespace) || validServiceLogo(HowRelated, props.namespace)) {
rc = HowRelated.attr(dvbi.a_href).value();
checkValidLogos(RelatedMaterial, errs, `${errCode}-32`, Location, this.#knownLanguages);
} else if (validOutScheduleHours(HowRelated, ANY_NAMESPACE) && SchemaVersion(props.namespace) >= SCHEMA_r6) {
errs.addError({
code: `${errCode}-35`,
message: "Out of Service Banner is not permitted in a Service Instance from A177r6",
key: "misplaced image type",
fragment: HowRelated,
});
errs.errorDescription({
code: `${errCode}-35`,
clause: "A177 table 16",
description: `Out of Service banner is not permitted in the ${tva.e_RelatedMaterial.elementize()} element of a ${dvbi.e_ServiceInstance.elementize()}`,
});
} else if (validServiceBanner(HowRelated, props.namespace)) {
errs.addError({
code: `${errCode}-33`,
message: "Service Banner is not permitted in a Service Instance",
key: "misplaced image type",
fragment: HowRelated,
});
errs.errorDescription({
code: `${errCode}-33`,
clause: "A177 table 16",
description: `Service banner is not permitted in the ${tva.e_RelatedMaterial.elementize()} element of a ${dvbi.e_ServiceInstance.elementize()}`,
});
} else if (this.#validServiceApplication(HowRelated, SchemaVersion(props.namespace))) {
rc = HowRelated.attr(dvbi.a_href).value();
MediaLocator.forEach((locator) => this.#checkSignalledApplication(locator, errs, Location, rc));
} else {
errs.addError(sl_InvalidHrefValue(HowRelated.attr(dvbi.a_href).value(), HowRelated, tva.e_RelatedMaterial.elementize(), Location, `${errCode}-34`));
errs.errorDescription(RMErrorDescription(`${errCode}-34`, dvbi.e_ServiceInstace, 16));
}
break;
case CONTENT_GUIDE_RM:
if (validContentGuideSourceLogo(HowRelated, props.namespace)) {
rc = HowRelated.attr(dvbi.a_href).value();
checkValidLogos(RelatedMaterial, errs, `${errCode}-41`, Location, this.#knownLanguages);
} else {
errs.addError(sl_InvalidHrefValue(HowRelated.attr(dvbi.a_href).value(), HowRelated, tva.e_RelatedMaterial.elementize(), Location, `${errCode}-42`));
errs.errorDescription(RMErrorDescription(`${errCode}-42`, dvbi.e_ContentGuideSource, 20));
}
break;
}
}
AccessibilityAttribiutes.forEach((aa) => {
CheckAccessibilityAttributes(
aa,
{
AccessibilityPurposeCS: this.#accessibilityPurposes,
RequiredStandardVersionCS: this.RequiredStandardVersionCS,
RequiredOptionalFeatureCS: this.RequiredOptionalFeatureCS,
VideoCodecCS: this.#allowedVideoSchemes,
AudioCodecCS: this.#allowedAudioSchemes,
SubtitleCarriageCS: this.#subtitleCarriages,
SubtitleCodingFormatCS: this.#subtitleCodings,
SubtitlePurposeTypeCS: this.#subtitlePurposes,
KnownLanguages: this.#knownLanguages,
AudioPresentationCS: this.#audioPresentations,
},
errs,
`${errCode}-51`
);
});
return rc;
}
/**
* check if the node provided contains an RelatedMaterial element for a signalled application
*
* @param {object} props Metadata of the XML document
* @param {XMLnode} node The XML tree node (either a <Service>, <TestService> or a <ServiceInstance>) to be checked
* @returns {boolean} true if the node contains a <RelatedMaterial> element which signals an application else false
*/
/*private*/ #hasSignalledApplication(props, node) {
if (node) {
let i = 0,
elem;
while ((elem = node.get(xPath(props.prefix, tva.e_RelatedMaterial, ++i), props.schema)) != null) {
let hr = elem.get(xPath(props.prefix, tva.e_HowRelated), props.schema);
if (hr && this.#validServiceApplication(hr, SchemaVersion(props.namespace))) return true;
}
}
return false;
}
/**
* perform any validation on a ContentTypeSourceType element
*
* @param {object} props Metadata of the XML document
* @param {XMLnode} source The <ContentGuideSource> element to be checked
* @param {Class} errs Errors found in validaton
* @param {object} loc The 'location' in the XML document of the element being checked, if unspecified then this is set to be the name of the parent element
* @param {string} errCode Error code prefix to be used in reports
*/
/*private*/ #validateAContentGuideSource(props, source, errs, loc, errCode) {
function CheckEndpoint(elementName, suffix, MustEndWithSlash = false) {
let ep = source.get(xPath(props.prefix, elementName), props.schema);
if (ep) {
let epURL = getElementByTagName(ep, dvbi.e_URI);
if (epURL) {
if (!isHTTPPathURL(epURL.text())) errs.addError(InvalidURL(epURL.text(), ep, elementName, `${errCode}-${suffix}a`));
if (MustEndWithSlash && !epURL.text().endsWith("/"))
errs.addError({
type: WARNING,
code: `${errCode}-${suffix}b`,
message: `"${epURL.text()}" should end with a slash '/' for ${elementName.elementize()}`,
fragment: ep,
key: "not URL path",
});
}
if (ep.attr(dvbi.a_contentType) && ep.attr(dvbi.a_contentType).value() != XMLdocumentType)
errs.addError({
type: WARNING,
code: `${errCode}-${suffix + 1}`,
message: `${elementName.elementize(dvbi.a_contentType)} should contain ${XMLdocumentType}`,
fragment: ep,
key: `invalid @${dvbi.a_contentType}`,
});
}
}
if (!source) {
errs.addError({
type: APPLICATION,
code: "GS000",
message: "validateAContentGuideSource() called with source==null",
});
return;
}
loc = loc ? loc : source.parent().name().elementize();
checkXMLLangs(dvbi.e_Name, loc, source, errs, `${errCode}-1`, this.#knownLanguages);
checkXMLLangs(dvbi.e_ProviderName, loc, source, errs, `${errCode}-2`, this.#knownLanguages);
let rm = 0,
RelatedMaterial;
while ((RelatedMaterial = source.get(xPath(props.prefix, tva.e_RelatedMaterial, ++rm), props.schema)) != null)
this.#validateRelatedMaterial(props, RelatedMaterial, errs, loc, CONTENT_GUIDE_RM, `${errCode}-3`);
// ContentGuideSourceType::ScheduleInfoEndpoint - should be a URL
CheckEndpoint(dvbi.e_ScheduleInfoEndpoint, 4);
// ContentGuideSourceType::ProgramInfoEndpoint - should be a URL
CheckEndpoint(dvbi.e_ProgramInfoEndpoint, 6);
// ContentGuideSourceType::GroupInfoEndpoint - should be a URL and should end with a /
CheckEndpoint(dvbi.e_GroupInfoEndpoint, 8, SchemaVersion(props.namespace) >= SCHEMA_r5);
// ContentGuideSourceType::MoreEpisodesEndpoint - should be a URL
CheckEndpoint(dvbi.e_MoreEpisodesEndpoint, 10);
}
/**
* validate the language specified record any errors
*
* @param {object} validator the validation class to use
* @param {Class} errs errors found in validaton
* @param {XMLnode} node the XML node whose @lang attribute should be checked
* @param {string} parentLang the language of the XML element which is the parent of node
* @param {boolean} isRequired report an error if @lang is not explicitly stated
* @param {string} errCode error number to use instead of local values
* @returns {string} the @lang attribute of the node element of the parentLang if it does not exist of is not specified
*/
/* private */ #GetLanguage(validator, errs, node, parentLang, isRequired, errCode) {
if (!node) return parentLang;
if (!node.attr(tva.a_lang) && isRequired) {
errs.addError({
code: errCode,
message: `${tva.a_lang.attribute()} is required for ${node.name().quote()}`,
key: keys.k_UnspecifiedLanguage,
line: node.line(),
});
return parentLang;
}
if (!node.attr(tva.a_lang)) return parentLang;
let localLang = node.attr(tva.a_lang).value();
if (validator && localLang) checkLanguage(validator, localLang, node.name(), node, errs, errCode);
return localLang;
}
/**
* validate the SynopsisType elements
*
* @param {object} props Metadata of the XML document
* @param {XMLnode} Element the element whose children should be checked
* @param {string} ElementName the name of the child element to be checked
* @param {array} requiredLengths @length attributes that are required to be present
* @param {array} optionalLengths @length attributes that can optionally be present
* @param {string} parentLanguage the xml:lang of the parent element
* @param {Class} errs errors found in validaton
* @param {string} errCode error code prefix to be used in reports
*/
/*private*/ #ValidateSynopsisType(props, Element, ElementName, requiredLengths, optionalLengths, parentLanguage, errs, errCode) {
if (!Element) {
errs.addError({
type: APPLICATION,
code: "SY000",
message: "ValidateSynopsisType() called with Element==null",
});
return;
}
let synopsisLengthError = (elem, label, length) => `length of ${elementize(`${tva.a_length.attribute(elem)}=${label.quote()}`)} exceeds ${length} characters`;
let synopsisToShortError = (elem, label, length) => `length of ${elementize(`${tva.a_length.attribute(elem)}=${label.quote()}`)} is less than ${length} characters`;
let singleLengthLangError = (elem, length, lang) => `only a single ${elem.elementize()} is permitted per length (${length}) and language (${lang})`;
let requiredSynopsisError = (elem, length) => `a ${elem.elementize()} element with ${tva.a_length.attribute()}=${quote(length)} is required`;
let s = 0,
ste,
hasBrief = false,
hasShort = false,
hasMedium = false,
hasLong = false,
hasExtended = false;
let briefLangs = [],
shortLangs = [],
mediumLangs = [],
longLangs = [],
extendedLangs = [];
let ERROR_KEY = "synopsis";
while ((ste = Element.get(xPath(props.prefix, ElementName, ++s), props.schema)) != null) {
let synopsisLang = this.#GetLanguage(this.#knownLanguages, errs, ste, parentLanguage, false, `${errCode}-2`);
let synopsisLength = ste.attr(tva.a_length) ? ste.attr(tva.a_length).value() : null;
if (synopsisLength) {
let cleanSynopsisLength = unEntity(ste.text()).length; // replace ENTITY strings with a generic character
if (isIn(requiredLengths, synopsisLength) || isIn(optionalLengths, synopsisLength)) {
switch (synopsisLength) {
case tva.SYNOPSIS_BRIEF_LABEL:
if (cleanSynopsisLength > tva.SYNOPSIS_BRIEF_LENGTH)
errs.addError({
code: `${errCode}-10`,
message: synopsisLengthError(ElementName, tva.SYNOPSIS_BRIEF_LABEL, tva.SYNOPSIS_BRIEF_LENGTH),
fragment: ste,
key: ERROR_KEY,
});
hasBrief = true;
break;
case tva.SYNOPSIS_SHORT_LABEL:
if (cleanSynopsisLength > tva.SYNOPSIS_SHORT_LENGTH)
errs.addError({
code: `${errCode}-11`,
message: synopsisLengthError(ElementName, tva.SYNOPSIS_SHORT_LABEL, tva.SYNOPSIS_SHORT_LENGTH),
fragment: ste,
key: ERROR_KEY,
});
hasShort = true;
break;
case tva.SYNOPSIS_MEDIUM_LABEL:
if (cleanSynopsisLength > tva.SYNOPSIS_MEDIUM_LENGTH)
errs.addError({
code: `${errCode}-12`,
message: synopsisLengthError(ElementName, tva.SYNOPSIS_MEDIUM_LABEL, tva.SYNOPSIS_MEDIUM_LENGTH),
fragment: ste,
key: ERROR_KEY,
});
hasMedium = true;
break;
case tva.SYNOPSIS_LONG_LABEL:
if (cleanSynopsisLength > tva.SYNOPSIS_LONG_LENGTH)
errs.addError({
code: `${errCode}-13`,
message: synopsisLengthError(ElementName, tva.SYNOPSIS_LONG_LABEL, tva.SYNOPSIS_LONG_LENGTH),
fragment: ste,
key: ERROR_KEY,
});
hasLong = true;
break;
case tva.SYNOPSIS_EXTENDED_LABEL:
if (cleanSynopsisLength < tva.SYNOPSIS_LONG_LENGTH)
errs.addError({
code: `${errCode}-14`,
message: synopsisToShortError(ElementName, tva.SYNOPSIS_EXTENDED_LABEL, tva.SYNOPSIS_LONG_LENGTH),
fragment: ste,
key: ERROR_KEY,
});
hasExtended = true;
break;
}
} else
errs.addError({
code: `${errCode}-15`,
message: `${tva.a_length.attribute()}=${synopsisLength.quote()} is not permitted in ${ElementName.elementize()}`,
fragment: ste,
key: ERROR_KEY,
});
}
if (synopsisLang && synopsisLength)
switch (synopsisLength) {
case tva.SYNOPSIS_BRIEF_LABEL:
if (isIn(briefLangs, synopsisLang))
errs.addError({
code: `${errCode}-21`,
message: singleLengthLangError(ElementName, synopsisLength, synopsisLang),
fragment: ste,
key: ERROR_KEY,
});
else briefLangs.push(synopsisLang);
break;
case tva.SYNOPSIS_SHORT_LABEL:
if (isIn(shortLangs, synopsisLang))
errs.addError({
code: `${errCode}-22`,
message: singleLengthLangError(ElementName, synopsisLength, synopsisLang),
fragment: ste,
key: ERROR_KEY,
});
else shortLangs.push(synopsisLang);
break;
case tva.SYNOPSIS_MEDIUM_LABEL:
if (isIn(mediumLangs, synopsisLang))
errs.addError({
code: `${errCode}-23`,
message: singleLengthLangError(ElementName, synopsisLength, synopsisLang),
fragment: ste,
key: ERROR_KEY,
});
else mediumLangs.push(synopsisLang);
break;
case tva.SYNOPSIS_LONG_LABEL:
if (isIn(longLangs, synopsisLang))
errs.addError({
code: `${errCode}-24`,
message: singleLengthLangError(ElementName, synopsisLength, synopsisLang),
fragment: ste,
key: ERROR_KEY,
});
else longLangs.push(synopsisLang);
break;
case tva.SYNOPSIS_EXTENDED_LABEL:
if (isIn(extendedLangs, synopsisLang))
errs.addError({
code: `${errCode}-25`,
message: singleLengthLangError(ElementName, synopsisLength, synopsisLang),
fragment: ste,
key: ERROR_KEY,
});
else extendedLangs.push(synopsisLang);
break;
}
}
if (isIn(requiredLengths, tva.SYNOPSIS_BRIEF_LABEL) && !hasBrief)
errs.addError({
code: `${errCode}-31`,
message: requiredSynopsisError(tva.SYNOPSIS_BRIEF_LABEL),
fragment: Element,
key: ERROR_KEY,
});
if (isIn(requiredLengths, tva.SYNOPSIS_SHORT_LABEL) && !hasShort)
errs.addError({
code: `${errCode}-32`,
message: requiredSynopsisError(tva.SYNOPSIS_SHORT_LABEL),
fragment: Element,
key: ERROR_KEY,
});
if (isIn(requiredLengths, tva.SYNOPSIS_MEDIUM_LABEL) && !hasMedium)
errs.addError({
code: `${errCode}-33`,
message: requiredSynopsisError(tva.SYNOPSIS_MEDIUM_LABEL),
fragment: Element,
key: ERROR_KEY,
});
if (isIn(requiredLengths, tva.SYNOPSIS_LONG_LABEL) && !hasLong)
errs.addError({
code: `${errCode}-34`,
message: requiredSynopsisError(tva.SYNOPSIS_LONG_LABEL),
fragment: Element,
key: ERROR_KEY,