all files / diagram/load-utility/visio-import-export/ xml-write.js

96.9% Statements 250/258
86.45% Branches 134/155
100% Functions 43/43
96.9% Lines 250/258
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374   3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123× 3123×     3031×         2853× 2853× 2853× 536× 536×   2853×   50172× 50172× 8754×   50172× 50172× 49536× 1483×   49536× 49536×     636× 636×       50172× 48053×   50172×   50163× 39067× 39067×   11096× 2168×   50163× 50163× 50163× 50163× 50163×   1280× 1280× 1216×   1280×   123390× 123390× 123390×   1251×   4870×   6121× 6121× 2348×   6121× 6121× 4870×     1251×       123390× 716× 716× 716×     123390× 123390×   123390× 120077×         120077× 120077×     123390× 121148× 1071×   121148× 120077×     123390×   123390× 120793× 716× 716× 716×   120077×         2597× 2278× 1404× 1404× 1404×   874× 662× 662× 662×       531×       120608× 531×   120608×   123390× 123390×   50172× 50172× 636× 636×   50172× 50172× 50172× 50172× 50172× 50172× 119481× 119481×   50172×   50163× 11096× 11096× 636× 636×   11096× 11096×     39067× 39067×     123390× 123390× 2597× 2597×   123390× 123390× 123390×   767× 767× 767×   767× 767×         125408× 24×   125408× 125408× 125408× 125408× 124157×   125408× 125408× 1251×     50169× 50169× 51998× 767×     50169× 50169×   200942×   52001× 52001× 52001×   1483× 4449×       1483×   49124× 53233× 49124×         52823× 61114× 51419×     1404×   50703× 50703× 50703× 50597×       50597× 50597×         106×   50703×   2120× 2120× 822× 822× 822×     1298× 1298×   123390× 123390× 123390× 123390×     123390× 123390×   173562×       61370× 61370× 61370×       53295× 53295× 53295× 53295×       123390× 123390× 123390×   119481× 119481× 119481×        
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var XmlWriter = (function () {
        function XmlWriter() {
            this.contentPos = 0;
            this.bufferText = '';
            this.bufferBlob = new Blob([''], { type: 'text/plain' });
            this.currentState = 'Initial';
            this.namespaceStack = [];
            this.namespaceStack.push(new Namespace());
            this.namespaceStack[0].set('xmlns', 'http://www.w3.org/2000/xmlns/', 'Special');
            this.namespaceStack.push(new Namespace());
            this.namespaceStack[1].set('xml', 'http://www.w3.org/XML/1998/namespace', 'Special');
            this.namespaceStack.push(new Namespace());
            this.namespaceStack[2].set('', '', 'Implied');
            this.elementStack = [];
            this.elementStack.push(new XmlElement());
            this.elementStack[0].set('', '', '', this.namespaceStack.length - 1);
            this.attributeStack = [];
        }
        Object.defineProperty(XmlWriter.prototype, "text", {
            get: function () {
                return this.bufferText;
            },
            enumerable: true,
            configurable: true
        });
        XmlWriter.prototype.writeStartDocument = function (standalone) {
            this.currentState = 'StartDocument';
            this.rawText('<?xml version="1.0" encoding="utf-8');
            if (standalone !== null && standalone !== undefined) {
                this.rawText('" standalone="');
                this.rawText(standalone ? 'yes' : 'no');
            }
            this.rawText('"?>');
        };
        XmlWriter.prototype.writeStartElement = function (prefix, localName, namespace) {
            this.checkName(localName);
            if (this.currentState === 'StartElement') {
                this.startElementContent();
            }
            this.currentState = 'StartElement';
            if (prefix === undefined || prefix === null) {
                if (namespace !== undefined && namespace !== null) {
                    prefix = this.lookupPrefix(namespace);
                }
                Eif (prefix === undefined || prefix === null) {
                    prefix = '';
                }
            }
            else Eif (prefix.length > 0) {
                Iif (namespace === undefined || namespace === null) {
                    namespace = this.lookupNamespace(prefix);
                }
            }
            if (namespace === undefined || namespace === null) {
                namespace = this.lookupNamespace(prefix);
            }
            this.writeStartElementInternal(prefix, localName, namespace);
        };
        XmlWriter.prototype.writeEndElement = function () {
            if (this.currentState === 'StartElement') {
                this.startElementContent();
                this.currentState = 'ElementContent';
            }
            else if (this.currentState === 'ElementContent') {
                this.currentState = 'ElementContent';
            }
            this.currentState = 'EndElement';
            var top = this.elementStack.length - 1;
            this.writeEndElementInternal(this.elementStack[top].prefix, this.elementStack[top].localName);
            this.namespaceStack.splice(this.elementStack[top].previousTop + 1);
            this.elementStack.splice(top);
        };
        XmlWriter.prototype.writeElementString = function (prefix, localName, namespace, value) {
            this.writeStartElement(prefix, localName, namespace);
            if (value !== undefined && value !== null && value.length !== 0) {
                this.writeString(value);
            }
            this.writeEndElement();
        };
        XmlWriter.prototype.writeAttributeString = function (prefix, localName, namespace, value) {
            this.writeStartAttribute(prefix, localName, namespace, value);
            this.writeStringInternal(value, true);
            this.writeEndAttribute();
        };
        XmlWriter.prototype.writeString = function (text) {
            this.writeInternal(text, false);
        };
        XmlWriter.prototype.writeRaw = function (text) {
            this.writeInternal(text, true);
        };
        XmlWriter.prototype.writeInternal = function (text, isRawString) {
            Eif (text !== undefined && text !== null) {
                if (this.currentState === 'StartElement') {
                    this.startElementContent();
                }
                this.currentState = 'ElementContent';
                if (isRawString) {
                    this.rawText(text);
                }
                else {
                    this.writeStringInternal(text, false);
                }
            }
        };
        XmlWriter.prototype.writeStartAttribute = function (prefix, localName, namespace, value) {
            if (localName === undefined || localName === null || localName.length === 0) {
                Eif (prefix === 'xmlns') {
                    localName = 'xmlns';
                    prefix = '';
                }
            }
            this.checkName(localName);
            this.writeStartAttributePrefixAndNameSpace(prefix, localName, namespace, value);
        };
        XmlWriter.prototype.writeStartAttributePrefixAndNameSpace = function (prefix, localName, namespace, value) {
            if (prefix === undefined || prefix === null) {
                Iif (namespace !== undefined && namespace !== null) {
                    if (!(localName === 'xmlns' && namespace === 'http://www.w3.org/2000/xmlns/')) {
                        prefix = this.lookupPrefix(namespace);
                    }
                }
                Eif (prefix === undefined || prefix === null) {
                    prefix = '';
                }
            }
            if (namespace === undefined || namespace === null) {
                if (prefix !== undefined && prefix !== null && prefix.length > 0) {
                    namespace = this.lookupNamespace(prefix);
                }
                if (namespace === undefined || namespace === null) {
                    namespace = '';
                }
            }
            this.writeStartAttributeSpecialAttribute(prefix, localName, namespace, value);
        };
        XmlWriter.prototype.writeStartAttributeSpecialAttribute = function (prefix, localName, namespace, value) {
            if (prefix.length === 0) {
                if (localName[0] === 'x' && localName === 'xmlns') {
                    this.skipPushAndWrite(prefix, localName, namespace);
                    this.pushNamespaceExplicit('', value);
                    return;
                }
                else Iif (namespace.length > 0) {
                    prefix = this.lookupPrefix(namespace);
                }
            }
            else {
                if (prefix[0] === 'x') {
                    if (prefix === 'xmlns') {
                        this.skipPushAndWrite(prefix, localName, namespace);
                        this.pushNamespaceExplicit(localName, value);
                        return;
                    }
                    else if (prefix === 'xml') {
                        Eif (localName === 'space' || localName === 'lang') {
                            this.skipPushAndWrite(prefix, localName, namespace);
                            return;
                        }
                    }
                }
                Iif (namespace.length === 0) {
                    prefix = '';
                }
            }
            if (prefix !== undefined && prefix !== null && prefix.length !== 0) {
                this.pushNamespaceImplicit(prefix, namespace);
            }
            this.skipPushAndWrite(prefix, localName, namespace);
        };
        XmlWriter.prototype.writeEndAttribute = function () {
            this.currentState = 'StartElement';
            this.bufferText += '"';
        };
        XmlWriter.prototype.writeStartElementInternal = function (prefix, localName, namespace) {
            this.bufferText += '<';
            if (prefix.length > 0) {
                this.rawText(prefix);
                this.bufferText += ':';
            }
            this.rawText(localName);
            var top = this.elementStack.length;
            this.elementStack.push(new XmlElement());
            this.elementStack[top].set(prefix, localName, namespace, this.namespaceStack.length - 1);
            this.pushNamespaceImplicit(prefix, namespace);
            for (var _i = 0, _a = this.attributeStack; _i < _a.length; _i++) {
                var attr = _a[_i];
                attr.destroy();
            }
            this.attributeStack = [];
        };
        XmlWriter.prototype.writeEndElementInternal = function (prefix, localName) {
            if (this.contentPos !== this.bufferText.length + 1) {
                this.bufferText += '</';
                if (prefix !== undefined && prefix !== null && prefix.length !== 0) {
                    this.rawText(prefix);
                    this.bufferText += ':';
                }
                this.rawText(localName);
                this.bufferText += '>';
            }
            else {
                this.bufferText = this.bufferText.substring(0, this.bufferText.length - 1);
                this.bufferText += ' />';
            }
        };
        XmlWriter.prototype.writeStartAttributeInternal = function (prefix, localName, _namespaceName) {
            this.bufferText += ' ';
            if (prefix !== undefined && prefix !== null && prefix.length > 0) {
                this.rawText(prefix);
                this.bufferText += ':';
            }
            this.rawText(localName);
            this.bufferText += '=';
            this.bufferText += '"';
        };
        XmlWriter.prototype.writeNamespaceDeclaration = function (prefix, namespaceUri) {
            this.writeStartNamespaceDeclaration(prefix);
            this.writeStringInternal(namespaceUri, true);
            this.bufferText += '"';
        };
        XmlWriter.prototype.writeStartNamespaceDeclaration = function (prefix) {
            Eif (prefix === undefined || prefix === null || prefix.length === 0) {
                this.rawText(' xmlns=\"');
            }
            else {
            }
        };
        XmlWriter.prototype.writeStringInternal = function (text, inAttributeValue) {
            if (text === null || text === undefined) {
                text = '';
            }
            text = text.replace(/\&/g, '&amp;');
            text = text.replace(/\</g, '&lt;');
            text = text.replace(/\>/g, '&gt;');
            if (inAttributeValue) {
                text = text.replace(/\"/g, '&quot;');
            }
            this.bufferText += text;
            if (!inAttributeValue) {
                this.contentPos = 0;
            }
        };
        XmlWriter.prototype.startElementContent = function () {
            var start = this.elementStack[this.elementStack.length - 1].previousTop;
            for (var i = this.namespaceStack.length - 1; i > start; i--) {
                if (this.namespaceStack[i].kind === 'NeedToWrite') {
                    this.writeNamespaceDeclaration(this.namespaceStack[i].prefix, this.namespaceStack[i].namespaceUri);
                }
            }
            this.bufferText += '>';
            this.contentPos = this.bufferText.length + 1;
        };
        XmlWriter.prototype.rawText = function (text) {
            this.bufferText += text;
        };
        XmlWriter.prototype.addNamespace = function (prefix, ns, kind) {
            var top = this.namespaceStack.length;
            this.namespaceStack.push(new Namespace());
            this.namespaceStack[top].set(prefix, ns, kind);
        };
        XmlWriter.prototype.lookupPrefix = function (namespace) {
            for (var i = this.namespaceStack.length - 1; i >= 0; i--) {
                Iif (this.namespaceStack[i].namespaceUri === namespace) {
                    return this.namespaceStack[i].prefix;
                }
            }
            return undefined;
        };
        XmlWriter.prototype.lookupNamespace = function (prefix) {
            for (var i = this.namespaceStack.length - 1; i >= 0; i--) {
                if (this.namespaceStack[i].prefix === prefix) {
                    return this.namespaceStack[i].namespaceUri;
                }
            }
            return undefined;
        };
        XmlWriter.prototype.lookupNamespaceIndex = function (prefix) {
            for (var i = this.namespaceStack.length - 1; i >= 0; i--) {
                if (this.namespaceStack[i].prefix === prefix) {
                    return i;
                }
            }
            return -1;
        };
        XmlWriter.prototype.pushNamespaceImplicit = function (prefix, ns) {
            var kind;
            var existingNsIndex = this.lookupNamespaceIndex(prefix);
            if (existingNsIndex !== -1) {
                Iif (existingNsIndex > this.elementStack[this.elementStack.length - 1].previousTop) {
                    return;
                }
                else {
                    Eif (this.namespaceStack[existingNsIndex].kind !== 'Special') {
                        kind = (this.namespaceStack[existingNsIndex].namespaceUri === ns) ? 'Implied' : 'NeedToWrite';
                    }
                }
            }
            else {
                kind = 'NeedToWrite';
            }
            this.addNamespace(prefix, ns, kind);
        };
        XmlWriter.prototype.pushNamespaceExplicit = function (prefix, ns) {
            var existingNsIndex = this.lookupNamespaceIndex(prefix);
            if (existingNsIndex !== -1) {
                Eif (existingNsIndex > this.elementStack[this.elementStack.length - 1].previousTop) {
                    this.namespaceStack[existingNsIndex].kind = 'Written';
                    return;
                }
            }
            this.addNamespace(prefix, ns, 'Written');
            return;
        };
        XmlWriter.prototype.addAttribute = function (prefix, localName, namespaceName) {
            var top = this.attributeStack.length;
            this.attributeStack.push(new XmlAttribute());
            this.attributeStack[top].set(prefix, localName, namespaceName);
            for (var i = 0; i < top; i++) {
            }
        };
        XmlWriter.prototype.skipPushAndWrite = function (prefix, localName, namespace) {
            this.addAttribute(prefix, localName, namespace);
            this.writeStartAttributeInternal(prefix, localName, namespace);
        };
        XmlWriter.prototype.checkName = function (text) {
            var format = /[ !@#$%^&*()+\=\[\]{};':"\\|,<>\/?]/;
        };
        return XmlWriter;
    }());
    exports.XmlWriter = XmlWriter;
    var Namespace = (function () {
        function Namespace() {
        }
        Namespace.prototype.set = function (prefix, namespaceUri, kind) {
            this.prefix = prefix;
            this.namespaceUri = namespaceUri;
            this.kind = kind;
        };
        return Namespace;
    }());
    exports.Namespace = Namespace;
    var XmlElement = (function () {
        function XmlElement() {
        }
        XmlElement.prototype.set = function (prefix, localName, namespaceUri, previousTop) {
            this.previousTop = previousTop;
            this.prefix = prefix;
            this.namespaceUri = namespaceUri;
            this.localName = localName;
        };
        return XmlElement;
    }());
    exports.XmlElement = XmlElement;
    var XmlAttribute = (function () {
        function XmlAttribute() {
        }
        XmlAttribute.prototype.set = function (prefix, localName, namespaceUri) {
            this.prefix = prefix;
            this.namespaceUri = namespaceUri;
            this.localName = localName;
        };
        XmlAttribute.prototype.destroy = function () {
            this.prefix = undefined;
            this.namespaceUri = undefined;
            this.localName = undefined;
        };
        return XmlAttribute;
    }());
    exports.XmlAttribute = XmlAttribute;
});