/*
 * 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.internal.uni;

public class UnitextHelper {

    public static com.pullenti.unitext.UnitextDocument create(String fileName, byte[] fileContent, com.pullenti.unitext.CreateDocumentParam pars) throws Exception, java.io.FileNotFoundException, java.io.IOException {
        if (fileContent == null) {
            if (fileName == null) 
                return com.pullenti.unitext.UnitextDocument._new380(com.pullenti.unitext.FileFormat.UNKNOWN, ("Not filename, not content..."));
            fileName = com.pullenti.unisharp.Utils.getFullPath(fileName);
            if (!((new java.io.File(fileName)).exists() && (new java.io.File(fileName)).isFile())) 
                return com.pullenti.unitext.UnitextDocument._new380(com.pullenti.unitext.FileFormat.UNKNOWN, ("File '" + fileName + "' not exists"));
        }
        com.pullenti.unitext.FileFormat frm = com.pullenti.util.FileFormatsHelper.analizeFileFormat(fileName, fileContent);
        com.pullenti.unitext.UnitextDocument doc = null;
        try {
            if (com.pullenti.unitext.UnitextService.EXTENSION != null) 
                doc = com.pullenti.unitext.UnitextService.EXTENSION.createDocument(frm, fileName, fileContent, pars);
            if (doc == null) 
                doc = _create(frm, fileName, fileContent, pars);
            if (doc == null) 
                doc = com.pullenti.unitext.UnitextDocument._new380(frm, (frm == com.pullenti.unitext.FileFormat.UNKNOWN ? "Format unknown" : "Can't create document"));
        } catch (Exception ex) {
            doc = com.pullenti.unitext.UnitextDocument._new380(frm, ex.toString());
        }
        if (doc.sourceFileName == null) 
            doc.sourceFileName = fileName;
        java.util.ArrayList<com.pullenti.unitext.UnitextItem> all = new java.util.ArrayList<com.pullenti.unitext.UnitextItem>();
        doc.getAllItems(all, 0);
        java.util.ArrayList<com.pullenti.unitext.UnitextHyperlink> inHyps = new java.util.ArrayList<com.pullenti.unitext.UnitextHyperlink>();
        java.util.HashMap<String, com.pullenti.unitext.UnitextMisc> ancs = new java.util.HashMap<String, com.pullenti.unitext.UnitextMisc>();
        for (com.pullenti.unitext.UnitextItem it : all) {
            com.pullenti.unitext.UnitextMisc mi = (com.pullenti.unitext.UnitextMisc)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextMisc.class);
            if (mi != null && mi.typ == com.pullenti.unitext.UnitextMiscType.ANCHOR) {
                if (!ancs.containsKey(mi.id)) 
                    ancs.put(mi.id, mi);
                mi.tag = null;
            }
            com.pullenti.unitext.UnitextHyperlink hyp = (com.pullenti.unitext.UnitextHyperlink)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextHyperlink.class);
            if (hyp != null) {
                if ((hyp.toString().indexOf("9.4") >= 0)) {
                }
            }
            if (hyp != null && hyp.href != null && hyp.isInternal) {
                inHyps.add(hyp);
                if (hyp.content == null) {
                    com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(hyp.parent, com.pullenti.unitext.UnitextContainer.class);
                    if (cnt != null) {
                        int i = cnt.getChildIndexOf(hyp);
                        if (i >= 0 && ((i + 1) < cnt.children.size())) {
                            int i0 = i;
                            i++;
                            com.pullenti.unitext.UnitextItem vv = cnt.children.get(i);
                            while ((vv instanceof com.pullenti.unitext.UnitextNewline) && ((i + 1) < cnt.children.size())) {
                                i++;
                                vv = cnt.children.get(i);
                            }
                            if (!(vv instanceof com.pullenti.unitext.UnitextNewline)) {
                                hyp.content = vv;
                                vv.parent = hyp;
                                com.pullenti.unisharp.Utils.putArrayValue(cnt.children, i, hyp);
                                cnt.children.remove(i0);
                            }
                        }
                    }
                }
            }
            com.pullenti.unitext.UnitextImage img = (com.pullenti.unitext.UnitextImage)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextImage.class);
            if (img != null) {
                if (img.content == null && img.id != null) {
                    if (img.id.startsWith("data:image")) {
                        int ii = img.id.indexOf("base64,");
                        if (ii > 0) {
                            try {
                                img.content = java.util.Base64.getDecoder().decode((img.id.substring(ii + 7)).replace("\r", "").replace("\n", ""));
                                img.id = null;
                            } catch (Exception ex) {
                            }
                        }
                    }
                    else if (img.id.startsWith("http") || img.id.startsWith("www")) {
                        img.htmlSrcUri = img.id;
                        continue;
                    }
                }
                if (img.content != null && img.content.length > 10) {
                    if (img.content[0] == ((byte)0x1F) && img.content[1] == ((byte)0x8B)) {
                        try {
                            img.content = com.pullenti.util.ArchiveHelper.decompressGZip(img.content);
                        } catch (Exception ex384) {
                        }
                    }
                }
                if (img.content == null && img.id != null && (img.id.length() < 30)) 
                    continue;
                if (pars != null && pars.maxImageSize > 0 && img.content != null) {
                    if (img.content.length > pars.maxImageSize) 
                        img.content = null;
                }
            }
        }
        int refid = 1;
        for (com.pullenti.unitext.UnitextHyperlink h : inHyps) {
            com.pullenti.unitext.UnitextMisc anc;
            com.pullenti.unisharp.Outargwrapper<com.pullenti.unitext.UnitextMisc> wrapanc385 = new com.pullenti.unisharp.Outargwrapper<com.pullenti.unitext.UnitextMisc>();
            boolean inoutres386 = com.pullenti.unisharp.Utils.tryGetValue(ancs, h.href, wrapanc385);
            anc = wrapanc385.value;
            if (!inoutres386) 
                continue;
            com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(anc.parent, com.pullenti.unitext.UnitextContainer.class);
            com.pullenti.unitext.UnitextMisc anc0 = anc;
            if (cnt != null) {
                int i = cnt.getChildIndexOf(anc);
                com.pullenti.unitext.UnitextItem ait = null;
                if (i >= 0) {
                    for (int j = i - 1; j >= 0; j--) {
                        com.pullenti.unitext.UnitextItem it = cnt.children.get(j);
                        if (((it instanceof com.pullenti.unitext.UnitextNewline) || (it instanceof com.pullenti.unitext.UnitextPagebreak) || (it instanceof com.pullenti.unitext.UnitextTable)) || (it instanceof com.pullenti.unitext.UnitextList)) 
                            break;
                        if (it instanceof com.pullenti.unitext.UnitextComment) 
                            continue;
                        com.pullenti.unitext.UnitextMisc um = (com.pullenti.unitext.UnitextMisc)com.pullenti.unisharp.Utils.cast(it, com.pullenti.unitext.UnitextMisc.class);
                        if (um != null) {
                            if (um.typ == com.pullenti.unitext.UnitextMiscType.HORIZONTALLINE) 
                                break;
                            continue;
                        }
                        ait = it;
                    }
                    if (ait == null) {
                        for (int j = i + 1; j < cnt.children.size(); j++) {
                            com.pullenti.unitext.UnitextItem it = cnt.children.get(j);
                            if (((it instanceof com.pullenti.unitext.UnitextNewline) || (it instanceof com.pullenti.unitext.UnitextMisc) || (it instanceof com.pullenti.unitext.UnitextPagebreak)) || (it instanceof com.pullenti.unitext.UnitextComment)) 
                                continue;
                            ait = it;
                            break;
                        }
                    }
                    if (ait != null) {
                        if (ait.id == null) {
                            if (refid == 101) {
                            }
                            ait.id = (ait.getInnerTag() + "_ref" + refid);
                            refid++;
                        }
                        h.href = ait.id;
                        anc0 = null;
                    }
                }
                if (i > 0 && ait == null) {
                    for (int j = i - 1; j >= 0; j--) {
                        if ((cnt.children.get(j) instanceof com.pullenti.unitext.UnitextMisc) && ((com.pullenti.unitext.UnitextMisc)com.pullenti.unisharp.Utils.cast(cnt.children.get(j), com.pullenti.unitext.UnitextMisc.class)).typ == com.pullenti.unitext.UnitextMiscType.ANCHOR) 
                            anc0 = (com.pullenti.unitext.UnitextMisc)com.pullenti.unisharp.Utils.cast(cnt.children.get(j), com.pullenti.unitext.UnitextMisc.class);
                        else 
                            break;
                    }
                }
            }
            if (anc0 != null) {
                if (anc0 != anc) 
                    h.href = anc0.id;
                anc0.tag = h;
            }
        }
        for (com.pullenti.unitext.UnitextMisc a : ancs.values()) {
            if (a.tag == null) {
                com.pullenti.unitext.UnitextContainer cnt = (com.pullenti.unitext.UnitextContainer)com.pullenti.unisharp.Utils.cast(a.parent, com.pullenti.unitext.UnitextContainer.class);
                if (cnt == null) 
                    continue;
                int i = cnt.getChildIndexOf(a);
                if (i >= 0) 
                    cnt.children.remove(i);
            }
        }
        return doc;
    }

    public static byte[] loadDataFromFile(String fileName, int attampts) throws Exception, java.io.IOException, InterruptedException {
        com.pullenti.unisharp.FileStream fStr = null;
        byte[] buf = null;
        try {
            Exception ex = null;
            for (int i = 0; i <= attampts; i++) {
                try {
                    fStr = new com.pullenti.unisharp.FileStream(fileName, "r", "");
                    break;
                } catch (Exception e) {
                    ex = e;
                }
                if (i == 0 && !((new java.io.File(fileName)).exists() && (new java.io.File(fileName)).isFile())) 
                    break;
                Thread.sleep(500L);
            }
            if (fStr == null) 
                throw ex;
            if (fStr.length() == (0L)) 
                return null;
            buf = new byte[(int)fStr.length()];
            fStr.read(buf, 0, (int)fStr.length());
        } finally {
            if (fStr != null) 
                fStr.close();
        }
        return buf;
    }

    private static com.pullenti.unitext.UnitextDocument _create(com.pullenti.unitext.FileFormat frm, String fileName, byte[] fileContent, com.pullenti.unitext.CreateDocumentParam pars) throws Exception, java.io.FileNotFoundException, java.io.IOException, InterruptedException, javax.xml.stream.XMLStreamException, org.xml.sax.SAXException, IllegalArgumentException, UnsupportedOperationException, NumberFormatException {
        com.pullenti.unitext.FileFormatClass frmcl = com.pullenti.util.FileFormatsHelper.getFormatClass(frm);
        if (frmcl == com.pullenti.unitext.FileFormatClass.ARCHIVE) {
            com.pullenti.unitext.UnitextDocument res = com.pullenti.unitext.UnitextDocument._new69(fileName, frm);
            if (pars.ignoreInnerDocuments) 
                return res;
            java.util.HashMap<String, Integer> files = null;
            try {
                files = com.pullenti.util.ArchiveHelper.getFileNamesFromArchive(fileName, fileContent);
            } catch (Exception ex) {
                res.errorMessage = ex.getMessage();
                return res;
            }
            if (files == null) {
                res.errorMessage = ("Archive format " + frm.toString() + " not supported");
                return res;
            }
            if (files.size() == 0) 
                return res;
            for (java.util.Map.Entry<String, Integer> f : files.entrySet()) {
                String ext = com.pullenti.unisharp.Utils.getFileExt(f.getKey());
                if (com.pullenti.unisharp.Utils.stringsEq(ext, ".exe") || com.pullenti.unisharp.Utils.stringsEq(ext, ".dll")) 
                    continue;
                try {
                    byte[] cnt = com.pullenti.util.ArchiveHelper.getFileFromArchive(fileName, fileContent, f.getKey());
                    if (cnt == null || cnt.length == 0) 
                        continue;
                    com.pullenti.unitext.UnitextDocument d = com.pullenti.unitext.UnitextService.createDocument(f.getKey(), cnt, pars);
                    res.innerDocuments.add(d);
                } catch (Exception ex) {
                    com.pullenti.unitext.UnitextDocument d = com.pullenti.unitext.UnitextDocument._new69(f.getKey(), com.pullenti.util.FileFormatsHelper.analizeFormat(ext, null));
                    d.errorMessage = ex.getMessage();
                    res.innerDocuments.add(d);
                }
            }
            return res;
        }
        if (fileContent == null) {
            java.io.File fi = new java.io.File(fileName);
            if (fi.length() < (10000000L)) 
                fileContent = loadDataFromFile(fileName, 0);
        }
        if (frm == com.pullenti.unitext.FileFormat.RTF) {
            if (fileContent != null) {
                try (com.pullenti.unisharp.MemoryStream mem = new com.pullenti.unisharp.MemoryStream(fileContent)) {
                    return com.pullenti.unitext.internal.rtf.RtfHelper.createUniDoc(mem, pars);
                }
            }
            else {
                try (com.pullenti.unisharp.FileStream f = new com.pullenti.unisharp.FileStream(fileName, "r", "")) {
                    return com.pullenti.unitext.internal.rtf.RtfHelper.createUniDoc(f, pars);
                }
            }
        }
        if ((frm == com.pullenti.unitext.FileFormat.DOCX || frm == com.pullenti.unitext.FileFormat.XLSX || frm == com.pullenti.unitext.FileFormat.PPTX) || frm == com.pullenti.unitext.FileFormat.ODT || frm == com.pullenti.unitext.FileFormat.DOCXML) {
            try (com.pullenti.unitext.internal.word.DocxToText d2t = new com.pullenti.unitext.internal.word.DocxToText(fileName, fileContent, frm == com.pullenti.unitext.FileFormat.DOCXML, (pars != null ? pars.setFormulaAsImage : false))) {
                d2t.maxTableRows = pars.maxTableRows;
                com.pullenti.unitext.UnitextDocument doc = d2t.createUniDoc(pars.onlyForPureText, frm, pars);
                if (doc != null) {
                    doc.optimize(false, pars);
                    StyleHelper.processDoc(doc);
                }
                return doc;
            }
        }
        if (frm == com.pullenti.unitext.FileFormat.XLSXML) {
            com.pullenti.unitext.internal.misc.MyXmlReader xm = null;
            if (fileContent != null) {
                xm = com.pullenti.unitext.internal.misc.MyXmlReader.create(fileContent);
                return com.pullenti.unitext.internal.misc.ExcelHelper.createDocForXml(xm, pars.maxTableRows);
            }
            else {
                xm = com.pullenti.unitext.internal.misc.MyXmlReader.create(loadDataFromFile(fileName, 0));
                return com.pullenti.unitext.internal.misc.ExcelHelper.createDocForXml(xm, pars.maxTableRows);
            }
        }
        if (frm == com.pullenti.unitext.FileFormat.DOC) {
            com.pullenti.unitext.UnitextDocument doc = null;
            if (fileContent != null) 
                doc = com.pullenti.unitext.internal.word.MSOfficeHelper._uniFromWord97Arr(fileContent);
            else 
                doc = com.pullenti.unitext.internal.word.MSOfficeHelper._uniFromWord97File(fileName);
            if (doc == null) 
                return null;
            if (doc.attrs.containsKey("word") && com.pullenti.unisharp.Utils.stringsEq(doc.attrs.get("word"), "6")) {
                if (pars.ignoreWord6) {
                    doc = com.pullenti.unitext.UnitextDocument._new47(com.pullenti.unitext.FileFormat.DOC);
                    doc.errorMessage = "Word6 not supported";
                    return doc;
                }
            }
            doc.optimize(false, pars);
            return doc;
        }
        if (frm == com.pullenti.unitext.FileFormat.PDF) 
            return com.pullenti.unitext.internal.pdf.PdfHelper.createUni(fileName, fileContent, pars);
        if (frm == com.pullenti.unitext.FileFormat.HTML) {
            com.pullenti.unitext.internal.html.HtmlNode nod = com.pullenti.unitext.internal.html.HtmlHelper.createNode(fileName, fileContent, null);
            if (nod == null) 
                return null;
            String dirName = null;
            if (!pars.onlyForPureText && fileName != null && ((new java.io.File(fileName)).exists() && (new java.io.File(fileName)).isFile())) {
                String dir = (new java.io.File(com.pullenti.unisharp.Utils.getFullPath(fileName))).getAbsoluteFile().getParent();
                if (((new java.io.File(dir)).exists() && (new java.io.File(dir)).isDirectory())) 
                    dirName = dir;
            }
            com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.internal.html.HtmlHelper.create(nod, dirName, null, pars);
            return doc;
        }
        if (frm == com.pullenti.unitext.FileFormat.XML) {
            com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new47(com.pullenti.unitext.FileFormat.XML);
            String txt = com.pullenti.unitext.internal.html.HtmlHelper.getXmlText(fileName, fileContent);
            if (txt != null) 
                doc.content = com.pullenti.unitext.UnitextPlaintext._new391(com.pullenti.unitext.UnitextPlaintextType.XML, txt);
            else 
                doc.errorMessage = "Размер XML слишком большой";
            return doc;
        }
        if (frm == com.pullenti.unitext.FileFormat.MHT || frm == com.pullenti.unitext.FileFormat.EML) {
            com.pullenti.unisharp.FileStream fs = null;
            if (fileContent == null) 
                fileContent = loadDataFromFile(fileName, 0);
            try {
                com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.internal.html.MhtHelper._createDoc(fileContent, pars, frm == com.pullenti.unitext.FileFormat.MHT);
                if (doc != null) {
                    doc.optimize(false, pars);
                    doc.sourceFileName = fileName;
                    doc.sourceFormat = frm;
                }
                else 
                    doc = com.pullenti.unitext.UnitextDocument._new380(frm, "Не удалось сформировать письмо");
                return doc;
            } finally {
                if (fs != null) 
                    fs.close();
            }
        }
        if (frm == com.pullenti.unitext.FileFormat.FB2) 
            return com.pullenti.unitext.internal.misc.Fb2Helper.createDoc(fileName, fileContent, pars);
        if (frm == com.pullenti.unitext.FileFormat.FB3) 
            return com.pullenti.unitext.internal.misc.Fb2Helper.createDocZip(fileName, fileContent, pars);
        if (frm == com.pullenti.unitext.FileFormat.EPUB) 
            return com.pullenti.unitext.internal.misc.EpubHelper.createDoc(fileName, fileContent, pars);
        if (frm == com.pullenti.unitext.FileFormat.TXT || frm == com.pullenti.unitext.FileFormat.CSV) {
            String txt = null;
            if (fileContent != null) 
                txt = com.pullenti.util.TextHelper.readStringFromBytes(fileContent, false, false);
            else 
                txt = com.pullenti.util.TextHelper.readStringFromFile(fileName, false, false);
            if (com.pullenti.unisharp.Utils.isNullOrEmpty(txt)) 
                return null;
            int len = txt.length();
            if (frm == com.pullenti.unitext.FileFormat.CSV) {
                com.pullenti.unitext.UnitextDocument csv = com.pullenti.unitext.internal.misc.CsvHelper.create(txt);
                if (csv != null) 
                    return csv;
            }
            UnitextGen gen = new UnitextGen();
            gen.appendText(txt, true);
            com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new47(com.pullenti.unitext.FileFormat.TXT);
            doc.content = gen.finish(true, null);
            if (doc.content == null) 
                return null;
            return doc;
        }
        if (frm == com.pullenti.unitext.FileFormat.TIF) {
            if (fileContent == null) 
                fileContent = loadDataFromFile(fileName, 0);
            com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.internal.misc.TiffHelper.createDoc(fileContent);
            doc.refreshContentByPages();
            return doc;
        }
        if (frmcl == com.pullenti.unitext.FileFormatClass.IMAGE) {
            com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new394(frm, fileName);
            com.pullenti.unitext.UnilayPage page = new com.pullenti.unitext.UnilayPage();
            page.imageContent = (fileContent != null ? fileContent : UnitextHelper.loadDataFromFile(fileName, 0));
            doc.pages.add(page);
            doc.refreshContentByPages();
            return doc;
        }
        com.pullenti.unitext.UnitextDocument err = com.pullenti.unitext.UnitextDocument._new380(frm, "Unsupported format");
        if (frm != com.pullenti.unitext.FileFormat.UNKNOWN) 
            err.errorMessage = (err.errorMessage + " " + frm.toString());
        return err;
    }

    public static com.pullenti.unitext.UnitextItem createItem(org.w3c.dom.Node xml, com.pullenti.unitext.UnitextItem par) throws NumberFormatException {
        com.pullenti.unitext.UnitextItem res = null;
        switch (com.pullenti.unisharp.Utils.getXmlLocalName(xml)) { 
        case "container":
            res = new com.pullenti.unitext.UnitextContainer();
            break;
        case "text":
            res = new com.pullenti.unitext.UnitextPlaintext();
            break;
        case "newline":
            res = new com.pullenti.unitext.UnitextNewline();
            break;
        case "pagebreak":
            res = new com.pullenti.unitext.UnitextPagebreak();
            break;
        case "table":
            res = new com.pullenti.unitext.UnitextTable();
            break;
        case "cell":
            res = new com.pullenti.unitext.UnitextTablecell();
            break;
        case "hyperlink":
            res = new com.pullenti.unitext.UnitextHyperlink();
            break;
        case "footnote":
            res = new com.pullenti.unitext.UnitextFootnote();
            break;
        case "list":
            res = new com.pullenti.unitext.UnitextList();
            break;
        case "listitem":
            res = new com.pullenti.unitext.UnitextListitem();
            break;
        case "image":
            res = new com.pullenti.unitext.UnitextImage();
            break;
        case "comment":
            res = new com.pullenti.unitext.UnitextComment();
            break;
        case "docblock":
            res = new com.pullenti.unitext.UnitextDocblock();
            break;
        case "misc":
            res = new com.pullenti.unitext.UnitextMisc();
            break;
    }
    if (res != null) {
        res.parent = par;
        res.fromXml(xml);
    }
    return res;
    }

    public static com.pullenti.unitext.UnitextDocument createDocFromText(String text) {
        com.pullenti.unitext.UnitextDocument doc = com.pullenti.unitext.UnitextDocument._new396((text != null ? text : ""));
        if (com.pullenti.unisharp.Utils.isNullOrEmpty(text)) 
            return doc;
        com.pullenti.unitext.UnitextItem cur = null;
        com.pullenti.unitext.UnitextContainer cnt = com.pullenti.unitext.UnitextContainer._new397(text.length() - 1);
        doc.content = cnt;
        cnt.parent = doc;
        doc.endChar = text.length() - 1;
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch == '\r' || ch == '\n') {
                if (!(cur instanceof com.pullenti.unitext.UnitextNewline)) {
                    cur = com.pullenti.unitext.UnitextNewline._new398(cnt);
                    cnt.children.add(cur);
                    cur.beginChar = i;
                }
                cur.endChar = i;
                if (ch == '\r') 
                    ((com.pullenti.unitext.UnitextNewline)com.pullenti.unisharp.Utils.cast(cur, com.pullenti.unitext.UnitextNewline.class)).count++;
                else if (ch == '\n') {
                    if (i == 0 || text.charAt(i - 1) != '\r') 
                        ((com.pullenti.unitext.UnitextNewline)com.pullenti.unisharp.Utils.cast(cur, com.pullenti.unitext.UnitextNewline.class)).count++;
                }
            }
            else if (((int)ch) == 0xC) {
                cur = com.pullenti.unitext.UnitextPagebreak._new399(cnt);
                cnt.children.add(cur);
                cur.beginChar = i;
                cur.endChar = i;
            }
            else {
                if (!(cur instanceof com.pullenti.unitext.UnitextPlaintext)) {
                    cur = com.pullenti.unitext.UnitextPlaintext._new400(cnt);
                    cnt.children.add(cur);
                    cur.beginChar = i;
                }
                cur.endChar = i;
            }
        }
        for (com.pullenti.unitext.UnitextItem ch : cnt.children) {
            if ((ch instanceof com.pullenti.unitext.UnitextPlaintext) && ch.beginChar <= ch.endChar) 
                ((com.pullenti.unitext.UnitextPlaintext)com.pullenti.unisharp.Utils.cast(ch, com.pullenti.unitext.UnitextPlaintext.class)).text = text.substring(ch.beginChar, ch.beginChar + (ch.endChar - ch.beginChar) + 1);
        }
        doc.generateIds();
        return doc;
    }

    private static com.pullenti.unitext.GetPlaintextParam m_StdParams;

    public static String getPlaintext(com.pullenti.unitext.UnitextItem it, com.pullenti.unitext.GetPlaintextParam pars) {
        if (it == null) 
            return null;
        if (pars == null) 
            pars = m_StdParams;
        StringBuilder res = new StringBuilder();
        it.getPlaintext(res, pars);
        if (res.length() == 0) 
            return null;
        return res.toString();
    }

    public static String[] m_Romans;

    public UnitextHelper() {
    }
    
    static {
        m_StdParams = com.pullenti.unitext.GetPlaintextParam._new357(false);
        m_Romans = new String[] {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI", "XXVII", "XXVIII", "XXIX", "XXX"};
    }
}
