2
0

builder.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.Builder = void 0;
  27. var _namespaces = require("./namespaces.js");
  28. var _xfa_object = require("./xfa_object.js");
  29. var _setup = require("./setup.js");
  30. var _template = require("./template.js");
  31. var _unknown = require("./unknown.js");
  32. var _util = require("../../shared/util.js");
  33. class Root extends _xfa_object.XFAObject {
  34. constructor(ids) {
  35. super(-1, "root", Object.create(null));
  36. this.element = null;
  37. this[_xfa_object.$ids] = ids;
  38. }
  39. [_xfa_object.$onChild](child) {
  40. this.element = child;
  41. return true;
  42. }
  43. [_xfa_object.$finalize]() {
  44. super[_xfa_object.$finalize]();
  45. if (this.element.template instanceof _template.Template) {
  46. this[_xfa_object.$ids].set(_xfa_object.$root, this.element);
  47. this.element.template[_xfa_object.$resolvePrototypes](this[_xfa_object.$ids]);
  48. this.element.template[_xfa_object.$ids] = this[_xfa_object.$ids];
  49. }
  50. }
  51. }
  52. class Empty extends _xfa_object.XFAObject {
  53. constructor() {
  54. super(-1, "", Object.create(null));
  55. }
  56. [_xfa_object.$onChild](_) {
  57. return false;
  58. }
  59. }
  60. class Builder {
  61. constructor(rootNameSpace = null) {
  62. this._namespaceStack = [];
  63. this._nsAgnosticLevel = 0;
  64. this._namespacePrefixes = new Map();
  65. this._namespaces = new Map();
  66. this._nextNsId = Math.max(...Object.values(_namespaces.NamespaceIds).map(({
  67. id
  68. }) => id));
  69. this._currentNamespace = rootNameSpace || new _unknown.UnknownNamespace(++this._nextNsId);
  70. }
  71. buildRoot(ids) {
  72. return new Root(ids);
  73. }
  74. build({
  75. nsPrefix,
  76. name,
  77. attributes,
  78. namespace,
  79. prefixes
  80. }) {
  81. const hasNamespaceDef = namespace !== null;
  82. if (hasNamespaceDef) {
  83. this._namespaceStack.push(this._currentNamespace);
  84. this._currentNamespace = this._searchNamespace(namespace);
  85. }
  86. if (prefixes) {
  87. this._addNamespacePrefix(prefixes);
  88. }
  89. if (attributes.hasOwnProperty(_xfa_object.$nsAttributes)) {
  90. const dataTemplate = _setup.NamespaceSetUp.datasets;
  91. const nsAttrs = attributes[_xfa_object.$nsAttributes];
  92. let xfaAttrs = null;
  93. for (const [ns, attrs] of Object.entries(nsAttrs)) {
  94. const nsToUse = this._getNamespaceToUse(ns);
  95. if (nsToUse === dataTemplate) {
  96. xfaAttrs = {
  97. xfa: attrs
  98. };
  99. break;
  100. }
  101. }
  102. if (xfaAttrs) {
  103. attributes[_xfa_object.$nsAttributes] = xfaAttrs;
  104. } else {
  105. delete attributes[_xfa_object.$nsAttributes];
  106. }
  107. }
  108. const namespaceToUse = this._getNamespaceToUse(nsPrefix);
  109. const node = namespaceToUse && namespaceToUse[_namespaces.$buildXFAObject](name, attributes) || new Empty();
  110. if (node[_xfa_object.$isNsAgnostic]()) {
  111. this._nsAgnosticLevel++;
  112. }
  113. if (hasNamespaceDef || prefixes || node[_xfa_object.$isNsAgnostic]()) {
  114. node[_xfa_object.$cleanup] = {
  115. hasNamespace: hasNamespaceDef,
  116. prefixes,
  117. nsAgnostic: node[_xfa_object.$isNsAgnostic]()
  118. };
  119. }
  120. return node;
  121. }
  122. isNsAgnostic() {
  123. return this._nsAgnosticLevel > 0;
  124. }
  125. _searchNamespace(nsName) {
  126. let ns = this._namespaces.get(nsName);
  127. if (ns) {
  128. return ns;
  129. }
  130. for (const [name, {
  131. check
  132. }] of Object.entries(_namespaces.NamespaceIds)) {
  133. if (check(nsName)) {
  134. ns = _setup.NamespaceSetUp[name];
  135. if (ns) {
  136. this._namespaces.set(nsName, ns);
  137. return ns;
  138. }
  139. break;
  140. }
  141. }
  142. ns = new _unknown.UnknownNamespace(++this._nextNsId);
  143. this._namespaces.set(nsName, ns);
  144. return ns;
  145. }
  146. _addNamespacePrefix(prefixes) {
  147. for (const {
  148. prefix,
  149. value
  150. } of prefixes) {
  151. const namespace = this._searchNamespace(value);
  152. let prefixStack = this._namespacePrefixes.get(prefix);
  153. if (!prefixStack) {
  154. prefixStack = [];
  155. this._namespacePrefixes.set(prefix, prefixStack);
  156. }
  157. prefixStack.push(namespace);
  158. }
  159. }
  160. _getNamespaceToUse(prefix) {
  161. if (!prefix) {
  162. return this._currentNamespace;
  163. }
  164. const prefixStack = this._namespacePrefixes.get(prefix);
  165. if (prefixStack && prefixStack.length > 0) {
  166. return prefixStack.at(-1);
  167. }
  168. (0, _util.warn)(`Unknown namespace prefix: ${prefix}.`);
  169. return null;
  170. }
  171. clean(data) {
  172. const {
  173. hasNamespace,
  174. prefixes,
  175. nsAgnostic
  176. } = data;
  177. if (hasNamespace) {
  178. this._currentNamespace = this._namespaceStack.pop();
  179. }
  180. if (prefixes) {
  181. prefixes.forEach(({
  182. prefix
  183. }) => {
  184. this._namespacePrefixes.get(prefix).pop();
  185. });
  186. }
  187. if (nsAgnostic) {
  188. this._nsAgnosticLevel--;
  189. }
  190. }
  191. }
  192. exports.Builder = Builder;