1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import { IXmlElement } from "./Xml";
- import { Promise } from "es6-promise";
- import JSZip = require("jszip");
- /**
- * Some helper methods to handle MXL files.
- */
- export class MXLHelper {
- /**
- *
- * @param data
- * @returns {Promise<IXmlElement>}
- * @constructor
- */
- public static MXLtoIXmlElement(data: string): Promise<IXmlElement> {
- const zip: JSZip.JSZip = new JSZip();
- // asynchronously load zip file and process it - with Promises
- return zip.loadAsync(data).then(
- (_: any) => {
- return zip.file("META-INF/container.xml").async("string");
- },
- (err: any) => {
- throw err;
- }
- ).then(
- (content: string) => {
- const parser: DOMParser = new DOMParser();
- const doc: Document = parser.parseFromString(content, "text/xml");
- const rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
- return zip.file(rootFile).async("string");
- },
- (err: any) => {
- throw err;
- }
- ).then(
- (content: string) => {
- const parser: DOMParser = new DOMParser();
- const xml: Document = parser.parseFromString(content, "text/xml");
- const doc: IXmlElement = new IXmlElement(xml.documentElement);
- return Promise.resolve(doc);
- },
- (err: any) => {
- throw err;
- }
- ).then(
- (content: IXmlElement) => {
- return content;
- },
- (err: any) => {
- throw new Error("extractSheetFromMxl: " + err.message);
- }
- );
- }
- public static MXLtoXMLstring(data: string): Promise<string> {
- const zip: JSZip.JSZip = new JSZip();
- // asynchronously load zip file and process it - with Promises
- return zip.loadAsync(data).then(
- (_: any) => {
- return zip.file("META-INF/container.xml").async("string");
- },
- (err: any) => {
- throw err;
- }
- ).then(
- (content: string) => {
- const parser: DOMParser = new DOMParser();
- const doc: Document = parser.parseFromString(content, "text/xml");
- const rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
- return zip.file(rootFile).async("string");
- },
- (err: any) => {
- throw err;
- }
- );
- }
- }
|