2
0

metadata.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.js");
  28. var _xml_parser = require("./xml_parser.js");
  29. class Metadata {
  30. constructor(data) {
  31. (0, _util.assert)(typeof data === "string", "Metadata: input is not a string");
  32. data = this._repair(data);
  33. const parser = new _xml_parser.SimpleXMLParser();
  34. const xmlDocument = parser.parseFromString(data);
  35. this._metadataMap = new Map();
  36. if (xmlDocument) {
  37. this._parse(xmlDocument);
  38. }
  39. }
  40. _repair(data) {
  41. return data.replace(/^[^<]+/, "").replace(/>\\376\\377([^<]+)/g, function (all, codes) {
  42. const bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
  43. return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
  44. }).replace(/&(amp|apos|gt|lt|quot);/g, function (str, name) {
  45. switch (name) {
  46. case "amp":
  47. return "&";
  48. case "apos":
  49. return "'";
  50. case "gt":
  51. return ">";
  52. case "lt":
  53. return "<";
  54. case "quot":
  55. return '"';
  56. }
  57. throw new Error(`_repair: ${name} isn't defined.`);
  58. });
  59. let chars = "";
  60. for (let i = 0, ii = bytes.length; i < ii; i += 2) {
  61. const code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
  62. if (code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38) {
  63. chars += String.fromCharCode(code);
  64. } else {
  65. chars += "&#x" + (0x10000 + code).toString(16).substring(1) + ";";
  66. }
  67. }
  68. return ">" + chars;
  69. });
  70. }
  71. _parse(xmlDocument) {
  72. let rdf = xmlDocument.documentElement;
  73. if (rdf.nodeName.toLowerCase() !== "rdf:rdf") {
  74. rdf = rdf.firstChild;
  75. while (rdf && rdf.nodeName.toLowerCase() !== "rdf:rdf") {
  76. rdf = rdf.nextSibling;
  77. }
  78. }
  79. const nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
  80. if (!rdf || nodeName !== "rdf:rdf" || !rdf.hasChildNodes()) {
  81. return;
  82. }
  83. const children = rdf.childNodes;
  84. for (let i = 0, ii = children.length; i < ii; i++) {
  85. const desc = children[i];
  86. if (desc.nodeName.toLowerCase() !== "rdf:description") {
  87. continue;
  88. }
  89. for (let j = 0, jj = desc.childNodes.length; j < jj; j++) {
  90. if (desc.childNodes[j].nodeName.toLowerCase() !== "#text") {
  91. const entry = desc.childNodes[j];
  92. const name = entry.nodeName.toLowerCase();
  93. this._metadataMap.set(name, entry.textContent.trim());
  94. }
  95. }
  96. }
  97. }
  98. get(name) {
  99. return this._metadataMap.has(name) ? this._metadataMap.get(name) : null;
  100. }
  101. getAll() {
  102. return Object.fromEntries(this._metadataMap);
  103. }
  104. has(name) {
  105. return this._metadataMap.has(name);
  106. }
  107. }
  108. exports.Metadata = Metadata;