metadata.js 4.6 KB

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