2012-09-01 07:48:21 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2012-03-29 03:09:03 +09:00
|
|
|
|
2017-10-15 20:27:10 +09:00
|
|
|
import { assert } from '../shared/util';
|
2018-03-17 06:48:08 +09:00
|
|
|
import { SimpleXMLParser } from './xml_parser';
|
2017-09-14 06:37:51 +09:00
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
class Metadata {
|
|
|
|
constructor(data) {
|
2017-09-14 06:37:51 +09:00
|
|
|
assert(typeof data === 'string', 'Metadata: input is not a string');
|
|
|
|
|
|
|
|
// Ghostscript may produce invalid metadata, so try to repair that first.
|
|
|
|
data = this._repair(data);
|
|
|
|
|
2018-03-17 06:48:08 +09:00
|
|
|
// Convert the string to an XML document.
|
2017-09-14 06:37:51 +09:00
|
|
|
let parser = new SimpleXMLParser();
|
2018-03-17 06:48:08 +09:00
|
|
|
const xmlDocument = parser.parseFromString(data);
|
2012-03-25 03:59:51 +09:00
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
this._metadata = Object.create(null);
|
2017-04-02 21:25:33 +09:00
|
|
|
|
2018-03-17 06:48:08 +09:00
|
|
|
if (xmlDocument) {
|
|
|
|
this._parse(xmlDocument);
|
|
|
|
}
|
2012-03-25 03:59:51 +09:00
|
|
|
}
|
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
_repair(data) {
|
2019-01-16 20:37:21 +09:00
|
|
|
// Start by removing any "junk" before the first tag (see issue 10395).
|
|
|
|
return data.replace(/^([^<]+)/, '').replace(/>\\376\\377([^<]+)/g,
|
|
|
|
function(all, codes) {
|
2017-09-14 06:22:53 +09:00
|
|
|
let bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g,
|
|
|
|
function(code, d1, d2, d3) {
|
|
|
|
return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
|
2017-12-13 22:22:39 +09:00
|
|
|
}).replace(/&(amp|apos|gt|lt|quot);/g, function(str, name) {
|
|
|
|
switch (name) {
|
|
|
|
case 'amp':
|
|
|
|
return '&';
|
|
|
|
case 'apos':
|
|
|
|
return '\'';
|
|
|
|
case 'gt':
|
|
|
|
return '>';
|
|
|
|
case 'lt':
|
|
|
|
return '<';
|
|
|
|
case 'quot':
|
|
|
|
return '\"';
|
|
|
|
}
|
|
|
|
throw new Error(`_repair: ${name} isn't defined.`);
|
2017-09-14 06:22:53 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
let chars = '';
|
|
|
|
for (let i = 0, ii = bytes.length; i < ii; i += 2) {
|
|
|
|
let code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
|
|
|
|
if (code >= 32 && code < 127 && code !== 60 && code !== 62 &&
|
|
|
|
code !== 38) {
|
|
|
|
chars += String.fromCharCode(code);
|
|
|
|
} else {
|
|
|
|
chars += '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '>' + chars;
|
|
|
|
});
|
|
|
|
}
|
2012-03-29 02:15:59 +09:00
|
|
|
|
2018-03-17 06:48:08 +09:00
|
|
|
_parse(xmlDocument) {
|
|
|
|
let rdf = xmlDocument.documentElement;
|
2012-03-28 05:37:02 +09:00
|
|
|
|
2017-04-02 21:25:33 +09:00
|
|
|
if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') { // Wrapped in <xmpmeta>
|
|
|
|
rdf = rdf.firstChild;
|
|
|
|
while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
|
|
|
|
rdf = rdf.nextSibling;
|
2014-03-09 20:41:01 +09:00
|
|
|
}
|
2017-04-02 21:25:33 +09:00
|
|
|
}
|
2012-03-25 03:59:51 +09:00
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
let nodeName = rdf ? rdf.nodeName.toLowerCase() : null;
|
2017-04-02 21:25:33 +09:00
|
|
|
if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-25 03:59:51 +09:00
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
let children = rdf.childNodes;
|
|
|
|
for (let i = 0, ii = children.length; i < ii; i++) {
|
|
|
|
let desc = children[i];
|
2017-04-02 21:25:33 +09:00
|
|
|
if (desc.nodeName.toLowerCase() !== 'rdf:description') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
for (let j = 0, jj = desc.childNodes.length; j < jj; j++) {
|
|
|
|
if (desc.childNodes[j].nodeName.toLowerCase() !== '#text') {
|
|
|
|
let entry = desc.childNodes[j];
|
|
|
|
let name = entry.nodeName.toLowerCase();
|
|
|
|
|
|
|
|
this._metadata[name] = entry.textContent.trim();
|
2012-03-25 03:59:51 +09:00
|
|
|
}
|
|
|
|
}
|
2017-04-02 21:25:33 +09:00
|
|
|
}
|
2017-09-14 06:22:53 +09:00
|
|
|
}
|
2012-03-25 03:59:51 +09:00
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
get(name) {
|
2019-01-16 20:37:21 +09:00
|
|
|
const data = this._metadata[name];
|
|
|
|
return (typeof data !== 'undefined' ? data : null);
|
2017-09-14 06:22:53 +09:00
|
|
|
}
|
2012-03-25 03:59:51 +09:00
|
|
|
|
2017-09-14 06:37:51 +09:00
|
|
|
getAll() {
|
|
|
|
return this._metadata;
|
|
|
|
}
|
|
|
|
|
2017-09-14 06:22:53 +09:00
|
|
|
has(name) {
|
|
|
|
return typeof this._metadata[name] !== 'undefined';
|
|
|
|
}
|
|
|
|
}
|
2012-03-25 03:59:51 +09:00
|
|
|
|
2017-04-02 21:25:33 +09:00
|
|
|
export {
|
|
|
|
Metadata,
|
|
|
|
};
|