2
0

metadata.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.Metadata = void 0;
  27. var _util = require("../shared/util");
  28. var _xml_parser = require("./xml_parser");
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. var Metadata =
  33. /*#__PURE__*/
  34. function () {
  35. function Metadata(data) {
  36. _classCallCheck(this, Metadata);
  37. (0, _util.assert)(typeof data === 'string', 'Metadata: input is not a string');
  38. data = this._repair(data);
  39. var parser = new _xml_parser.SimpleXMLParser();
  40. var xmlDocument = parser.parseFromString(data);
  41. this._metadata = Object.create(null);
  42. if (xmlDocument) {
  43. this._parse(xmlDocument);
  44. }
  45. }
  46. _createClass(Metadata, [{
  47. key: "_repair",
  48. value: function _repair(data) {
  49. return data.replace(/^([^<]+)/, '').replace(/>\\376\\377([^<]+)/g, function (all, codes) {
  50. var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
  51. return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
  52. }).replace(/&(amp|apos|gt|lt|quot);/g, function (str, name) {
  53. switch (name) {
  54. case 'amp':
  55. return '&';
  56. case 'apos':
  57. return '\'';
  58. case 'gt':
  59. return '>';
  60. case 'lt':
  61. return '<';
  62. case 'quot':
  63. return '\"';
  64. }
  65. throw new Error("_repair: ".concat(name, " isn't defined."));
  66. });
  67. var chars = '';
  68. for (var i = 0, ii = bytes.length; i < ii; i += 2) {
  69. var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
  70. if (code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38) {
  71. chars += String.fromCharCode(code);
  72. } else {
  73. chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
  74. }
  75. }
  76. return '>' + chars;
  77. });
  78. }
  79. }, {
  80. key: "_parse",
  81. value: function _parse(xmlDocument) {
  82. var rdf = xmlDocument.documentElement;
  83. if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  84. rdf = rdf.firstChild;
  85. while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  86. rdf = rdf.nextSibling;
  87. }
  88. }
  89. var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
  90. if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
  91. return;
  92. }
  93. var children = rdf.childNodes;
  94. for (var i = 0, ii = children.length; i < ii; i++) {
  95. var desc = children[i];
  96. if (desc.nodeName.toLowerCase() !== 'rdf:description') {
  97. continue;
  98. }
  99. for (var j = 0, jj = desc.childNodes.length; j < jj; j++) {
  100. if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
  101. var entry = desc.childNodes[j];
  102. var name = entry.nodeName.toLowerCase();
  103. this._metadata[name] = entry.textContent.trim();
  104. }
  105. }
  106. }
  107. }
  108. }, {
  109. key: "get",
  110. value: function get(name) {
  111. var data = this._metadata[name];
  112. return typeof data !== 'undefined' ? data : null;
  113. }
  114. }, {
  115. key: "getAll",
  116. value: function getAll() {
  117. return this._metadata;
  118. }
  119. }, {
  120. key: "has",
  121. value: function has(name) {
  122. return typeof this._metadata[name] !== 'undefined';
  123. }
  124. }]);
  125. return Metadata;
  126. }();
  127. exports.Metadata = Metadata;