Mxl.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. var Xml_1 = require("./Xml");
  3. var es6_promise_1 = require("es6-promise");
  4. var JSZip = require("jszip");
  5. // Usage for extractSheetMusicFromMxl:
  6. // extractSheetFromMxl(" *** binary content *** ").then(
  7. // (score: IXmlElement) => {
  8. // // Success! use the score here!
  9. // },
  10. // (error: any) => {
  11. // // There was an error.
  12. // // Handle it here.
  13. // }
  14. // )
  15. function extractSheetFromMxl(data) {
  16. "use strict";
  17. var zip = new JSZip();
  18. // asynchronously load zip file and process it - with Promises
  19. return zip.loadAsync(data).then(function (_) {
  20. return zip.file("META-INF/container.xml").async("string");
  21. }, function (err) {
  22. throw err;
  23. }).then(function (content) {
  24. var parser = new DOMParser();
  25. var doc = parser.parseFromString(content, "text/xml");
  26. var rootFile = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
  27. return zip.file(rootFile).async("string");
  28. }, function (err) {
  29. throw err;
  30. }).then(function (content) {
  31. var parser = new DOMParser();
  32. var xml = parser.parseFromString(content, "text/xml");
  33. var doc = new Xml_1.IXmlElement(xml.documentElement);
  34. return es6_promise_1.Promise.resolve(doc);
  35. }, function (err) {
  36. throw err;
  37. }).then(function (content) {
  38. return es6_promise_1.Promise.resolve(content);
  39. }, function (err) {
  40. throw new Error("extractSheetFromMxl: " + err.message);
  41. });
  42. }
  43. exports.extractSheetFromMxl = extractSheetFromMxl;
  44. function openMxl(data) {
  45. "use strict";
  46. var zip = new JSZip();
  47. // asynchronously load zip file and process it - with Promises
  48. return zip.loadAsync(data).then(function (_) {
  49. return zip.file("META-INF/container.xml").async("string");
  50. }, function (err) {
  51. throw err;
  52. });
  53. }
  54. exports.openMxl = openMxl;