Commit c16291e1 authored by jan.koester's avatar jan.koester
Browse files

tes

parent 95327593
Loading
Loading
Loading
Loading
+328 −0

File added.

Preview size limit exceeded, changes collapsed.

+154 −0
Original line number Diff line number Diff line
@@ -170,6 +170,20 @@ public:

            val = xml_data->Attribute("version");
            if (val) version = val;

            json_object *legacyProps = json_object_new_object();
            importLegacyAttributes(xml_data, legacyProps);
            importLegacyInlineText(xml_data, legacyProps);
            importLegacyTableCells(xml_data, legacyProps);

            const char *legacyPropsStr = json_object_to_json_string_ext(
                legacyProps,
                JSON_C_TO_STRING_NOSLASHESCAPE
            );
            propsJson = legacyPropsStr ? legacyPropsStr : "{}";
            json_object_put(legacyProps);

            fromPropertiesJson(propsJson);
        }

        tinyxml2::XMLElement *fallback = xml_data->FirstChildElement("FallbackHtml");
@@ -579,6 +593,146 @@ protected:
        }
    }

    static bool isLegacyMetaAttribute(const char *key) {
        if (!key)
            return true;
        return std::strcmp(key, "type_id") == 0
            || std::strcmp(key, "instance_id") == 0
            || std::strcmp(key, "script_path") == 0
            || std::strcmp(key, "wrapper_tag") == 0
            || std::strcmp(key, "can_have_children") == 0
            || std::strcmp(key, "name") == 0
            || std::strcmp(key, "author") == 0
            || std::strcmp(key, "version") == 0;
    }

    static bool isTrueString(const char *value) {
        return value
            && (std::strcmp(value, "true") == 0
                || std::strcmp(value, "1") == 0);
    }

    static bool isFalseString(const char *value) {
        return value
            && (std::strcmp(value, "false") == 0
                || std::strcmp(value, "0") == 0);
    }

    static void addLegacyPropValue(json_object *obj, const char *key, const char *value) {
        if (!obj || !key || !value || value[0] == '\0')
            return;

        if (isTrueString(value)) {
            json_object_object_add(obj, key, json_object_new_boolean(true));
            return;
        }
        if (isFalseString(value)) {
            json_object_object_add(obj, key, json_object_new_boolean(false));
            return;
        }

        json_object_object_add(obj, key, json_object_new_string(value));
    }

    static void importLegacyAttributes(tinyxml2::XMLElement *xml_data, json_object *legacyProps) {
        if (!xml_data || !legacyProps)
            return;

        for (const tinyxml2::XMLAttribute *attr = xml_data->FirstAttribute();
             attr;
             attr = attr->Next()) {
            const char *key = attr->Name();
            if (isLegacyMetaAttribute(key))
                continue;
            addLegacyPropValue(legacyProps, key, attr->Value());
        }
    }

    static void importLegacyInlineText(tinyxml2::XMLElement *xml_data, json_object *legacyProps) {
        if (!xml_data || !legacyProps)
            return;

        const char *txt = xml_data->GetText();
        if (!txt || txt[0] == '\0')
            return;

        json_object *existing = nullptr;
        if (json_object_object_get_ex(legacyProps, "content", &existing))
            return;

        json_object_object_add(legacyProps, "content", json_object_new_string(txt));
    }

    static void importLegacyTableCells(tinyxml2::XMLElement *xml_data, json_object *legacyProps) {
        if (!xml_data || !legacyProps)
            return;
        if (std::strcmp(xml_data->Name(), "Table") != 0)
            return;

        struct CellData {
            int row = -1;
            int col = -1;
            std::string value;
        };

        std::vector<CellData> parsedCells;
        int maxRow = -1;
        int maxCol = -1;

        int fallbackRow = 0;
        for (tinyxml2::XMLElement *rowEl = xml_data->FirstChildElement("Row");
             rowEl;
             rowEl = rowEl->NextSiblingElement("Row")) {
            int rowIdx = fallbackRow;
            rowEl->QueryIntAttribute("index", &rowIdx);
            int fallbackCol = 0;

            for (tinyxml2::XMLElement *cellEl = rowEl->FirstChildElement("Cell");
                 cellEl;
                 cellEl = cellEl->NextSiblingElement("Cell")) {
                int colIdx = fallbackCol;
                cellEl->QueryIntAttribute("col", &colIdx);
                const char *cellTxt = cellEl->GetText();
                parsedCells.push_back({rowIdx, colIdx, cellTxt ? cellTxt : ""});
                if (rowIdx > maxRow)
                    maxRow = rowIdx;
                if (colIdx > maxCol)
                    maxCol = colIdx;
                ++fallbackCol;
            }

            ++fallbackRow;
        }

        if (parsedCells.empty())
            return;

        const int rows = maxRow + 1;
        const int cols = maxCol + 1;
        json_object *cells = json_object_new_array();
        for (int r = 0; r < rows; ++r) {
            json_object *rowArr = json_object_new_array();
            for (int c = 0; c < cols; ++c)
                json_object_array_add(rowArr, json_object_new_string(""));
            json_object_array_add(cells, rowArr);
        }

        for (const CellData &entry : parsedCells) {
            if (entry.row < 0 || entry.col < 0 || entry.row >= rows || entry.col >= cols)
                continue;
            json_object *rowArr = json_object_array_get_idx(cells, static_cast<size_t>(entry.row));
            if (!rowArr)
                continue;
            json_object_array_put_idx(rowArr,
                                      static_cast<size_t>(entry.col),
                                      json_object_new_string(entry.value.c_str()));
        }

        json_object_object_add(legacyProps, "cells", cells);
        json_object_object_add(legacyProps, "num_rows", json_object_new_int(rows));
        json_object_object_add(legacyProps, "num_cols", json_object_new_int(cols));
    }

    json_object *parsePropsJsonObject() const {
        json_object *obj = json_tokener_parse(propsJson.c_str());
        if (!obj || !json_object_is_type(obj, json_type_object)) {