-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_schema.lua
More file actions
86 lines (77 loc) · 2.38 KB
/
generate_schema.lua
File metadata and controls
86 lines (77 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
local xmlua = require("lua_schema.xmlua");
local mhf = require("schema_processor")
local xsd = xmlua.XSD.new();
local basic_stuff = require("lua_schema.basic_stuff");
local stringx = require("pl.stringx");
local str_mappings = '';
_G.handler_cache = {};
local function generate_mappings(v)
local formatted_path = basic_stuff.package_name_from_uri(v.ns).."."..v.name;
local local_path_parts = stringx.split(formatted_path,".");
local i = 1;
local local_path = '';
while(i <= #local_path_parts) do
if(local_path == '') then
local_path = local_path_parts[i];
else
local_path = local_path.."/"..local_path_parts[i];
end
i = i + 1;
end
local_path = local_path..".lua";
local local_pth = local_path:gsub("build/","");
if(str_mappings ~= '') then
str_mappings = str_mappings..",\n"..'\t["'..formatted_path..'"]'..' = '..'"'..local_pth..'"';
else
str_mappings = '\t["'..formatted_path..'"]'..' = '..'"'..local_pth..'"';
end
end
local function generate_schema_for_typedef(typedef)
print(debug.getinfo(1).source, debug.getinfo(1).currentline, typedef.name);
require 'pl.pretty'.dump(typedef);
mhf:generate_lua_schema_from_typedef(typedef);
end
local function generate_schema_for_element(element)
print(debug.getinfo(1).source, debug.getinfo(1).currentline);
require 'pl.pretty'.dump(element);
mhf:generate_lua_schema_from_element(element);
end
-- Main()
--
--if (#arg ~= 1) then
-- error("Usage geneate_schema <xsd_file>");
-- os.exit(-1);
--end
--
local xsd_name = arg[1];
local schema = xsd:parse(xsd_name);
local build_mode = (arg[2] ~= nil and arg[2] ~= "0");
local types = schema:get_type_defs();
if (types ~= nil) then
for _, v in ipairs(types) do
generate_schema_for_typedef(v);
generate_mappings(v);
end
end
local elems = schema:get_element_decls();
if (elems ~= nil) then
for _, v in ipairs(elems) do
generate_schema_for_element(v);
generate_mappings(v);
end
end
do
local header = "local build_mappings = {\n"
local footer = '\n}'.."\n\nreturn build_mappings;"
str_mappings = header..str_mappings..footer;
xsd_file = xsd_name:gsub("%.%.","");
xsd_file = xsd_file:gsub("xsd/","");
if (build_mode) then
os.execute("mkdir -p output_files/xsd")
local target_file_path = "output_files/xsd/"..xsd_file:gsub(".xsd$","").."_xsd.lua";
local file = io.open(target_file_path,"w+");
file:write(str_mappings)
file:close();
end
end
_G.handler_cache = nil;