Page 1 of 1

mod XML lua

Posted: Mon Nov 10, 2008 6:07 am
by zer0
Hey Elverion,

I've been using the mod XML Parser as u know, I ran into a parser error, which had me stumped for a bit, so I decided to add error handling for the parse() call in the open function, here it is:

Code: Select all

function open(filename)
	callbacks = {
		StartElement = startElement,
		EndElement = endElement,
		CharacterData = characterData,
		_nonstrict = true,
		stack = {{}}}

	local p = lxp.new(callbacks);
	local file = io.open(filename);

	if( file == nil ) then
		local err = string.format("Cannot open file \'%s\' for reading.", filename);
		error(err, 0);
	end

        local parse = {}
	for l in file:lines() do
        parse.result, parse.msg, parse.line, parse.col, parse.pos = p:parse(l)
        if (parse.result) then
        else
            error("XML Parse Error." .. 
                    "\nfile: " .. filename ..
                    "\nline: " .. parse.line ..
                    "\ncolumn: " .. parse.col ..
                    "\nabsolute postion: " .. parse.pos ..
                    "\nmessage: " .. parse.msg)            
        end
		p:parse("\n");
	end
	p:parse();
	p:close();
	file:close();

	return callbacks.stack[1][1];
end
If you could add this to the XML file that would be cool. ;)

Re: mod XML lua

Posted: Mon Nov 10, 2008 2:27 pm
by Administrator
I've saved this for now, but won't be able to test it. Kind of surprised that I never thought of adding that in while I was writing the function. Well, thanks for the addition. I might change it a bit so that the error message follows the same format as other error messages, but it will definitely be added.

Re: mod XML lua

Posted: Tue Nov 11, 2008 12:07 am
by zer0
It was only because I had modified the XML and broken it, that's why I added it.

It gave me a chance to look at the code a bit, so I'm more than happy to help. Over the holiday season, I might start taking a look at the Micromacro source, and familiarising myself with the code base.

Re: mod XML lua

Posted: Tue Nov 11, 2008 2:04 am
by Administrator
I'm actually trying to finalize the changes for 0.99 right now. I've already begun re-writing the whole thing for 1.0. So far, things are going well. Unless I come across some problems, the code for 1.0 should be much cleaner and easier to maintain. As things are now, it's a mess.

Re: mod XML lua

Posted: Tue Nov 11, 2008 6:48 am
by zer0
Yeah I understand, ZS Shaiya is up to about 3500 lines of code now, and there is quite a bit of cleaning up, error handling and optimisation to do (it's all a learning experience for me).

Hey I found that I need to parse XML data and convert a value or an attribute from a string to a number or boolean data type.
There is both an implicit version, and an explicit one.
You may find it useful, maybe a way to incorporate it into the XML mod, I dunno, anyway here they are.

Code: Select all

-- explicit string conversion to another basic data type.
-- currently only number and boolean types supported.
-- @param data The data you want to convert.
-- @param data_type The basic data type you want to change it too.
-- @return The converted data
function stringConvert(data, data_type)
    converts = {}
    converts["boolean"] = function()
        if (data == "true") then
            return true
        elseif (data == "false") then
            return false
        else
            return nil
        end
    end
    converts["number"] = function()
        print("data: " .. data)
        local l_res = string.find(data, "[^%.?0-9+]")
        if (l_res == nil) then
            print("number")
            return tonumber(data)
        else
            print("not number")
            return nil
        end    
    end
    print("data: " .. data)
    print("data_type: " .. data_type)
    return converts[data_type]()
end

-- implicit string conversion to another basic data type.
-- currently only number and boolean types supported.
-- @param data The data you want to convert.
-- @return The converted data.
function string_convert(data)
    print("data: " .. data)
    if (data == "true") then
        return true
    elseif (data == "false") then
        return false
    elseif(string.find(data, "[^%.?0-9+]") == nil) then
        return tonumber(data)
    else
        return data
    end
end

Re: mod XML lua

Posted: Tue Nov 11, 2008 6:24 pm
by Administrator
Yeah, I guess that could be useful. I'll go ahead and add a condensed version of that to the XML module.

I've attached the update.