dataset_reader.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Copyright 2022 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { stringToUTF8String, warn } from "../shared/util.js";
  16. import { parseXFAPath } from "./core_utils.js";
  17. import { SimpleXMLParser } from "./xml_parser.js";
  18. function decodeString(str) {
  19. try {
  20. return stringToUTF8String(str);
  21. } catch (ex) {
  22. warn(`UTF-8 decoding failed: "${ex}".`);
  23. return str;
  24. }
  25. }
  26. class DatasetXMLParser extends SimpleXMLParser {
  27. constructor(options) {
  28. super(options);
  29. this.node = null;
  30. }
  31. onEndElement(name) {
  32. const node = super.onEndElement(name);
  33. if (node && name === "xfa:datasets") {
  34. this.node = node;
  35. // We don't need anything else, so just kill the parser.
  36. throw new Error("Aborting DatasetXMLParser.");
  37. }
  38. }
  39. }
  40. class DatasetReader {
  41. constructor(data) {
  42. if (data.datasets) {
  43. this.node = new SimpleXMLParser({ hasAttributes: true }).parseFromString(
  44. data.datasets
  45. ).documentElement;
  46. } else {
  47. const parser = new DatasetXMLParser({ hasAttributes: true });
  48. try {
  49. parser.parseFromString(data["xdp:xdp"]);
  50. } catch (_) {}
  51. this.node = parser.node;
  52. }
  53. }
  54. getValue(path) {
  55. if (!this.node || !path) {
  56. return "";
  57. }
  58. const node = this.node.searchNode(parseXFAPath(path), 0);
  59. if (!node) {
  60. return "";
  61. }
  62. const first = node.firstChild;
  63. if (first && first.nodeName === "value") {
  64. return node.children.map(child => decodeString(child.textContent));
  65. }
  66. return decodeString(node.textContent);
  67. }
  68. }
  69. export { DatasetReader };