1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | "use strict";
|
23 |
|
24 | Object.defineProperty(exports, "__esModule", {
|
25 | value: true
|
26 | });
|
27 | exports.Metadata = void 0;
|
28 |
|
29 | var _util = require("../shared/util");
|
30 |
|
31 | var _xml_parser = require("./xml_parser");
|
32 |
|
33 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
34 |
|
35 | 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); } }
|
36 |
|
37 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
38 |
|
39 | var Metadata =
|
40 |
|
41 | function () {
|
42 | function Metadata(data) {
|
43 | _classCallCheck(this, Metadata);
|
44 |
|
45 | (0, _util.assert)(typeof data === 'string', 'Metadata: input is not a string');
|
46 | data = this._repair(data);
|
47 | var parser = new _xml_parser.SimpleXMLParser();
|
48 | var xmlDocument = parser.parseFromString(data);
|
49 | this._metadata = Object.create(null);
|
50 |
|
51 | if (xmlDocument) {
|
52 | this._parse(xmlDocument);
|
53 | }
|
54 | }
|
55 |
|
56 | _createClass(Metadata, [{
|
57 | key: "_repair",
|
58 | value: function _repair(data) {
|
59 | return data.replace(/^([^<]+)/, '').replace(/>\\376\\377([^<]+)/g, function (all, codes) {
|
60 | var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g, function (code, d1, d2, d3) {
|
61 | return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
|
62 | }).replace(/&(amp|apos|gt|lt|quot);/g, function (str, name) {
|
63 | switch (name) {
|
64 | case 'amp':
|
65 | return '&';
|
66 |
|
67 | case 'apos':
|
68 | return '\'';
|
69 |
|
70 | case 'gt':
|
71 | return '>';
|
72 |
|
73 | case 'lt':
|
74 | return '<';
|
75 |
|
76 | case 'quot':
|
77 | return '\"';
|
78 | }
|
79 |
|
80 | throw new Error("_repair: ".concat(name, " isn't defined."));
|
81 | });
|
82 | var chars = '';
|
83 |
|
84 | for (var i = 0, ii = bytes.length; i < ii; i += 2) {
|
85 | var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
|
86 |
|
87 | if (code >= 32 && code < 127 && code !== 60 && code !== 62 && code !== 38) {
|
88 | chars += String.fromCharCode(code);
|
89 | } else {
|
90 | chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
|
91 | }
|
92 | }
|
93 |
|
94 | return '>' + chars;
|
95 | });
|
96 | }
|
97 | }, {
|
98 | key: "_parse",
|
99 | value: function _parse(xmlDocument) {
|
100 | var rdf = xmlDocument.documentElement;
|
101 |
|
102 | if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
|
103 | rdf = rdf.firstChild;
|
104 |
|
105 | while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
|
106 | rdf = rdf.nextSibling;
|
107 | }
|
108 | }
|
109 |
|
110 | var nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
|
111 |
|
112 | if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
|
113 | return;
|
114 | }
|
115 |
|
116 | var children = rdf.childNodes;
|
117 |
|
118 | for (var i = 0, ii = children.length; i < ii; i++) {
|
119 | var desc = children[i];
|
120 |
|
121 | if (desc.nodeName.toLowerCase() !== 'rdf:description') {
|
122 | continue;
|
123 | }
|
124 |
|
125 | for (var j = 0, jj = desc.childNodes.length; j < jj; j++) {
|
126 | if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
|
127 | var entry = desc.childNodes[j];
|
128 | var name = entry.nodeName.toLowerCase();
|
129 | this._metadata[name] = entry.textContent.trim();
|
130 | }
|
131 | }
|
132 | }
|
133 | }
|
134 | }, {
|
135 | key: "get",
|
136 | value: function get(name) {
|
137 | var data = this._metadata[name];
|
138 | return typeof data !== 'undefined' ? data : null;
|
139 | }
|
140 | }, {
|
141 | key: "getAll",
|
142 | value: function getAll() {
|
143 | return this._metadata;
|
144 | }
|
145 | }, {
|
146 | key: "has",
|
147 | value: function has(name) {
|
148 | return typeof this._metadata[name] !== 'undefined';
|
149 | }
|
150 | }]);
|
151 |
|
152 | return Metadata;
|
153 | }();
|
154 |
|
155 | exports.Metadata = Metadata; |
\ | No newline at end of file |