1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | /* istanbul ignore next */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); define(["require", "exports", "./xml-writer", "./../utils", "./xfdf-document"], function (require, exports, xml_writer_1, utils_1, xfdf_document_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _XmlDocument = (function (_super) { __extends(_XmlDocument, _super); function _XmlDocument(fileName) { var _this = _super.call(this) || this; if (fileName !== null && typeof fileName !== 'undefined') { _this._fileName = fileName; } return _this; } _XmlDocument.prototype._exportAnnotations = function () { throw new Error('Method not implemented.'); }; _XmlDocument.prototype._exportFormFields = function (document) { this._document = document; this._crossReference = document._crossReference; this._isAnnotationExport = false; this._format = 'XML'; this._key = utils_1._getNewGuidString(); return this._save(); }; _XmlDocument.prototype._save = function () { var writer = new xml_writer_1._XmlWriter(); writer._writeStartDocument(); if (this._asPerSpecification) { writer._writeStartElement('fields'); writer._writeAttributeString('xfdf', 'http://ns.adobe.com/xfdf-transition/', 'xmlns', null); } else { writer._writeStartElement('Fields'); } var form = this._document.form; if (form !== null && typeof form !== 'undefined') { this._exportEmptyFields = form.exportEmptyFields; var count = this._document.form.count; for (var i = 0; i < count; i++) { var field = this._document.form.fieldAt(i); if (field !== null && typeof field !== 'undefined' && field.export) { this._exportFormFieldData(field); } } this._writeFormFieldData(writer, this._asPerSpecification); } var result = writer._save(); writer._destroy(); return result; }; _XmlDocument.prototype._writeFormFieldData = function (writer, isAcrobat) { if (isAcrobat === void 0) { isAcrobat = false; } if (isAcrobat) { this._table.forEach(function (value, key) { if (key.includes(' ')) { var text = key.replace(/ /g, ''); writer._writeStartElement(text.toString()); writer._writeAttributeString('original', key.toString(), 'xfdf', null); } else { writer._writeStartElement(key.toString()); } writer._writeString(value.toString()); writer._writeEndElement(); }); } else { this._table.forEach(function (value, key) { if (key.includes(' ')) { key = key.replace(/ /g, '_x0020_'); } writer._writeStartElement(key.toString()); writer._writeString(value.toString()); writer._writeEndElement(); }); } writer._writeEndElement(); }; _XmlDocument.prototype._importFormData = function (document, data) { this._document = document; this._crossReference = document._crossReference; this._isAnnotationExport = false; var value = utils_1._bytesToString(data); value = value.replace(/(\r\n|\n|\r)/gm, ''); value = value.replace(/[^\x20-\x7E]/g, ''); this._xmlDocument = (new DOMParser()).parseFromString(value, 'text/xml'); this._checkXml(this._xmlDocument); this._xmlImport = true; this._parseFormData(this._xmlDocument.documentElement); this._xmlImport = false; }; _XmlDocument.prototype._parseFormData = function (root) { var child = root.childNodes; if (child !== null && typeof child !== 'undefined' && child.length > 0) { for (var i = 0; i < child.length; i++) { var childNode = child.item(i); if (childNode !== null && typeof childNode !== 'undefined' && childNode.nodeType === 1) { var element = childNode; var text = ''; if (element.attributes !== null && typeof element.attributes !== 'undefined' && element.attributes.length > 0) { var attribute = element.attributes.item(0); if (attribute !== null && typeof attribute !== 'undefined' && attribute.name === 'xfdf:original') { text = attribute.value; } } else { text = element.tagName; } var v = element.textContent; if (text !== null && text !== undefined && text.length > 0) { this._table.set(text, v); } } } } this._importField(); }; _XmlDocument.prototype._importField = function () { var _this = this; var form = this._document.form; var count = form.count; if (count) { this._table.forEach(function (value, key) { var textValue; if (_this._table.size > 0 && _this._table.has(key)) { textValue = _this._table.get(key); } var text = key.toString(); if (text.indexOf('_x0020_') !== -1) { text = text.replace(/_x0020_/g, ' '); } var index = form._getFieldIndex(text); if (index !== -1 && index < count) { var field = form.fieldAt(index); if (field && field !== null && typeof field !== 'undefined') { if (textValue && textValue !== '') { field._dictionary.update('RV', textValue); } var param = []; param.push(value); _this._importFieldData(field, param); } } }); } }; _XmlDocument.prototype._checkXml = function (xmlDocument) { if (xmlDocument.getElementsByTagName('parsererror').length > 0) { throw new Error('Invalid XML file.'); } }; return _XmlDocument; }(xfdf_document_1._ExportHelper)); exports._XmlDocument = _XmlDocument; }); |