builder.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.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.element.template[_xfa_object.$resolvePrototypes](this[_xfa_object.$ids]);
  47. this.element.template[_xfa_object.$ids] = this[_xfa_object.$ids];
  48. }
  49. }
  50. }
  51. class Empty extends _xfa_object.XFAObject {
  52. constructor() {
  53. super(-1, "", Object.create(null));
  54. }
  55. [_xfa_object.$onChild](_) {
  56. return false;
  57. }
  58. }
  59. class Builder {
  60. constructor() {
  61. this._namespaceStack = [];
  62. this._namespacePrefixes = new Map();
  63. this._namespaces = new Map();
  64. this._nextNsId = Math.max(...Object.values(_namespaces.NamespaceIds).map(({
  65. id
  66. }) => id));
  67. this._currentNamespace = new _unknown.UnknownNamespace(++this._nextNsId);
  68. }
  69. buildRoot(ids) {
  70. return new Root(ids);
  71. }
  72. build({
  73. nsPrefix,
  74. name,
  75. attributes,
  76. namespace,
  77. prefixes
  78. }) {
  79. const hasNamespaceDef = namespace !== null;
  80. if (hasNamespaceDef) {
  81. this._namespaceStack.push(this._currentNamespace);
  82. this._currentNamespace = this._searchNamespace(namespace);
  83. }
  84. if (prefixes) {
  85. this._addNamespacePrefix(prefixes);
  86. }
  87. if (attributes.hasOwnProperty(_xfa_object.$nsAttributes)) {
  88. const dataTemplate = _setup.NamespaceSetUp.datasets;
  89. const nsAttrs = attributes[_xfa_object.$nsAttributes];
  90. let xfaAttrs = null;
  91. for (const [ns, attrs] of Object.entries(nsAttrs)) {
  92. const nsToUse = this._getNamespaceToUse(ns);
  93. if (nsToUse === dataTemplate) {
  94. xfaAttrs = {
  95. xfa: attrs
  96. };
  97. break;
  98. }
  99. }
  100. if (xfaAttrs) {
  101. attributes[_xfa_object.$nsAttributes] = xfaAttrs;
  102. } else {
  103. delete attributes[_xfa_object.$nsAttributes];
  104. }
  105. }
  106. const namespaceToUse = this._getNamespaceToUse(nsPrefix);
  107. const node = namespaceToUse && namespaceToUse[_namespaces.$buildXFAObject](name, attributes) || new Empty();
  108. if (hasNamespaceDef || prefixes) {
  109. node[_xfa_object.$cleanup] = {
  110. hasNamespace: hasNamespaceDef,
  111. prefixes
  112. };
  113. }
  114. return node;
  115. }
  116. _searchNamespace(nsName) {
  117. let ns = this._namespaces.get(nsName);
  118. if (ns) {
  119. return ns;
  120. }
  121. for (const [name, {
  122. check
  123. }] of Object.entries(_namespaces.NamespaceIds)) {
  124. if (check(nsName)) {
  125. ns = _setup.NamespaceSetUp[name];
  126. if (ns) {
  127. this._namespaces.set(nsName, ns);
  128. return ns;
  129. }
  130. break;
  131. }
  132. }
  133. ns = new _unknown.UnknownNamespace(++this._nextNsId);
  134. this._namespaces.set(nsName, ns);
  135. return ns;
  136. }
  137. _addNamespacePrefix(prefixes) {
  138. for (const {
  139. prefix,
  140. value
  141. } of prefixes) {
  142. const namespace = this._searchNamespace(value);
  143. let prefixStack = this._namespacePrefixes.get(prefix);
  144. if (!prefixStack) {
  145. prefixStack = [];
  146. this._namespacePrefixes.set(prefix, prefixStack);
  147. }
  148. prefixStack.push(namespace);
  149. }
  150. }
  151. _getNamespaceToUse(prefix) {
  152. if (!prefix) {
  153. return this._currentNamespace;
  154. }
  155. const prefixStack = this._namespacePrefixes.get(prefix);
  156. if (prefixStack && prefixStack.length > 0) {
  157. return prefixStack[prefixStack.length - 1];
  158. }
  159. (0, _util.warn)(`Unknown namespace prefix: ${prefix}.`);
  160. return null;
  161. }
  162. clean(data) {
  163. const {
  164. hasNamespace,
  165. prefixes
  166. } = data;
  167. if (hasNamespace) {
  168. this._currentNamespace = this._namespaceStack.pop();
  169. }
  170. if (prefixes) {
  171. prefixes.forEach(({
  172. prefix
  173. }) => {
  174. this._namespacePrefixes.get(prefix).pop();
  175. });
  176. }
  177. }
  178. }
  179. exports.Builder = Builder;