util/xmllex.lua

Tue, 04 Jan 2011 00:23:51 +0000

author
Daurnimator <quae@daurnimator.com>
date
Tue, 04 Jan 2011 00:23:51 +0000
changeset 3995
7214dc7a5642
parent 3992
de77ec2b49bc
child 4002
2b53b4b5d46e
permissions
-rw-r--r--

util.xmppstream, util.xmllex: Basic test passes

3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
1 local assert, ipairs , pairs , setmetatable , rawget , rawset , tostring =
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
2 assert, ipairs , pairs , setmetatable , rawget , rawset , tostring
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local strsub = string.sub
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
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
7 local function getstring ( msgs , startpos , finishpos )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
8 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
9 return strsub ( msgs[1] , startpos , finishpos )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 else -- Over multiple source strings
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
11 return strsub ( msgs[1] , startpos , -1 )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
12 .. tblconcat ( msgs , "" , 2 , #msgs - 1 )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
13 .. strsub ( msgs[#msgs] , 1 , finishpos )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local m_mt = {
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 __tostring = function ( v )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
19 local str = v.stringform
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
20 if str then
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
21 return str
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
22 else
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
23 str = getstring ( v.msgs , v.start , v.finish )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
24 v.stringform = str
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
25 return str
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
26 end
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 }
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
30 local function handleoutside ( str , r , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
31 local a , b , close = str:find ( "<(/?)" , initial )
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
32
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
33 if not a then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
34 r.state = "outside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
35 return false
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
38 --Finalise text object
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
39 local m = r[#r]
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
40 m.finish = a - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
41 m.type = "text"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
42
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
43 local m = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
44 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
45 start = a ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
46 starte = b + 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
47 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
48
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
49 if close ~= "/" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
50 r.depth = r.depth + 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
51 m.type = "open"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
52 else
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
53 r.depth = r.depth - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
54 m.type = "close"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
55 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
56
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
57 tblinsert ( r , m )
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 r.state = "inside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
60 return true
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
61 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
62
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
63 local function handleinside ( str, r , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
64 local c , d , selfclosing = str:find ( "(/?)>" , initial )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
65 if not c then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
66 r.state = "inside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
67 return false
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
70 local m = r[#r]
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
71 m.finish = d
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
72 m.finishs = c - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
73 if selfclosing == "/" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
74 m.type = "selfclosing"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
75 r.depth = r.depth - 1
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
76 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
77
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
78 local m = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
79 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
80 start = d + 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
81 type = "text" ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
82 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
83 tblinsert ( r , m )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
84
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
85 r.state = "outside"
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
86 return true
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
87 end
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
89 local function index ( str , r )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
90 r = r or { depth = 0, state = "outside" }
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
91
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
92 local initial = 1
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
93
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
94 if not r[#r] then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
95 r[1] = setmetatable ( {
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
96 msgs = { str } ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
97 type = "text" ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
98 start = 1 ;
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
99 } , m_mt )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
100 else
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
101 tblinsert ( r[#r].msgs , str )
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
102 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
103
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
104 while true do
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
105 if r.state == "outside" then
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
106 if not handleoutside ( str , r , initial ) then
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
107 break
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
108 end
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
109 else
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
110 if not handleinside ( str , r , initial ) then
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
111 break
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
114 initial = r[#r].start
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
115 end
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
116
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 return r
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
120 local function process_starttag ( starttag )
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local str = tostring ( starttag )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
122 local attr = { }
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local elem = str:match ( "[^%s=></]+" )
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 for name , quote, attvalue in str:gmatch ( [=[([^%s=<]+)%s*=%s*(["'])([^"]*)%2]=] ) do
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 attr [ name ] = attvalue
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 return elem , attr
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 end
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
131 local stanza_mt = {
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
132 __index = function ( t , k )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
133 if k == "name" or k == "attr" then
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
134 local elem , attr = process_starttag ( t.opentag )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
135 rawset ( t , "name" , elem )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
136 rawset ( t , "attr" , attr )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
137 return rawget ( t , k )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
138 else
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
139 print("METHOD",k)
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
140 return stanza_methods [ k ]
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
141 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
142 end ;
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
143 __tostring = function ( t )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
144 local opentag = t.opentag
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
145 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
146 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
147 end ;
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
148 }
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
149
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
150 local function new_stanza ( )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
151 return setmetatable ( { tags = { } } , stanza_mt )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
152 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
153
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
154 local function tagindex_to_tree(indices)
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
155 if not start then
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
156 start = 1
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
157 finish = #indices
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
158 end
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
159
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
160 local root = { tags = { } }
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
161 local leaf = root
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
162
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
163 for i = start , finish do
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
164 local v = indices [ i ]
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
165
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
166 if v.type == "selfclosing" then
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
167 local newleaf = new_stanza ( )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
168 newleaf.opentag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
169 newleaf.selfclosing = true
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
170 newleaf.parent = leaf
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
171
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
172 tblinsert ( leaf , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
173 tblinsert ( leaf.tags , newleaf )
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
174 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
175 leaf.endtag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
176 leaf = leaf.parent
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
177 elseif v.type == "text" then
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
178 tblinsert ( leaf, v )
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
179 else -- Open tag
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
180 local newleaf = new_stanza ( )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
181 newleaf.opentag = v
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
182 newleaf.parent = leaf
3992
de77ec2b49bc All correcthg diff util/xmppstream.lua
Matthew Wild <mwild1@gmail.com>
parents: 3991
diff changeset
183
3991
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
184 tblinsert ( leaf , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
185 tblinsert ( leaf.tags , newleaf )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
186
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
187 leaf = newleaf
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
188 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
189 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
190
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
191 assert ( leaf == root , "Mismatched opening/closing tags" )
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
192 return root;
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
193 end
7a2856c8ab7a Tree structure now similar to prosody stanza format
Daurnimator <quae@daurnimator.com>
parents: 3990
diff changeset
194
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 return {
3995
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
196 index = index ;
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
197 tagindex_to_tree = tagindex_to_tree ;
7214dc7a5642 util.xmppstream, util.xmllex: Basic test passes
Daurnimator <quae@daurnimator.com>
parents: 3992
diff changeset
198 process_starttag = process_starttag ;
3990
783004a12224 util.xmllex: Add
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 };

mercurial