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

/**
 * Это блок данных
 */
public class DocxPart {

    public String name;

    public org.w3c.dom.Node xml;

    public byte[] data;

    public boolean isName(String _name) {
        if (com.pullenti.unisharp.Utils.stringsCompare(_name, name, true) == 0) 
            return true;
        if (name.startsWith("/")) {
            if (com.pullenti.unisharp.Utils.stringsCompare(_name, name.substring(1), true) == 0) 
                return true;
        }
        return false;
    }

    public boolean isNameStarts(String _name) {
        if (com.pullenti.unisharp.Utils.startsWithString(name, _name, true)) 
            return true;
        return false;
    }

    public com.pullenti.unitext.internal.misc.MyZipEntry zipEntry;

    @Override
    public String toString() {
        return name;
    }

    private static boolean _startOfIns(byte[] dat, int i) {
        if ((i + 7) >= dat.length) 
            return false;
        if (((((char)dat[i]) == '<' && ((char)dat[i + 1]) == 'w' && ((char)dat[i + 2]) == ':') && ((char)dat[i + 3]) == 'i' && ((char)dat[i + 4]) == 'n') && ((char)dat[i + 5]) == 's') {
            if (((char)dat[i + 6]) == ' ' || ((char)dat[i + 6]) == '>') 
                return true;
        }
        if (((((char)dat[i]) == '<' && ((char)dat[i + 1]) == '/' && ((char)dat[i + 2]) == 'w') && ((char)dat[i + 3]) == ':' && ((char)dat[i + 4]) == 'i') && ((char)dat[i + 5]) == 'n' && ((char)dat[i + 6]) == 's') {
            if (((char)dat[i + 7]) == ' ' || ((char)dat[i + 7]) == '>') 
                return true;
        }
        return false;
    }

    public org.w3c.dom.Node getXmlNode(boolean correct) throws Exception, javax.xml.stream.XMLStreamException, java.io.IOException, org.xml.sax.SAXException {
        if (xml != null) 
            return xml;
        if (data != null) 
            return null;
        com.pullenti.unisharp.XmlDocumentWrapper _xml = new com.pullenti.unisharp.XmlDocumentWrapper();
        byte[] dat = zipEntry.getData();
        if (dat == null) 
            return null;
        boolean hasIns = false;
        for (int i = 0; i < (dat.length - 6); i++) {
            if (_startOfIns(dat, i)) {
                hasIns = true;
                break;
            }
        }
        if (hasIns) {
            java.util.ArrayList<Byte> buf = new java.util.ArrayList<Byte>(dat.length);
            for (int i = 0; i < dat.length; i++) {
                if (!_startOfIns(dat, i)) 
                    buf.add(dat[i]);
                else 
                    for (; i < dat.length; i++) {
                        if (((char)dat[i]) == '>') 
                            break;
                    }
            }
            dat = com.pullenti.unisharp.Utils.toBytesArray(buf.toArray(new Byte[buf.size()]));
        }
        try (com.pullenti.unisharp.MemoryStream documentXml = new com.pullenti.unisharp.MemoryStream(dat)) {
            _xml.setPreserveWhitespace(true);
            _xml.load(documentXml);
            return _xml.doc.getDocumentElement();
        }
    }

    private static String _getTagName(String _xml, int i) {
        for (int j = i; j < _xml.length(); j++) {
            if (!Character.isLetter(_xml.charAt(j)) && _xml.charAt(j) != ':') {
                if (j == i) 
                    return null;
                return _xml.substring(i, i + j - i);
            }
        }
        return null;
    }

    public byte[] getBytes() {
        if (data != null) 
            return data;
        if (xml != null) 
            return null;
        // всё-равно эти картинки не поддержаны браузером, а то на больших документах памяти не хватает
        if (com.pullenti.unisharp.Utils.endsWithString(zipEntry.name, ".emf", true)) 
            return null;
        try {
            return zipEntry.getData();
        } catch (Exception ex) {
            return null;
        }
    }

    public static DocxPart _new488(String _arg1, com.pullenti.unitext.internal.misc.MyZipEntry _arg2) {
        DocxPart res = new DocxPart();
        res.name = _arg1;
        res.zipEntry = _arg2;
        return res;
    }

    public static DocxPart _new489(String _arg1, org.w3c.dom.Node _arg2) {
        DocxPart res = new DocxPart();
        res.name = _arg1;
        res.xml = _arg2;
        return res;
    }
    public DocxPart() {
    }
}
