Air code Issue

After 15min, i’ve understand why i try doesn’t work… air code remove some parts of my code in the file xD
Can you try, for watch if the problem is just for me or if this is a issue.

XML = class()

XML.templates = {}
XML.templates.header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
XML.patterns = {}
XML.patterns.header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"

-- This class is very simple to use :

--     - If you have an object, and you want to export as xml :
--         tostring(XML(obj, nameOfObj))
        
--     - If you have an xml, and you want to remake table :
--         XML(obj).racine
        
--     This doesn't support namespace.
--     This doesn't support attribute.
        

function XML:init(obj, name)
    -- make xml from tab
    -- or make tab from xml
    local xml,tab
    
    if type(obj) == "table" then -- mode to xml
        tab = obj
        assert(name, "IllegalArgumentException: @Param (2) name is nil")
    elseif type(obj) == "string" then -- mode to tab
        xml = obj
        local _i, _j = string.find(xml, XML.patterns.header)
        if _i and _j then
            xml = string.sub(xml, _j+1)
            xml = xml:gsub(">[\\s\\b\\t\\r\
]*<", "><")
            xml = xml:gsub("^%s*(.-)%s*$", "%1")
            --print(xml)
        end
    else
        error("IllegalArgumentException: @Param (1) obj must be a table or a string")
    end
    
    if xml then
        local make
        make = function(xml)
            -- print(xml)
            if xml and string.len(xml) == 0 then return nil end -- empty element
        
            local xmltab = {}
            local _begin = nil
            local _end = nil
        
            _begin, _end = string.find(xml, "<[^>]*>", _end)
            if (_begin == nil or _end == nil) then
                return xml
            end
        
            local nameOfElement = string.sub(xml, _begin + 1, _end - 1)
            -- print("nameOfElement :", nameOfElement)
            
            --
            local nbSubElementsWithSameName = 0
            local tst = true
            local _begin2, _end2 = nil, _end
            while tst do
                _begin2, _end2 = string.find(xml, "<" .. nameOfElement .. ">", _end2)
                if (_begin2 == nil or _end2 == nil) then
                    tst = false
                else
                    nbSubElementsWithSameName = nbSubElementsWithSameName + 1
                end
            end
            -- print("nbSubElementsWithSameName", nbSubElementsWithSameName)
            --
            
            local _begin2, _end2 = string.find(xml, "" , _end + 1)
            for i = 1, nbSubElementsWithSameName do
                _begin2, _end2 = string.find(xml, "" , _end2 + 1)
            end
            assert(_begin2 and _end2, "Xml malformed : " .. nameOfElement)
            
            local valueOfElement = string.sub(xml, _end + 1, _begin2 - 1)
            xmltab[nameOfElement] = make(valueOfElement)
        
            if (_end2 ~= string.len(xml)) then
                for k, v in pairs(make(string.sub(xml, _end2 + 1))) do
                    xmltab[k] = v
                end
            end
            
            return xmltab
        end
        self.racine = make(xml)
    elseif tab then
        local toXml
        toXml = function(tab, level, name)
            level = level or 0
            local str = ""
            if name then
                str = "<" .. name .. ">\
"
                level = level + 1
            end
            for k,v in pairs(tab) do
                for i = 1, level do
                    str = str .. "\\t"
                end
                if type(v) == "table" then
                    str = str .. "<" .. k .. ">\
"
                    str = str .. toXml(v, level + 1)
                    for i = 1, level do
                        str = str .. "\\t"
                    end
                    str = str .. "\
"
                else
                    str = str .. "<" .. k .. ">" .. tostring(v) .. "\
"
                end
            end
            if name then
                str = str .. "\
"
            end
            return str
        end
        self.xml = toXml(tab, 0, name)
    end
end

function XML:copyIn(obj)
    for k, v in pairs(self.racine) do
        obj[k] = v
    end
end

function XML.fileToString(location)
    local rFd = io.open(location,"r")
    local str = ""
    while 1 do
        local data = rFd:read()
        if data == nil then
            rFd:close()
            break
        end
        str = str .. data
    end
    return str
end

function XML:__tostring()
    local toXmlString
    toXmlString = function(tab, level)
        level = level or 0
        local str = ""
        for k,v in pairs(tab) do
            for i = 1, level do
                str = str .. "\\t"
            end
            if type(v) == "table" then
                str = str .. k .. ":\
" .. toXmlString(v, level + 1)
            else
                str = str .. k .. ": " .. tostring(v) .. "\
"
            end
        end
        return str
    end
    if self.racine then
        return toXmlString(self.racine)
    elseif self.xml then
        return XML.templates.header .. "\
" .. self.xml
    end
end

-- XML

function setup()
    local test = "" .. 
    "" ..
        "Takeshi Yamamoto" ..
        "<1><1>300" ..
    ""
    local result = XML(test)
    print(result)
    print(XML(result.racine.root, "root"))
end

function draw()
    background(40, 40, 50)
end

```


Thanks before !

I’m not near a computer to test aircode but very cool class :slight_smile: Looking forward to the finished library

@HyroVitalyProtago thanks for reporting this bug, I’ve fixed it in the next version of Codea.

@Simeon no problem :wink: Thank you for Air Code !