/*
*
* An XmlReader implementation for loading SGML (including HTML) converting it
* to well formed XML, by adding missing quotes, empty attribute values, ignoring
* duplicate attributes, case folding on tag names, adding missing closing tags
* based on SGML DTD information, and so on.
*
* Copyright (c) 2002 Microsoft Corporation. All rights reserved.
*
* Chris Lovett
*
*/
/* MD 2/27/13
 * This software is being re-used under the Microsoft Public License (MS-PL):
 * -----------------------------------------------------------------------------
 * This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the
 * license, do not use the software.
 *
 * 1. Definitions
 * The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
 * A "contribution" is the original software, or any additions or changes to the software. A "contributor" is any person that distributes
 * its contribution under this license. "Licensed patents" are a contributor's patent claims that read directly on its contribution.
 *
 * 2. Grant of Rights
 * (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each
 *     contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare
 *     derivative works of its contribution, and distribute its contribution or any derivative works that you create.
 * (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each
 *     contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use,
 *     sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution
 *     in the software.
 *
 * 3. Conditions and Limitations
 * (A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
 * (B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license
 *     from such contributor to the software ends automatically.
 * (C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are
 *     present in the software.
 * (D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete
 *     copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may
 *     only do so under a license that complies with this license.
 * (E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions.
 *     You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your
 *     local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
 * -----------------------------------------------------------------------------
 *
 * In addition, the public classes have been made internal and the namespace has been changed from Sgml to
 * Infragistics.Documents.Excel.Serialization.Excel2007.Sgml
 *
 * Also, I updated the code to work with Silverlight and WinRT.
 */
import { Base, BaseError, markType } from "igniteui-angular-core";
import { StringUtilitiesSgml } from "./StringUtilitiesSgml";
import { StringUtilities } from "./StringUtilities";
import { StringReader } from "igniteui-angular-core";
import { StringBuilder } from "igniteui-angular-core";
import { Convert } from "igniteui-angular-core";
import { stringCompare1, isLetter, stringFormat, isLetterOrDigit, stringFormat1 } from "igniteui-angular-core";
/**
 * @hidden
 */
export class Entity extends Base {
    constructor(a, ..._rest) {
        super();
        this.v = null;
        this.u = null;
        this.e = false;
        this.w = null;
        this.aa = null;
        this.t = null;
        this.c = 0;
        this.b = null;
        this.d = false;
        this.n = 0;
        this.j = '\0';
        this.g = false;
        this.ab = null;
        this.ad = null;
        this.q = null;
        this.h = false;
        this.p = 0;
        this.m = 0;
        a = (a == void 0) ? 0 : a;
        switch (a) {
            case 0:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    let e = _rest[2];
                    let f = _rest[3];
                    this.u = c;
                    this.w = d;
                    this.aa = e;
                    this.v = f;
                    this.d = (c != null && StringUtilitiesSgml.a(c, "html"));
                }
                break;
            case 1:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    this.u = c;
                    this.t = d;
                    this.e = true;
                }
                break;
            case 2:
                {
                    let c = _rest[0];
                    let d = _rest[1];
                    let e = _rest[2];
                    let f = _rest[3];
                    this.u = c;
                    this.e = true;
                    this.q = e;
                    this.ad = d;
                    this.v = f;
                    this.d = (stringCompare1(c, "html", StringUtilities.j) == 0);
                }
                break;
        }
    }
    get ae() {
        if (this.ad != null) {
            return this.ad;
        }
        else if (this.b != null) {
            return this.b.ae;
        }
        return null;
    }
    get o() {
        return this.m - this.p + 1;
    }
    k() {
        let a = String.fromCharCode(this.q.b());
        if (a.charCodeAt(0) == 0) {
            a = ' ';
        }
        this.m++;
        if (a.charCodeAt(0) == 10) {
            this.g = true;
            this.p = this.m + 1;
            this.n++;
        }
        else if (a == ' ' || a == '\t') {
            this.g = true;
            if (this.j.charCodeAt(0) == 13) {
                this.p = this.m;
                this.n++;
            }
        }
        else if (a.charCodeAt(0) == 13) {
            this.g = true;
        }
        else {
            this.g = false;
            if (this.j.charCodeAt(0) == 13) {
                this.n++;
                this.p = this.m;
            }
        }
        this.j = a;
        return a;
    }
    ak(a, b) {
        this.b = a;
        if (a != null) {
            this.d = a.d;
        }
        this.n = 1;
        if (this.e) {
            if (this.t != null) {
                this.q = new StringReader(this.t);
            }
        }
        else if (this.aa == null) {
            this.aj("Unresolvable entity '{0}'", this.u);
        }
        else {
            this.aj("Unresolvable entity '{0}'", this.u);
        }
    }
    ac() {
        return this.ab;
    }
    af() {
        if (this.h) {
            this.q.i();
        }
    }
    l() {
        let a = this.j;
        while (a != "\uffff" && (a == ' ' || a == '\r' || a == '\n' || a == '\t')) {
            a = this.k();
        }
        return a;
    }
    z(a, b, c) {
        a.c = 0;
        let d = this.j;
        if (c && d != '_' && !isLetter(d)) {
            throw new BaseError(1, stringFormat("Invalid name start character '{0}'", d));
        }
        while (d != "\uffff" && b.indexOf(d) < 0) {
            if (!c || d == '_' || d == '.' || d == '-' || d == ':' || isLetterOrDigit(d)) {
                a.h(d);
            }
            else {
                throw new BaseError(1, stringFormat("Invalid name character '{0}'", d));
            }
            d = this.k();
        }
        return a.toString();
    }
    x(a, b) {
        a.c = 0;
        let c = this.k();
        while (c != "\uffff" && c != b) {
            if (c == '&') {
                c = this.k();
                if (c == '#') {
                    let d = this.s();
                    a.l(d);
                    c = this.j;
                }
                else {
                    a.h('&');
                    a.h(c);
                    c = this.k();
                }
            }
            else {
                a.h(c);
                c = this.k();
            }
        }
        this.k();
        return a.toString();
    }
    y(a, b, c) {
        if (a != null) {
            a.c = 0;
        }
        let d = this.n;
        let e = this.k();
        let f = 0;
        let g = c.charAt(f);
        while (e != "\uffff") {
            if (e == g) {
                f++;
                if (f >= c.length) {
                    break;
                }
                g = c.charAt(f);
            }
            else if (f > 0) {
                let h = f - 1;
                let i = 0;
                while (h >= 0 && i == 0) {
                    if (c.charAt(h) == e) {
                        let j = 1;
                        while (h - j >= 0) {
                            if (c.charAt(h - j) != c.charAt(f - j)) {
                                break;
                            }
                            j++;
                        }
                        if (j > h) {
                            i = h + 1;
                        }
                    }
                    else {
                        h--;
                    }
                }
                if (a != null) {
                    h = (h < 0) ? 1 : 0;
                    for (let k = 0; k <= f - i - h; k++) {
                        a.h(c.charAt(k));
                    }
                    if (h > 0) {
                        a.h(e);
                    }
                }
                f = i;
                g = c.charAt(i);
            }
            else {
                if (a != null) {
                    a.h(e);
                }
            }
            e = this.k();
        }
        if (e.charCodeAt(0) == 0) {
            this.ai(b + " starting on line {0} was never closed", d);
        }
        this.k();
        if (a != null) {
            return a.toString();
        }
        return "";
    }
    s() {
        let a = this.k();
        let b = 0;
        if (a == 'x') {
            a = this.k();
            for (; a != "\uffff" && a != ';'; a = this.k()) {
                let c = 0;
                if (a.charCodeAt(0) >= '0'.charCodeAt(0) && a.charCodeAt(0) <= '9'.charCodeAt(0)) {
                    c = (a.charCodeAt(0) - '0'.charCodeAt(0));
                }
                else if (a.charCodeAt(0) >= 'a'.charCodeAt(0) && a.charCodeAt(0) <= 'f'.charCodeAt(0)) {
                    c = (a.charCodeAt(0) - 'a'.charCodeAt(0)) + 10;
                }
                else if (a.charCodeAt(0) >= 'A'.charCodeAt(0) && a.charCodeAt(0) <= 'F'.charCodeAt(0)) {
                    c = (a.charCodeAt(0) - 'A'.charCodeAt(0)) + 10;
                }
                else {
                    break;
                }
                b = (b * 16) + c;
            }
        }
        else {
            for (; a != "\uffff" && a != ';'; a = this.k()) {
                if (a.charCodeAt(0) >= '0'.charCodeAt(0) && a.charCodeAt(0) <= '9'.charCodeAt(0)) {
                    b = (b * 10) + (a.charCodeAt(0) - '0'.charCodeAt(0));
                }
                else {
                    break;
                }
            }
        }
        if (a.charCodeAt(0) == 0) {
            this.ah("Premature {0} parsing entity reference", a);
        }
        else if (a == ';') {
            this.k();
        }
        if (this.d && b >= 128 && b <= 159) {
            let d = Entity.a.length;
            let e = b - 128;
            let f = Entity.a[e];
            return Convert.toChar1(f).toString();
        }
        return Convert.toChar1(b).toString();
    }
    ag(a) {
        throw new BaseError(1, a);
    }
    ah(a, b) {
        let c = (b == "\uffff") ? "EOF" : b.toString();
        throw new BaseError(1, stringFormat(a, c));
    }
    ai(a, b) {
        throw new BaseError(1, stringFormat(a, b));
    }
    aj(a, b) {
        throw new BaseError(1, stringFormat(a, b));
    }
    r() {
        let a = this;
        let b = new StringBuilder(0);
        while (a != null) {
            let c;
            if (a.e) {
                c = stringFormat("\nReferenced on line {0}, position {1} of internal entity '{2}'", a.n, a.o, a.u);
            }
            else {
                c = stringFormat1("\nReferenced on line {0}, position {1} of '{2}' entity at [{3}]", a.n, a.o, a.u, a.ae.absolutePath);
            }
            b.l(c);
            a = a.b;
        }
        return b.toString();
    }
    static f(a) {
        return (a == "CDATA" || a == "SDATA" || a == "PI");
    }
    al(a) {
        switch (a) {
            case "CDATA":
                this.c = 0;
                break;
            case "SDATA":
                this.c = 1;
                break;
            case "PI":
                this.c = 2;
                break;
        }
    }
}
Entity.$t = markType(Entity, 'Entity');
Entity.a = [8364, 129, 8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 352, 8249, 338, 141, 381, 143, 144, 8216, 8217, 8220, 8221, 8226, 8211, 8212, 732, 8482, 353, 8250, 339, 157, 382, 376];
//# sourceMappingURL=Entity.js.map