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

public class HtmlHelper {

    public static String getXmlText(String fileName, byte[] fileContent) throws Exception, java.io.IOException, InterruptedException {
        if (fileContent == null) 
            fileContent = com.pullenti.unitext.internal.uni.UnitextHelper.loadDataFromFile(fileName, 0);
        if (fileContent == null) 
            return null;
        if (fileContent.length > 10000000) 
            return null;
        String str = com.pullenti.util.TextHelper.readStringFromBytes(fileContent, false, false);
        if (str == null) 
            return null;
        int i = str.indexOf("encoding=");
        if (i > 0 && (i < 100)) {
            i += 9;
            if (str.charAt(i) == '\'' || str.charAt(i) == '"') 
                i++;
            int j;
            for (j = i; j < str.length(); j++) {
                if (!Character.isLetterOrDigit(str.charAt(j)) && str.charAt(j) != '-') 
                    break;
            }
            if (j > i) {
                String encod = str.substring(i, i + j - i);
                com.pullenti.util.EncodingWrapper wr = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, encod, 0);
                str = wr.getString(fileContent, 0, -1);
                return str;
            }
        }
        return str;
    }

    public static HtmlNode createNode(String fileName, byte[] fileContent, String contentType) throws Exception, java.io.IOException, InterruptedException {
        if (fileContent == null) 
            fileContent = com.pullenti.unitext.internal.uni.UnitextHelper.loadDataFromFile(fileName, 0);
        if (fileContent == null) 
            return null;
        String str = com.pullenti.util.TextHelper.readStringFromBytes(fileContent, false, false);
        if (str == null) 
            return null;
        for (int k = 0; k < 2; k++) {
            String str0 = (k == 0 ? contentType : str);
            if (str0 == null) 
                continue;
            int i = str0.indexOf("charset=");
            if (i > 0) {
                i += 8;
                if (str0.charAt(i) == '\'' || str0.charAt(i) == '"') 
                    i++;
                int j;
                for (j = i; j < str0.length(); j++) {
                    if (!Character.isLetterOrDigit(str0.charAt(j)) && str0.charAt(j) != '-') 
                        break;
                }
                if (j > i) {
                    String encod = str0.substring(i, i + j - i);
                    com.pullenti.util.EncodingWrapper wr = new com.pullenti.util.EncodingWrapper(com.pullenti.util.EncodingStandard.UNDEFINED, encod, 0);
                    str = wr.getString(fileContent, 0, -1);
                    break;
                }
            }
        }
        StringBuilder tmp = new StringBuilder(str);
        HtmlNode nod = HtmlParser.parse(tmp, false);
        return nod;
    }

    public static com.pullenti.unitext.UnitextDocument create(HtmlNode nod, String dirName, java.util.HashMap<String, byte[]> images, com.pullenti.unitext.CreateDocumentParam pars) {
        UnitextHtmlGen gen = new UnitextHtmlGen();
        return gen.create(nod, dirName, images, pars);
    }
    public HtmlHelper() {
    }
}
