File: xmlparser.lua

package info (click to toggle)
swupdate 2025.12%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,004 kB
  • sloc: ansic: 66,621; python: 6,291; makefile: 791; sh: 538; javascript: 229
file content (128 lines) | stat: -rw-r--r-- 2,813 bytes parent folder | download | duplicates (5)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
-- SPDX-FileCopyrightText: 2014-2021 Stefano Babic <stefano.babic@swupdate.org>
--
-- SPDX-License-Identifier: CC0-1.0
--
----------------------------------------------
-- Test XML parsing with Lua
----------------------------------------------

lxp = require "lxp"
lxp.lom = require "lxp.lom"

function XMLSWDescParse(name)
	io.input(name)
	s = io.read("*all")
	return lxp.lom.parse(s)
end

function find_by_attr(tab, tag, attr, value)
      	for i = 1, #tab do
		if (type(tab[i]) == "table") then
			if (tab[i]["tag"] == tag and tab[i]["attr"][attr] == value) then
				return tab[i]
			end
		end
	end
end

function node_by_path(tab, p)
	t = string.find(p, "/")
	if (t == 1) then
		return node_by_path(tab, string.sub(p, t + 1))
	end

	if (t) then
		tag = string.sub(p, 1, t - 1)
	else
		tag = p
	end

	if (tab["tag"] == tag) then
		return node_by_path(tab, string.sub(p, t+1))
	end

      	for i = 1, #tab do
		if (type(tab[i]) == "table") then
			if (tab[i]["tag"] == tag) then
				if (t == nil) then
					return(tab[i])
				else
					return node_by_path(tab[i], string.sub(p, t+1),
						       	attrname, value)
				end
			end
		end
	end

	print ("Not found "..tag)
	return nil
end

function stream_by_device(tab, device)

	if (tab == nil or tab["tag"] == nil) then
		return nil
	end

      	for i = 1, #tab do
		if (type(tab[i]) == "table") then
			if (tab[i]["attr"]["device"] == device) then
				return tab[i]
			end
		end
	end

	return nil

end


function xmlparser(name, device)
	local tab = XMLSWDescParse(name)

	images = node_by_path(tab, "software/images")

	if (images == nil) then
		print("No image found")
		return nil
	end

	streams = stream_by_device(images, device)
	local count = 0
	img = {}
	if (streams) then
		for i = 1, #streams do
			if (streams[i] and streams[i]["tag"] == "stream") then
				print ("Name :"..streams[i]["attr"]["name"].." type:"..
					streams[i]["attr"]["type"])
				count = count + 1
				img[count] = {}
				img[count]["name"] = streams[i]["attr"]["name"]
				img[count]["type"] = streams[i]["attr"]["type"]
				--- check for all types that are scripts, for now only Lua scripts
				if (streams[i]["attr"]["type"] == "lua") then
					img[count]["script"] = 1
				end
				if (streams[i]["attr"]["volume"]) then
					img[count]["volume"] = streams[i]["attr"]["volume"]
				end
				if (streams[i]["attr"]["mtdname"]) then
					img[count]["mtdname"] = streams[i]["attr"]["mtdname"]
				end
				if (streams[i]["attr"]["dest"]) then
					img[count]["dest"] = streams[i]["attr"]["dest"]
				end
				if (streams[i]["attr"]["device_id"]) then
					img[count]["device_id"] = streams[i]["attr"]["device_id"]
				end

			end
		end
	end

	name = node_by_path(tab, "software/name")[1]
	version = tab["attr"]["version"]

	print (name..version)
	return name, version, count, img
end