/*
 * 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 UnitextPagebreak extends UnitextItem {

    /**
     * Предположительный тип (вычисляется в ходе анализа)
     */
    public WhitespaceType typ = WhitespaceType.ORIGINAL;

    /**
     * Для PDF это номер страницы, который заканчивает этот разделитель (1 - первая страница)
     */
    public int pageNumber;

    public UnitextPagebreak() {
        super();
    }

    @Override
    public String toString() {
        String res = "PageBreak";
        if (pageNumber > 0) 
            res = ("PageBreak N=" + pageNumber);
        if (typ == WhitespaceType.IGNORABLE) 
            res += " - ignore";
        else if (typ == WhitespaceType.ONESPACE) 
            res += " - space";
        return res;
    }

    @Override
    public UnitextItem clone() {
        UnitextPagebreak res = new UnitextPagebreak();
        res._cloneFrom(this);
        res.typ = typ;
        res.pageNumber = pageNumber;
        return res;
    }

    @Override
    public void getPlaintext(StringBuilder res, GetPlaintextParam pars) {
        if (pars != null && pars.setPositions) 
            beginChar = res.length();
        if (typ == WhitespaceType.IGNORABLE) {
        }
        else if (typ == WhitespaceType.ONESPACE) 
            res.append(' ');
        else {
            res.append(((((pars != null ? pars : UnitextItem.m_DefParams))).newLine != null ? (((pars != null ? pars : UnitextItem.m_DefParams))).newLine : ""));
            res.append(((((pars != null ? pars : UnitextItem.m_DefParams))).pageBreak != null ? (((pars != null ? pars : UnitextItem.m_DefParams))).pageBreak : ""));
        }
        if (pars != null && pars.setPositions) 
            endChar = res.length() - 1;
    }

    @Override
    public void getHtml(StringBuilder res, GetHtmlParam par) {
        if (typ == WhitespaceType.IGNORABLE) 
            return;
        if (typ == WhitespaceType.ONESPACE) {
            res.append(' ');
            return;
        }
        if (!par.callBefore(this, res)) 
            return;
        int k = 0;
        for (UnitextItem p = parent; p != null && (k < 10); p = p.parent,k++) {
            UnitextContainer cnt = (UnitextContainer)com.pullenti.unisharp.Utils.cast(p, UnitextContainer.class);
            if (cnt != null) {
                if (cnt.typ != UnitextContainerType.UNDEFINED && cnt.typ != UnitextContainerType.HEAD) {
                    res.append("\r\n<BR/>");
                    par.callAfter(this, res);
                    return;
                }
            }
            UnitextDocblock db = (UnitextDocblock)com.pullenti.unisharp.Utils.cast(p, UnitextDocblock.class);
            if (db != null) {
                if (com.pullenti.unisharp.Utils.stringsCompare((db.typname != null ? db.typname : ""), "Document", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare((db.typname != null ? db.typname : ""), "Subdocument", true) == 0 || com.pullenti.unisharp.Utils.stringsCompare((db.typname != null ? db.typname : ""), "Appendix", true) == 0) {
                }
                else if (db.typname != null) {
                    res.append("\r\n<BR/>");
                    par.callAfter(this, res);
                    return;
                }
            }
        }
        if (par != null) 
            par._outFootnotes(res);
        res.append("\r\n<HR");
        if (pageNumber > 0) 
            res.append(" title=\"End of page ").append(pageNumber).append("\"");
        res.append("/><br/></br>");
        par.callAfter(this, res);
    }

    @Override
    public void getXml(com.pullenti.unisharp.XmlWriterWrapper xml) throws javax.xml.stream.XMLStreamException {
        xml.wr.writeStartElement("pagebreak");
        this._writeXmlAttrs(xml);
        if (typ != WhitespaceType.ORIGINAL) 
            xml.wr.writeAttribute("typ", typ.toString().toLowerCase());
        if (pageNumber > 0) 
            xml.wr.writeAttribute("pagenum", ((Integer)pageNumber).toString());
        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), "typ")) {
                    try {
                        typ = (WhitespaceType)WhitespaceType.of(a.getNodeValue());
                    } catch (Exception ex618) {
                    }
                }
                else if (com.pullenti.unisharp.Utils.stringsEq(com.pullenti.unisharp.Utils.getXmlLocalName(a), "pagenum")) 
                    pageNumber = com.pullenti.unisharp.Utils.parseInteger(a.getNodeValue(), 0, null);
            }
        }
    }

    @Override
    public boolean isWhitespaces() {
        return true;
    }


    @Override
    public boolean isInline() {
        if (typ != WhitespaceType.ORIGINAL) 
            return true;
        return false;
    }


    @Override
    public void setDefaultTextPos(com.pullenti.unisharp.Outargwrapper<Integer> cp, StringBuilder res) {
        beginChar = (endChar = cp.value);
        cp.value++;
        if (res != null) 
            res.append('\f');
    }

    public static UnitextPagebreak _new351(String _arg1, int _arg2) {
        UnitextPagebreak res = new UnitextPagebreak();
        res.pageSectionId = _arg1;
        res.pageNumber = _arg2;
        return res;
    }

    public static UnitextPagebreak _new370(String _arg1) {
        UnitextPagebreak res = new UnitextPagebreak();
        res.pageSectionId = _arg1;
        return res;
    }

    public static UnitextPagebreak _new399(UnitextItem _arg1) {
        UnitextPagebreak res = new UnitextPagebreak();
        res.parent = _arg1;
        return res;
    }
}
