parser.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.XFAParser = void 0;
  27. var _xfa_object = require("./xfa_object.js");
  28. var _xml_parser = require("../xml_parser.js");
  29. var _builder = require("./builder.js");
  30. var _util = require("../../shared/util.js");
  31. class XFAParser extends _xml_parser.XMLParserBase {
  32. constructor() {
  33. super();
  34. this._builder = new _builder.Builder();
  35. this._stack = [];
  36. this._ids = new Map();
  37. this._current = this._builder.buildRoot(this._ids);
  38. this._errorCode = _xml_parser.XMLParserErrorCode.NoError;
  39. this._whiteRegex = /^\s+$/;
  40. }
  41. parse(data) {
  42. this.parseXml(data);
  43. if (this._errorCode !== _xml_parser.XMLParserErrorCode.NoError) {
  44. return undefined;
  45. }
  46. this._current[_xfa_object.$finalize]();
  47. return this._current.element;
  48. }
  49. onText(text) {
  50. if (this._whiteRegex.test(text)) {
  51. return;
  52. }
  53. this._current[_xfa_object.$onText](text.trim());
  54. }
  55. onCdata(text) {
  56. this._current[_xfa_object.$onText](text);
  57. }
  58. _mkAttributes(attributes, tagName) {
  59. let namespace = null;
  60. let prefixes = null;
  61. const attributeObj = Object.create({});
  62. for (const {
  63. name,
  64. value
  65. } of attributes) {
  66. if (name === "xmlns") {
  67. if (!namespace) {
  68. namespace = value;
  69. } else {
  70. (0, _util.warn)(`XFA - multiple namespace definition in <${tagName}>`);
  71. }
  72. } else if (name.startsWith("xmlns:")) {
  73. const prefix = name.substring("xmlns:".length);
  74. if (!prefixes) {
  75. prefixes = [];
  76. }
  77. prefixes.push({
  78. prefix,
  79. value
  80. });
  81. } else {
  82. const i = name.indexOf(":");
  83. if (i === -1) {
  84. attributeObj[name] = value;
  85. } else {
  86. let nsAttrs = attributeObj[_xfa_object.$nsAttributes];
  87. if (!nsAttrs) {
  88. nsAttrs = attributeObj[_xfa_object.$nsAttributes] = Object.create(null);
  89. }
  90. const [ns, attrName] = [name.slice(0, i), name.slice(i + 1)];
  91. let attrs = nsAttrs[ns];
  92. if (!attrs) {
  93. attrs = nsAttrs[ns] = Object.create(null);
  94. }
  95. attrs[attrName] = value;
  96. }
  97. }
  98. }
  99. return [namespace, prefixes, attributeObj];
  100. }
  101. _getNameAndPrefix(name) {
  102. const i = name.indexOf(":");
  103. if (i === -1) {
  104. return [name, null];
  105. }
  106. return [name.substring(i + 1), name.substring(0, i)];
  107. }
  108. onBeginElement(tagName, attributes, isEmpty) {
  109. const [namespace, prefixes, attributesObj] = this._mkAttributes(attributes, tagName);
  110. const [name, nsPrefix] = this._getNameAndPrefix(tagName);
  111. const node = this._builder.build({
  112. nsPrefix,
  113. name,
  114. attributes: attributesObj,
  115. namespace,
  116. prefixes
  117. });
  118. if (isEmpty) {
  119. node[_xfa_object.$finalize]();
  120. if (this._current[_xfa_object.$onChild](node)) {
  121. node[_xfa_object.$setId](this._ids);
  122. }
  123. node[_xfa_object.$clean](this._builder);
  124. return;
  125. }
  126. this._stack.push(this._current);
  127. this._current = node;
  128. }
  129. onEndElement(name) {
  130. const node = this._current;
  131. node[_xfa_object.$finalize]();
  132. this._current = this._stack.pop();
  133. if (this._current[_xfa_object.$onChild](node)) {
  134. node[_xfa_object.$setId](this._ids);
  135. }
  136. node[_xfa_object.$clean](this._builder);
  137. }
  138. onError(code) {
  139. this._errorCode = code;
  140. }
  141. }
  142. exports.XFAParser = XFAParser;