util/xmllex.lua

Wed, 05 Jan 2011 05:14:02 +0000

author
daurnimator <quae@daurnimator.com>
date
Wed, 05 Jan 2011 05:14:02 +0000
changeset 4002
2b53b4b5d46e
parent 3995
7214dc7a5642
child 4003
cb6ddda1cb5f
permissions
-rw-r--r--

util.xmllex, util.xmppstream: It runs

4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
1 local assert , ipairs , pairs , setmetatable , rawget , rawset , tostring =
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
2 assert , ipairs , pairs , setmetatable , rawget , rawset , tostring
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
3 local strsub , strmatch = string.sub , string.match
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local tblconcat = table.concat
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
5 local tblinsert = table.insert
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
7 local stanza_methods = require "util.stanza".stanza_mt;
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
8
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
9 local function getstring ( msgs , startpos , finishpos )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
10 if #msgs == 1 then --All originated in same string
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
11 return strsub ( msgs[1] , startpos , finishpos )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 else -- Over multiple source strings
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
13 return strsub ( msgs[1] , startpos , -1 )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
14 .. tblconcat ( msgs , "" , 2 , #msgs - 1 )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
15 .. strsub ( msgs[#msgs] , 1 , finishpos )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local m_mt = {
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 __tostring = function ( v )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
21 local str = v.stringform
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
22 if str then
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
23 return str
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
24 else
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
25 str = getstring ( v.msgs , v.start , v.finish )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
26 v.stringform = str
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
27 return str
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
28 end
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 }
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
32 local function handleoutside ( str , r , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
33 local a , b , close = str:find ( "<(/?)" , initial )
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
34
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
35 if not a then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
36 r.state = "outside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
37 return false
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
40 --Finalise text object
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
41 local m = r[#r]
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
42 m.finish = a - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
43 m.type = "text"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
44
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
45 local m = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
46 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
47 start = a ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
48 starte = b + 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
49 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
50
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
51 if close ~= "/" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
52 r.depth = r.depth + 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
53 m.type = "open"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
54 else
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
55 r.depth = r.depth - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
56 m.type = "close"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
57 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
58
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
59 tblinsert ( r , m )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
60
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
61 r.state = "inside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
62 return true
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
63 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
64
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
65 local function handleinside ( str, r , initial )
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
66 local c , d , selfclosing = str:find ( "([/?]?)>" , initial )
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
67 if not c then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
68 r.state = "inside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
69 return false
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
72 local m = r[#r]
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
73 m.finish = d
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
74 m.finishs = c - 1
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
75 if selfclosing == "/" or selfclosing == "?" then
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
76 m.type = "selfclosing"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
77 r.depth = r.depth - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
78 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
79
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
80 local m = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
81 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
82 start = d + 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
83 type = "text" ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
84 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
85 tblinsert ( r , m )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
86
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
87 r.state = "outside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
88 return true
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
89 end
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
91 local function index ( str , r )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
92 r = r or { depth = 0, state = "outside" }
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
93
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
94 local initial = 1
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
95
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
96 if not r[#r] then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
97 r[1] = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
98 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
99 type = "text" ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
100 start = 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
101 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
102 else
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
103 tblinsert ( r[#r].msgs , str )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
104 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
105
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
106 while true do
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
107 if r.state == "outside" then
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
108 if not handleoutside ( str , r , initial ) then
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
109 break
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
110 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
111 else
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
112 if not handleinside ( str , r , initial ) then
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
113 break
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 end
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
116 initial = r[#r].start
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
117 end
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
118
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 return r
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
122 local function get_name ( str )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
123 return strmatch ( str , "^<([^%s>/]+)" )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
124 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
125
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
126 local function get_attr ( str )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
127 local attr = { }
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
128 for name , quote, attvalue in str:gmatch ( [=[([^%s=/<]+)%s*=%s*(["'])([^'"]*)%2]=] ) do
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 attr [ name ] = attvalue
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
131 return attr
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
132 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
133
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
134 function resolve_attr_namespaces ( attr )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
135 local namespace = { }
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
136 local prefixattr = { }
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
137 for k , attr_value in pairs ( attr ) do
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
138 local attr_prefix , attr_name = k:match ( "^([^:\1]+):?([^\1]-)$" )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
139
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
140 if attr_prefix == nil then
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
141 error ( "already resolved" )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
142 elseif attr_prefix == "xmlns" then
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
143 namespace [ attr_name ] = attr_value
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
144 elseif #attr_name ~= 0 and attr_prefix ~= "xml" then
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
145 local t = prefixattr [ attr_prefix ]
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
146 if not t then
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
147 t = { }
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
148 prefixattr [ attr_prefix ] = t
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
149 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
150 t [ attr_name ] = attr_value
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
151 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
152 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
153
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
154 for k , v in pairs ( prefixattr ) do
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
155 for name , value in pairs ( v ) do
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
156 attr [ namespace [ k ] .. "\1" .. name ] = value
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
157 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
158 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
159 return namespace
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
162 local currentindex = 1
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
163
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
164 local function resolve_namespace ( element )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
165 local parent = element.parent
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
166
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
167 local prefix = get_name ( element.str ):match ( "^([^:]+):" )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
168
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
169 local namespace = setmetatable ( resolve_attr_namespaces ( element.attr ) , { __index = parent.namespace } )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
170
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
171 local current_namespace = prefix and ( namespace [ prefix ] or error ("unbound prefix: "..prefix) )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
172 or rawget(element.attr, "xmlns")
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
173 or parent.attr.xmlns
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
174 or parent.namespace [ currentindex ]
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
175 namespace [ currentindex ] = current_namespace
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
176
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
177 return namespace
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
178 end
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
179
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
180 local dynamic_properties = {
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
181 name = function ( t )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
182 return get_name ( t.str ):match("[^:]+$")
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
183 end;
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
184 attr = function ( t , k )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
185 return setmetatable ( get_attr ( t.str ) , { __index = function ( attr_table , attr )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
186 local _ = t.namespace -- DO NOT OPTIMISE AWAY WAQAS
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
187 setmetatable ( attr_table , { __index = { xmlns = t.namespace[currentindex] } } )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
188 return attr_table [ attr ]
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
189 end } )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
190 end;
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
191 str = function ( t , k )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
192 return tostring ( t.opentag )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
193 end;
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
194 namespace = resolve_namespace ;
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
195 }
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
196
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
197 local stanza_mt = {
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
198 __index = function ( t , k )
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
199 local f = dynamic_properties [ k ]
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
200 if f then
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
201 local v = f ( t )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
202
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
203 rawset ( t , k , v )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
204 return v
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
205 else
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
206 return stanza_methods[k]
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
207 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
208 end ;
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
209
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
210 __tostring = function ( t )
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
211 if t.modified then
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
212 return stanza_methods.__tostring ( t )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
213 end
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
214 local opentag = t.opentag
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
215 local endtag = assert ( rawget ( t , "endtag" ) or rawget ( t , "selfclosing" ) and t.opentag )
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
216 return getstring ( opentag.msgs , opentag.start , endtag.finish )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
217 end ;
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
218
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
219 __newindex = function ( t , k , v )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
220 rawset ( t , "modified", true )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
221 rawset ( t , k , v )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
222 end ;
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
223 }
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
224
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
225 local function new_stanza ( parent )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
226 return setmetatable ( { tags = { } , parent = parent } , stanza_mt )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
227 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
228
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
229 local function tagindex_to_tree(indices, start, finish,root)
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
230 if not start then
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
231 start = 1
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
232 finish = #indices
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
233 end
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
234
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
235 root = root or { attr = { } }
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
236 root.namespace = resolve_attr_namespaces ( root.attr )
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
237 root.root = true
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
238 root.tags = { }
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
239 local leaf = root
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
240
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
241 for i = start , finish do
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
242 local v = indices [ i ]
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
243
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
244 if v.type == "selfclosing" then
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
245 local newleaf = new_stanza ( leaf )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
246 newleaf.opentag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
247 newleaf.selfclosing = true
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
248
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
249 tblinsert ( leaf , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
250 tblinsert ( leaf.tags , newleaf )
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
251 elseif v.type == "close" then -- Close tag
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
252 leaf.endtag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
253 leaf = leaf.parent
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
254 elseif v.type == "text" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
255 tblinsert ( leaf, v )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
256 else -- Open tag
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
257 local newleaf = new_stanza ( leaf )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
258 newleaf.opentag = v
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
259
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
260 tblinsert ( leaf , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
261 tblinsert ( leaf.tags , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
262
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
263 leaf = newleaf
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
264 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
265 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
266
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
267 assert ( leaf == root , "Mismatched opening/closing tags" )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
268 return root;
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
269 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
270
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 return {
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
272 index = index ;
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
273 tagindex_to_tree = tagindex_to_tree ;
4002
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
274 get_name = get_name ;
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
275 get_attr = get_attr ;
2b53b4b5d46e util.xmllex, util.xmppstream: It runs
daurnimator <quae@daurnimator.com>
parents: 3995
diff changeset
276 resolve_attr_namespaces = resolve_attr_namespaces ;
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 };

mercurial