/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext;

/**
 * Контейнер других элементов
 * 
 */
public class UnitextContainer extends UnitextItem {

    private static int _getBoxChars(UnitextItem it) {
        UnitextPlaintext pl = (UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it, UnitextPlaintext.class);
        if (pl == null) 
            return 0;
        int co = 0;
        for (char ch : pl.text.toCharArray()) {
            if (((int)ch) >= 0x2500 && ((int)ch) <= 0x257F) 
                co++;
        }
        return co;
    }

    private static int _getUnderlineChars(UnitextItem it) {
        UnitextPlaintext pl = (UnitextPlaintext)com.pullenti.unisharp.Utils.cast(it, UnitextPlaintext.class);
        if (pl == null) 
            return 0;
        int co = 0;
        for (char ch : pl.text.toCharArray()) {
            if (ch == '_' || ch == '_') 
                co++;
        }
        return co;
    }

    @Override
    public UnitextItem optimize(boolean isContainer, CreateDocumentParam pars) {
        if (children == null || children.size() == 0) {
            if (typ == UnitextContainerType.SHAPE) 
                return this;
            return null;
        }
        for (int i = 0; i < children.size(); i++) {
            if (i == 17) {
            }
            UnitextItem ch0 = children.get(i);
            ch0.parent = this;
            if (ch0 instanceof UnitextTable) {
            }
            UnitextItem ch = ch0.optimize(false, pars);
            if (ch == null) {
                children.remove(i);
                i--;
            }
            else 
                com.pullenti.unisharp.Utils.putArrayValue(children, i, ch);
        }
        if (children.size() >= 30) {
        }
        for (int i = children.size() - 1; i >= 0; i--) {
            UnitextContainer cnt = (UnitextContainer)com.pullenti.unisharp.Utils.cast(children.get(i), UnitextContainer.class);
            if (cnt == null) 
                continue;
            if (i == 0) {
            }
            if (cnt.pageSectionId != null && pageSectionId != null && com.pullenti.unisharp.Utils.stringsNe(cnt.pageSectionId, pageSectionId)) 
                continue;
            if (cnt.typ == UnitextContainerType.SHAPE && cnt.children.size() > 0 && (cnt.children.get(cnt.children.size() - 1) instanceof UnitextNewline)) {
                UnitextItem nl = cnt.children.get(cnt.children.size() - 1);
                cnt.children.remove(cnt.children.size() - 1);
                children.add(i + 1, nl);
                nl.parent = this;
                continue;
            }
            if (cnt.typ != UnitextContainerType.UNDEFINED) 
                continue;
            children.remove(i);
            for (UnitextItem chh : cnt.children) {
                if (chh.pageSectionId == null) 
                    chh.pageSectionId = cnt.pageSectionId;
            }
            com.pullenti.unisharp.Utils.insertToArrayList(children, cnt.children, i);
        }
        if (children.size() == 7) {
        }
        for (int i = 0; i < (children.size() - 1); i++) {
            UnitextItem ch = children.get(i);
            UnitextItem ch1 = children.get(i + 1);
            if ((ch instanceof UnitextPagebreak) && (ch1 instanceof UnitextPagebreak)) {
                children.remove(i + 1);
                i--;
                continue;
            }
            if (ch instanceof UnitextNewline) {
                if (ch1 instanceof UnitextNewline) {
                    int ii;
                    for (ii = i + 1; ii < children.size(); ii++) {
                        if (!(children.get(ii) instanceof UnitextNewline)) 
                            break;
                        else 
                            ((UnitextNewline)com.pullenti.unisharp.Utils.cast(ch, UnitextNewline.class)).count += ((UnitextNewline)com.pullenti.unisharp.Utils.cast(children.get(ii), UnitextNewline.class)).count;
                    }
                    for(int jjj = i + 1 + ii - i - 1 - 1, mmm = i + 1; jjj >= mmm; jjj--) children.remove(jjj);
                    i--;
                }
                else if (ch1 instanceof UnitextPagebreak) {
                    children.remove(i);
                    i--;
                }
                continue;
            }
            if ((ch instanceof UnitextTable) && !(ch1 instanceof UnitextNewline) && !(ch1 instanceof UnitextPagebreak)) {
                children.add(i + 1, new UnitextNewline());
                continue;
            }
            if ((ch instanceof UnitextPlaintext) && ch.isWhitespaces()) {
                if ((ch1 instanceof UnitextNewline) || (ch1 instanceof UnitextPagebreak)) {
                    children.remove(i);
                    i--;
                    if (i >= 0) 
                        i--;
                    continue;
                }
            }
            if ((ch instanceof UnitextContainer) && !ch.isInline() && (ch1 instanceof UnitextNewline)) {
                ((UnitextContainer)com.pullenti.unisharp.Utils.cast(ch, UnitextContainer.class)).children.add(ch1);
                children.remove(i + 1);
                i--;
                continue;
            }
            if ((ch instanceof UnitextContainer) && (ch1 instanceof UnitextContainer)) {
                UnitextContainer cnt = (UnitextContainer)com.pullenti.unisharp.Utils.cast(ch, UnitextContainer.class);
                UnitextContainer cnt1 = (UnitextContainer)com.pullenti.unisharp.Utils.cast(ch1, UnitextContainer.class);
                if (cnt.typ == cnt1.typ && com.pullenti.unisharp.Utils.stringsEq(cnt.pageSectionId, cnt1.pageSectionId)) {
                    for (UnitextItem chh : cnt1.children) {
                        if (chh.pageSectionId == null) 
                            chh.pageSectionId = cnt1.pageSectionId;
                    }
                    com.pullenti.unisharp.Utils.addToArrayList(cnt.children, cnt1.children);
                    children.remove(i + 1);
                    i--;
                    continue;
                }
            }
            if ((ch instanceof UnitextPlaintext) && (ch1 instanceof UnitextPlaintext)) {
                if (((UnitextPlaintext)com.pullenti.unisharp.Utils.cast(ch, UnitextPlaintext.class)).typ == ((UnitextPlaintext)com.pullenti.unisharp.Utils.cast(ch1, UnitextPlaintext.class)).typ) {
                    if (ch.m_StyledFrag != null && ch1.m_StyledFrag != null && ch.m_StyledFrag.styleId != ch1.m_StyledFrag.styleId) 
                        continue;
                    if (ch.getPageSection() != ch1.getPageSection()) 
                        continue;
                    ((UnitextPlaintext)com.pullenti.unisharp.Utils.cast(ch, UnitextPlaintext.class)).mergeWith((UnitextPlaintext)com.pullenti.unisharp.Utils.cast(ch1, UnitextPlaintext.class));
                    children.remove(i + 1);
                    i--;
                    continue;
                }
            }
            if (pars != null && !pars.correctParams.concatTables) {
            }
            else {
                if ((ch instanceof UnitextTable) && (ch1 instanceof UnitextTable) && com.pullenti.unisharp.Utils.stringsEq(ch.pageSectionId, ch1.pageSectionId)) {
                    if (((UnitextTable)com.pullenti.unisharp.Utils.cast(ch, UnitextTable.class)).tryAppend((UnitextTable)com.pullenti.unisharp.Utils.cast(ch1, UnitextTable.class))) {
                        children.remove(i + 1);
                        i--;
                        continue;
                    }
                }
                if (((((i + 2) < children.size()) && (ch instanceof UnitextTable) && (children.get(i + 2) instanceof UnitextTable)) && (ch1 instanceof UnitextNewline) && (((UnitextNewline)com.pullenti.unisharp.Utils.cast(ch1, UnitextNewline.class)).count < 3)) && ((UnitextTable)com.pullenti.unisharp.Utils.cast(ch, UnitextTable.class)).tryAppend((UnitextTable)com.pullenti.unisharp.Utils.cast(children.get(i + 2), UnitextTable.class))) {
                    children.remove(i + 1);
                    children.remove(i + 1);
                    i--;
                    continue;
                }
            }
            if ((ch instanceof UnitextPlaintext) && (((ch1 instanceof UnitextNewline) || (ch1 instanceof UnitextPagebreak) || (ch1 instanceof UnitextFootnote)))) 
                ch.correct(com.pullenti.unitext.internal.uni.LocCorrTyp.TRIMEND, null);
        }
        for (int i = 0; i < (children.size() - 1); i++) {
            if ((children.get(i) instanceof UnitextDocblock) && (((children.get(i + 1) instanceof UnitextPagebreak) || (children.get(i + 1) instanceof UnitextNewline)))) {
                if (((UnitextDocblock)com.pullenti.unisharp.Utils.cast(children.get(i), UnitextDocblock.class))._appendChild(children.get(i + 1))) {
                    children.remove(i + 1);
                    children.get(i).optimize(false, pars);
                    i--;
                }
            }
        }
        if (isContainer) {
            for (int i = children.size() - 1; i >= 0; i--) {
                if (children.get(i).isWhitespaces()) 
                    children.remove(i);
                else 
                    break;
            }
            for (int i = 0; i < children.size(); i++) {
                if (children.get(i).isWhitespaces()) {
                    children.remove(i);
                    i--;
                }
                else 
                    break;
            }
        }
        for (int i = 0; i < children.size(); i++) {
            int co = _getBoxChars(children.get(i));
            if (co == 0) 
                continue;
            int i1 = i;
            int no = 1;
            int cou = 1;
            for (int j = i + 1; j < children.size(); j++) {
                if (children.get(j).isWhitespaces()) 
                    continue;
                co = _getBoxChars(children.get(j));
                if (co > 0) {
                    no = 1;
                    i1 = j;
                    cou++;
                    if (cou > 3) 
                        no++;
                    if (cou > 10) 
                        no++;
                }
                else {
                    co = _getUnderlineChars(children.get(j));
                    if (co > 10) {
                        no = 1;
                        i1 = j;
                        cou++;
                        if (cou > 3) 
                            no++;
                        if (cou > 10) 
                            no++;
                    }
                    if (co > 3) 
                        cou++;
                    else if ((--no) == 0) 
                        break;
                }
            }
            for (; (i1 + 1) < children.size(); i1++) {
                if (!children.get(i1 + 1).isWhitespaces() && (_getUnderlineChars(children.get(i1 + 1)) < 3)) 
                    break;
            }
            for (int j = i - 1; j >= 0; j--) {
                if (children.get(j).isWhitespaces()) 
                    continue;
                if (_getUnderlineChars(children.get(j)) < 3) 
                    break;
                i = j;
            }
            if (i == 0 && i1 == (children.size() - 1) && typ == UnitextContainerType.UNDEFINED) {
                typ = UnitextContainerType.MONOSPACE;
                break;
            }
            UnitextContainer cnt = _new588(UnitextContainerType.MONOSPACE, this);
            for (int j = i; j <= i1; j++) {
                cnt.children.add(children.get(j));
                children.get(j).parent = cnt;
            }
            for(int jjj = i + (i1 - i) + 1 - 1, mmm = i; jjj >= mmm; jjj--) children.remove(jjj);
            children.add(i, cnt);
        }
        if (children.size() == 0) 
            return null;
        if (typ != UnitextContainerType.UNDEFINED) {
            if (children.get(0) instanceof UnitextPlaintext) {
                if (com.pullenti.unisharp.Utils.startsWithString(((UnitextPlaintext)com.pullenti.unisharp.Utils.cast(children.get(0), UnitextPlaintext.class)).text.trim(), "КОНСУЛЬТАНТПЛЮС", true)) 
                    children.get(0).ignorable = true;
            }
        }
        if (children.size() == 1) {
            if (typ == UnitextContainerType.SHAPE && (children.get(0) instanceof UnitextImage)) 
                return children.get(0);
            if (!isInline() && children.get(0).isInline()) 
                return this;
            if (children.get(0) instanceof UnitextContainer) {
                UnitextContainer ch = (UnitextContainer)com.pullenti.unisharp.Utils.cast(children.get(0), UnitextContainer.class);
                if (ch.typ == typ) {
                    if (children.get(0).pageSectionId == null) 
                        children.get(0).pageSectionId = pageSectionId;
                    if (id != null && children.get(0).id == null) 
                        children.get(0).id = id;
                    return children.get(0);
                }
                if (typ == UnitextContainerType.HEAD || typ == UnitextContainerType.TAIL) {
                    if (ch.typ == UnitextContainerType.UNDEFINED) {
                        if (pageSectionId == null) 
                            pageSectionId = children.get(0).pageSectionId;
                        children.clear();
                        com.pullenti.unisharp.Utils.addToArrayList(children, ch.children);
                    }
                    return this;
                }
            }
            if (typ == UnitextContainerType.UNDEFINED) {
                if (children.get(0).pageSectionId == null) 
                    children.get(0).pageSectionId = pageSectionId;
                if (id != null && children.get(0).id == null) 
                    children.get(0).id = id;
                return children.get(0);
            }
        }
        for (int i = 0; i < (children.size() - 1); i++) {
            UnitextTable tab0 = (UnitextTable)com.pullenti.unisharp.Utils.cast(children.get(i), UnitextTable.class);
            if (tab0 == null || (tab0.getColsCount() < 2)) 
                continue;
            int j;
            boolean hasSpec = false;
            int txtLen = 0;
            UnitextTable tab1 = null;
            for (j = i + 1; j < children.size(); j++) {
                UnitextItem ch = children.get(j);
                tab1 = (UnitextTable)com.pullenti.unisharp.Utils.cast(ch, UnitextTable.class);
                if (tab1 != null) 
                    break;
                if ((ch instanceof UnitextNewline) || (ch instanceof UnitextPagebreak)) 
                    continue;
                UnitextPlaintext pl = (UnitextPlaintext)com.pullenti.unisharp.Utils.cast(ch, UnitextPlaintext.class);
                if (pl == null) 
                    break;
                if (com.pullenti.unisharp.Utils.startsWithString(pl.text, "ВХ", true) || com.pullenti.unisharp.Utils.startsWithString(pl.text, "ИСХ", true)) 
                    hasSpec = true;
                txtLen += pl.text.length();
                if (txtLen > 200) 
                    break;
            }
            if (tab1 == null || !hasSpec) 
                continue;
            if (tab1.getColsCount() != tab0.getColsCount()) 
                continue;
            if (!tab0.appendTable(tab1, true, false)) 
                continue;
            for(int jjj = i + 1 + j - i - 1, mmm = i + 1; jjj >= mmm; jjj--) children.remove(jjj);
            i--;
        }
        return this;
    }

    /**
     * Внутренние элементы
     * 
     */
    public java.util.ArrayList<UnitextItem> children = new java.util.ArrayList<UnitextItem>();

    /**
     * Специфический тип контейнера
     */
    public UnitextContainerType typ = UnitextContainerType.UNDEFINED;

    /**
     * Используйте произвольным образом, сериализуется
     */
    public String data;

    /**
     * Используйте произвольным образом, несериализуется
     */
    public Object userData;

    /**
     * При генерации HTML если не null, то явлется дополнительным стилем вывода
     */
    public String htmlStyle;

    /**
     * Это определяет класс при генерации Html
     */
    public String htmlClass;

    @Override
    public String getInnerTag() {
        return "cnt";
    }


    @Override
    public UnitextItem clone() {
        UnitextContainer res = new UnitextContainer();
        res._cloneFrom2(this);
        return res;
    }

    private void _cloneFrom2(UnitextContainer src) {
        this._cloneFrom(src);
        typ = src.typ;
        htmlStyle = src.htmlStyle;
        htmlClass = src.htmlClass;
        data = src.data;
        userData = src.userData;
        m_IsInline = src.m_IsInline;
        for (UnitextItem ch : src.children) {
            children.add(ch.clone());
        }
    }

    @Override
    public boolean getStartsOnNewline() {
        if (children.size() == 0) 
            return false;
        return children.get(0).getStartsOnNewline();
    }


    @Override
    public UnitextItem findById(String _id) {
        if (com.pullenti.unisharp.Utils.stringsEq(id, _id)) 
            return this;
        for (UnitextItem ch : children) {
            UnitextItem res = ch.findById(_id);
            if (res != null) 
                return res;
        }
        return null;
    }

    /**
     * Получить индекс дочернего элемента из списка Children (работает быстро)
     * @param it исколмый элемент
     * @return индекс или -1
     */
    public int getChildIndexOf(UnitextItem it) {
        if ((it.beginChar > 0 && children.size() > 20 && beginChar <= it.beginChar) && it.endChar <= endChar) {
            int i = children.size() / 2;
            int d = children.size() / 4;
            if (d == 0) 
                d = 1;
            int k = d + 2;
            while (k > 0) {
                if (i >= children.size() || (i < 0)) 
                    break;
                if (children.get(i) == it) 
                    return i;
                if (children.get(i).beginChar < it.beginChar) 
                    i += d;
                else if (children.get(i).endChar > it.beginChar) 
                    i -= d;
                else 
                    i += d;
                d /= 2;
                if (d == 0) {
                    d = 1;
                    k--;
                }
            }
        }
        return children.indexOf(it);
    }

    @Override
    public String toString() {
        String res = ((typ == UnitextContainerType.UNDEFINED ? "Container" : typ.toString()) + " " + (children == null ? 0 : children.size()) + " items");
        if (htmlTitle != null) 
            res = (res + " " + htmlTitle);
        return res;
    }

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (ignorable && pars != null) 
            pars.ignoreLevel++;
        if (children == null || children.size() == 0) {
        }
        else if (typ == UnitextContainerType.SHAPE && pars != null && pars.ignoreShapes) {
        }
        else {
            if (typ == UnitextContainerType.SHAPE && pars != null && res.length() > 0) {
                char ch = res.charAt(res.length() - 1);
                if (Character.isLetterOrDigit(ch)) 
                    res.append(pars.newLine);
            }
            for (UnitextItem ch : children) {
                ch.getPlaintext(res, (pars != null ? pars : UnitextItem.m_DefParams));
                if (pars != null && pars.maxTextLength > 0 && res.length() > pars.maxTextLength) 
                    break;
            }
            if (typ == UnitextContainerType.SHAPE && pars != null) 
                res.append(pars.newLine);
        }
        if (pars != null && pars.setPositions) 
            endChar = res.length() - 1;
        if (ignorable && pars != null) 
            pars.ignoreLevel--;
    }

    @Override
    public void getHtml(StringBuilder res, GetHtmlParam par) {
        if (par.hideEditionsAndComments) {
            if (typ == UnitextContainerType.EDITION || typ == UnitextContainerType.COMMENT) 
                return;
        }
        if (!par.callBefore(this, res)) 
            return;
        String _id = "";
        if (id != null) 
            _id = (" id=\"" + id + "\"");
        if (par != null && par.outBeginEndChars && beginChar <= endChar) 
            _id = (_id + " bc=\"" + beginChar + "\" ec=\"" + endChar + "\"");
        if (((children == null || children.size() == 0)) && typ != UnitextContainerType.SHAPE && typ != UnitextContainerType.FORMULA) {
            par.callAfter(this, res);
            return;
        }
        boolean isDiv = false;
        boolean isSpan = false;
        boolean isPre = false;
        String tit = (typ == UnitextContainerType.UNDEFINED ? "" : typ.toString().toLowerCase());
        if (htmlTitle != null) {
            StringBuilder tmp = new StringBuilder();
            com.pullenti.util.MiscHelper.correctHtmlValue(tmp, htmlTitle, true, false, false);
            tit = tmp.toString();
        }
        else 
            for (UnitextItem p = parent; p != null; p = p.parent) {
                if (p.htmlTitle != null) {
                    tit = "";
                    break;
                }
            }
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(tit)) 
            tit = (" title=\"" + tit + "\"");
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(htmlClass)) 
            tit = (tit + " class=\"" + htmlClass + "\"");
        String style = null;
        if (par != null && id != null && par.styles.containsKey(id)) 
            style = par.styles.get(id);
        if (style == null) 
            style = htmlStyle;
        if (htmlClass != null) {
            isDiv = !this.isInline();
            isSpan = !isDiv;
            if (isDiv) 
                res.append("\r\n");
            res.append("<").append((isDiv ? "div" : "span")).append(" ").append(_id).append(tit).append(">");
        }
        else if (style != null) {
            isDiv = !this.isInline();
            isSpan = !isDiv;
            if (isDiv) 
                res.append("\r\n");
            res.append("<").append((isDiv ? "div" : "span")).append(" style=\"").append(style).append("\" ").append(_id).append(tit).append(">");
        }
        else if (typ == UnitextContainerType.SHAPE) {
            res.append("\r\n<div style=\"border-width:2pt;border-color:green;border-style:dotted;margin:10pt;padding:5pt\" ").append(_id).append(tit).append(">");
            isDiv = true;
        }
        else if (typ == UnitextContainerType.FORMULA) {
            isDiv = !this.isInline();
            isSpan = !isDiv;
            if (isDiv) 
                res.append("\r\n");
            res.append("\r\n<").append((isDiv ? "div" : "span")).append(" style=\"border-width:2pt;border-color:blue;border-style:dotted;margin:5pt;padding:5pt\" ").append(tit).append(_id).append(">");
        }
        else if (typ == UnitextContainerType.MONOSPACE) {
            res.append("\r\n<div style=\"font-family:monospace\" ").append(_id).append(tit).append(">");
            res.append("<pre>");
            isDiv = true;
            isPre = true;
        }
        else if (typ == UnitextContainerType.CONTENTCONTROL) {
            isDiv = !this.isInline();
            isSpan = !isDiv;
            if (isDiv) 
                res.append("\r\n");
            res.append("<").append((isDiv ? "div" : "span")).append(" style=\"background-color:lightyellow;border:1pt solid black\" ").append(_id).append(tit).append(">");
        }
        else if (typ == UnitextContainerType.RIGHTALIGN) {
            res.append("\r\n<div style=\"font-weight:normal;font-style:italic;text-align:right\" ").append(_id).append(tit).append(">");
            isDiv = true;
        }
        else if (typ == UnitextContainerType.EDITION || typ == UnitextContainerType.COMMENT) {
            if (typ == UnitextContainerType.COMMENT && isInline()) 
                isSpan = true;
            else {
                isDiv = true;
                res.append("\r\n");
            }
            res.append("<").append((isSpan ? "span" : "div")).append(" ").append(_id).append(" style=\"font-weight: normal;font-style: italic;").append("font-size: smaller;color: gray").append("\"").append(tit).append(">");
        }
        else if (((typ == UnitextContainerType.HEAD || ((typ == UnitextContainerType.NAME && (parent instanceof UnitextDocblock))))) && !isInline()) {
            String ali = "center";
            int marg = 10;
            UnitextDocblock db = (UnitextDocblock)com.pullenti.unisharp.Utils.cast(parent, UnitextDocblock.class);
            if (db != null && db.typname != null) {
                if (((com.pullenti.unisharp.Utils.stringsCompare(db.typname, "Clause", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare(db.typname, "Mail", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare(db.typname, "Chapter", true) == 0) || com.pullenti.unisharp.Utils.stringsCompare(db.typname, "Paragraph", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare(db.typname, "SubParagraph", true) == 0) || com.pullenti.unisharp.Utils.stringsCompare(db.typname, "Section", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare(db.typname, "Subsection", true) == 0) {
                    ali = "left";
                    marg = 10;
                }
            }
            res.append("\r\n<div style=\"text-align:").append(ali).append(";margin-bottom:").append(marg).append("pt\" ").append(_id).append(tit).append(">");
            isDiv = true;
        }
        else if (typ == UnitextContainerType.DIRECTIVE) {
            res.append("\r\n<div style=\"text-align:center;text-decoration:underline\" ").append(_id).append(tit).append(">");
            isDiv = true;
        }
        else if (typ == UnitextContainerType.TAIL) {
            res.append("\r\n<div style=\"text-align:right\" ").append(_id).append(tit).append(">");
            isDiv = true;
        }
        else if (typ == UnitextContainerType.HIGHLIGHTING) {
            res.append("<span style=\"background-color:yellow\" ").append(_id).append(tit).append(">");
            isSpan = true;
        }
        else if (typ != UnitextContainerType.UNDEFINED) {
            if (parent != null && (parent.parent instanceof UnitextDocblock) && com.pullenti.unisharp.Utils.stringsCompare((((UnitextDocblock)com.pullenti.unisharp.Utils.cast(parent.parent, UnitextDocblock.class)).typname != null ? ((UnitextDocblock)com.pullenti.unisharp.Utils.cast(parent.parent, UnitextDocblock.class)).typname : ""), "INDEXITEM", true) == 0) {
                res.append("<span").append(_id).append(((tit != null ? tit : ""))).append(">");
                isSpan = true;
            }
            else 
                res.append("<b ").append(_id).append(((tit != null ? tit : ""))).append(">");
        }
        else if (((parent instanceof UnitextDocument) && par != null && par.outStyles) && m_StyledFrag != null && m_StyledFrag.getStyle() != null) {
            res.append("\r\n<div").append(_id).append(" style=\"");
            m_StyledFrag.getStyle().getHtml(res);
            res.append("\"").append(tit).append(">");
            isDiv = true;
        }
        else if (!com.pullenti.unisharp.Utils.isNullOrEmpty(_id)) {
            res.append("<span").append(_id).append(((tit != null ? tit : ""))).append(">");
            isSpan = true;
        }
        boolean outStyles = false;
        if (par != null && par.outStyles && !isInline()) {
            if (this.getStyledFragment(-1) != null) 
                outStyles = true;
        }
        UnitextStyledFragment curStFr = null;
        if (children.size() == 13) {
        }
        for (int i = 0; i < children.size(); i++) {
            UnitextItem ch = children.get(i);
            if (i == 9) {
            }
            if (ch instanceof UnitextNewline) {
                if (((UnitextNewline)com.pullenti.unisharp.Utils.cast(ch, UnitextNewline.class)).count == 1) {
                    if (((i + 1) < children.size()) && children.get(i + 1).getStartsOnNewline()) 
                        continue;
                    if ((i + 1) == children.size() && parent != null && !parent.isInline()) 
                        continue;
                }
            }
            if (outStyles) {
                UnitextStyledFragment fr = ch.getStyledFragment(-1);
                if (fr != null && fr.typ == UnitextStyledFragmentType.INLINE) 
                    fr = fr.parent;
                if (fr == null) {
                    if (curStFr != null) {
                        curStFr = null;
                        res.append("</").append((isSpan ? "span" : "div")).append(">");
                    }
                }
                else if (fr != curStFr) {
                    if (curStFr != null) 
                        res.append("</div>");
                    curStFr = null;
                    if (fr.styleId > 0 && fr.getStyle() != null) {
                        curStFr = fr;
                        res.append("\r\n<").append((isSpan ? "span" : "div")).append(" style=\"");
                        fr.getStyle().getHtml(res);
                        res.append("\">");
                    }
                }
            }
            ch.getHtml(res, par);
            if (par != null && res.length() > par.maxHtmlSize) 
                break;
        }
        if (curStFr != null) 
            res.append("</").append((isSpan ? "span" : "div")).append(">");
        if (isPre) 
            res.append("</pre>");
        if (isDiv) {
            res.append("</div>\r\n");
            if (par != null) 
                par._outFootnotes(res);
        }
        else if (isSpan) 
            res.append("</span>");
        else if (typ != UnitextContainerType.UNDEFINED) 
            res.append("</b>");
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("container");
        this._writeXmlAttrs(xml);
        if (typ != UnitextContainerType.UNDEFINED) 
            xml.wr.writeAttribute("type", typ.toString().toLowerCase());
        if (data != null) 
            xml.wr.writeAttribute("data", com.pullenti.util.MiscHelper.correctXmlValue(data));
        if (!com.pullenti.unisharp.Utils.isNullOrEmpty(htmlStyle)) 
            xml.wr.writeAttribute("style", com.pullenti.util.MiscHelper.correctXmlValue(htmlStyle));
        if (m_IsInline >= 0) 
            xml.wr.writeAttribute("inline", (m_IsInline > 0 ? "true" : "false"));
        for (UnitextItem ch : children) {
            ch.getXml(xml);
        }
        xml.wr.writeEndElement();
    }

    @Override
    public void fromXml(org.w3c.dom.Node xml) throws NumberFormatException {
        super.fromXml(xml);
        if (xml.getAttributes() != null) {
            for (org.w3c.dom.Node a : (new com.pullenti.unisharp.XmlAttrListWrapper(xml.getAttributes())).arr) {
                if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "type")) {
                    try {
                        typ = (UnitextContainerType)UnitextContainerType.of(a.getNodeValue());
                    } catch (Exception ex589) {
                    }
                }
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "data")) 
                    data = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "style")) 
                    htmlStyle = a.getNodeValue();
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "inline")) 
                    m_IsInline = (com.pullenti.unisharp.Utils.stringsEq(a.getNodeValue(), "true") ? 1 : 0);
            }
        }
        for (org.w3c.dom.Node x : (new com.pullenti.unisharp.XmlNodeListWrapper(xml.getChildNodes())).arr) {
            UnitextItem it = com.pullenti.unitext.internal.uni.UnitextHelper.createItem(x, this);
            if (it != null) 
                children.add(it);
        }
    }

    @Override
    public boolean isWhitespaces() {
        for (UnitextItem ch : children) {
            if (!ch.isWhitespaces()) 
                return false;
        }
        return true;
    }


    @Override
    public boolean isInline() {
        if (m_IsInline >= 0) 
            return m_IsInline > 0;
        if (parent instanceof UnitextListitem) 
            return false;
        if (children != null) {
            for (UnitextItem ch : children) {
                if (!ch.isInline()) 
                    return false;
            }
        }
        if (parent == null || (parent instanceof UnitextDocument)) 
            return false;
        return true;
    }

    @Override
    public boolean setInline(boolean value) {
        m_IsInline = (value ? 1 : 0);
        return value;
    }


    private int m_IsInline = -1;

    @Override
    public void getAllItems(java.util.ArrayList<UnitextItem> res, int lev) {
        if (lev > 20) {
        }
        if (res != null) 
            res.add(this);
        if (children != null) {
            for (UnitextItem ch : children) {
                ch.parent = this;
                ch.getAllItems(res, lev + 1);
            }
        }
    }

    @Override
    public void addPlainTextPos(int d) {
        super.addPlainTextPos(d);
        for (UnitextItem ch : children) {
            ch.addPlainTextPos(d);
        }
    }

    @Override
    public void correct(com.pullenti.unitext.internal.uni.LocCorrTyp _typ, Object _data) {
        for (UnitextItem ch : children) {
            ch.correct(_typ, _data);
        }
    }

    @Override
    public boolean replaceChild(UnitextItem old, UnitextItem ne) {
        int i = this.getChildIndexOf(old);
        if (i < 0) 
            return false;
        com.pullenti.unisharp.Utils.putArrayValue(children, i, ne);
        ne.parent = this;
        return true;
    }

    @Override
    public void setDefaultTextPos(com.pullenti.unisharp.Outargwrapper<Integer> cp, StringBuilder res) {
        beginChar = cp.value;
        for (UnitextItem ch : children) {
            ch.setDefaultTextPos(cp, res);
            ch.parent = this;
        }
        endChar = cp.value - 1;
    }

    public static UnitextContainer _new94(UnitextContainerType _arg1) {
        UnitextContainer res = new UnitextContainer();
        res.typ = _arg1;
        return res;
    }

    public static UnitextContainer _new397(int _arg1) {
        UnitextContainer res = new UnitextContainer();
        res.endChar = _arg1;
        return res;
    }

    public static UnitextContainer _new402(UnitextItem _arg1, int _arg2, int _arg3, String _arg4) {
        UnitextContainer res = new UnitextContainer();
        res.parent = _arg1;
        res.beginChar = _arg2;
        res.endChar = _arg3;
        res.id = _arg4;
        return res;
    }

    public static UnitextContainer _new452(UnitextContainerType _arg1, UnitextStyledFragment _arg2) {
        UnitextContainer res = new UnitextContainer();
        res.typ = _arg1;
        res.m_StyledFrag = _arg2;
        return res;
    }

    public static UnitextContainer _new588(UnitextContainerType _arg1, UnitextItem _arg2) {
        UnitextContainer res = new UnitextContainer();
        res.typ = _arg1;
        res.parent = _arg2;
        return res;
    }

    public static UnitextContainer _new591(int _arg1) {
        UnitextContainer res = new UnitextContainer();
        res.beginChar = _arg1;
        return res;
    }

    public static UnitextContainer _new596(int _arg1, int _arg2, UnitextItem _arg3) {
        UnitextContainer res = new UnitextContainer();
        res.beginChar = _arg1;
        res.endChar = _arg2;
        res.parent = _arg3;
        return res;
    }
    public UnitextContainer() {
        super();
    }
}
