import { IXmlElement } from "./Xml" ; import JSZip from "jszip" ; import log from "loglevel" ; /**
 * Some helper methods to handle MXL files.
 */ export class MXLHelper { param data returns { Promise < IXmlElement > } constructor public static MXLtoIXmlElement ( data ) { const zip = new JSZip ( ) ; const zipLoadedAsync = zip . loadAsync ( data ) ; const text = zipLoadedAsync . then ( ( _ ) => { return zip . file ( "META-INF/container.xml" ) . async ( "text" ) ; } , ( err ) => { throw err ; } ) ; return text . then ( ( content ) => { const parser = new DOMParser ( ) ; const doc = parser . parseFromString ( content , "text/xml" ) ; const rootFile = doc . getElementsByTagName ( "rootfile" ) [ 0 ] . getAttribute ( "full-path" ) ; return zip . file ( rootFile ) . async ( "text" ) ; } , ( err ) => { throw err ; } ) . then ( ( content ) => { const parser = new DOMParser ( ) ; const xml = parser . parseFromString ( content , "text/xml" ) ; const doc = new IXmlElement ( xml . documentElement ) ; return Promise . resolve ( doc ) ; } , ( err ) => { throw err ; } ) . then ( ( content ) => { return content ; } , ( err ) => { throw new Error ( "extractSheetFromMxl: " + err . message ) ; } ) ; } public static MXLtoXMLstring ( data ) { const zip = new JSZip ( ) ; return zip . loadAsync ( data ) . then ( ( _ ) => { return zip . file ( "META-INF/container.xml" ) . async ( "text" ) ; } , ( err ) => { log . error ( err ) ; throw err ; } ) . then ( ( content ) => { const parser = new DOMParser ( ) ; const doc = parser . parseFromString ( content , "text/xml" ) ; const rootFile = doc . getElementsByTagName ( "rootfile" ) [ 0 ] . getAttribute ( "full-path" ) ; return zip . file ( rootFile ) . async ( "text" ) ; } , ( err ) => { throw err ; } ) ; } }