metadata.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var sharedUtil = require('../shared/util.js');
  17. var error = sharedUtil.error;
  18. function fixMetadata(meta) {
  19. return meta.replace(/>\\376\\377([^<]+)/g, function (all, codes) {
  20. var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
  21. return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
  22. });
  23. var chars = '';
  24. for (var i = 0; i < bytes.length; i += 2) {
  25. var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
  26. chars += code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38 ? String.fromCharCode(code) : '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
  27. }
  28. return '>' + chars;
  29. });
  30. }
  31. function Metadata(meta) {
  32. if (typeof meta === 'string') {
  33. meta = fixMetadata(meta);
  34. var parser = new DOMParser();
  35. meta = parser.parseFromString(meta, 'application/xml');
  36. } else if (!(meta instanceof Document)) {
  37. error('Metadata: Invalid metadata object');
  38. }
  39. this.metaDocument = meta;
  40. this.metadata = Object.create(null);
  41. this.parse();
  42. }
  43. Metadata.prototype = {
  44. parse: function Metadata_parse() {
  45. var doc = this.metaDocument;
  46. var rdf = doc.documentElement;
  47. if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  48. rdf = rdf.firstChild;
  49. while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
  50. rdf = rdf.nextSibling;
  51. }
  52. }
  53. var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
  54. if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
  55. return;
  56. }
  57. var children = rdf.childNodes,
  58. desc,
  59. entry,
  60. name,
  61. i,
  62. ii,
  63. length,
  64. iLength;
  65. for (i = 0, length = children.length; i < length; i++) {
  66. desc = children[i];
  67. if (desc.nodeName.toLowerCase() !== 'rdf:description') {
  68. continue;
  69. }
  70. for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) {
  71. if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') {
  72. entry = desc.childNodes[ii];
  73. name = entry.nodeName.toLowerCase();
  74. this.metadata[name] = entry.textContent.trim();
  75. }
  76. }
  77. }
  78. },
  79. get: function Metadata_get(name) {
  80. return this.metadata[name] || null;
  81. },
  82. has: function Metadata_has(name) {
  83. return typeof this.metadata[name] !== 'undefined';
  84. }
  85. };
  86. exports.Metadata = Metadata;