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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
mod actions;
mod rules;
mod types;
pub mod interface;
use std::borrow::{Cow};
use std::borrow::Cow::Borrowed;
use std::collections::{VecDeque, BTreeMap, HashSet};
use std::collections::btree_map::{Iter};
use std::result::Result;
use std::mem;
use {Prefix, Namespace, LocalName};
use tokenizer::{self, TokenSink, Tag, QName, Attribute, StartTag};
pub use self::interface::{TreeSink, Tracer, NextParserState, NodeOrText};
use self::rules::XmlTreeBuilderStep;
use self::types::*;
static XML_URI: &'static str = "http://www.w3.org/XML/1998/namespace";
static XMLNS_URI: &'static str = "http://www.w3.org/2000/xmlns/";
type InsResult = Result<(), Cow<'static, str>>;
#[derive(Debug)]
struct NamespaceMapStack(Vec<NamespaceMap>);
impl NamespaceMapStack{
fn new() -> NamespaceMapStack {
NamespaceMapStack({
let mut vec = Vec::new();
vec.push(NamespaceMap::default());
vec
})
}
fn push(&mut self, map: NamespaceMap) {
self.0.push(map);
}
#[doc(hidden)]
pub fn pop(&mut self) {
self.0.pop();
}
}
#[derive(Debug)]
#[doc(hidden)]
pub struct NamespaceMap {
scope: BTreeMap<Prefix, Option<Namespace>>,
}
impl NamespaceMap {
#[doc(hidden)]
pub fn empty() -> NamespaceMap {
NamespaceMap {
scope: BTreeMap::new(),
}
}
fn default() -> NamespaceMap {
NamespaceMap {
scope: {
let mut map = BTreeMap::new();
map.insert(namespace_prefix!(""), None);
map.insert(namespace_prefix!("xml"), Some(ns!(xml)));
map.insert(namespace_prefix!("xmlns"), Some(ns!(xmlns)));
map
},
}
}
#[doc(hidden)]
pub fn get(&self, prefix: &Prefix) -> Option<&Option<Namespace>> {
self.scope.get(prefix)
}
#[doc(hidden)]
pub fn get_scope_iter(&self) -> Iter<Prefix, Option<Namespace>> {
self.scope.iter()
}
#[doc(hidden)]
pub fn insert(&mut self, name: &QName) {
let prefix = Prefix::from(&*name.prefix);
let namespace = Some(Namespace::from(&*name.namespace_url));
self.scope.insert(prefix, namespace);
}
fn insert_ns(&mut self, attr: &Attribute) -> InsResult {
if &*attr.value == XMLNS_URI {
return Err(Borrowed("Can't declare XMLNS URI"));
};
let opt_uri = if attr.value.is_empty() {
None
} else {
Some(Namespace::from(&*attr.value))
};
let result = match (&*attr.name.prefix, &*attr.name.local) {
("xmlns", "xml") => {
if &*attr.value != XML_URI {
Err(Borrowed("XML namespace can't be redeclared"))
} else {
Ok(())
}
},
("xmlns" , "xmlns") => {
Err(Borrowed("XMLNS namespaces can't be changed"))
},
("xmlns", _)
| ("", "xmlns")=> {
let ext = if attr.name.prefix.is_empty() {
namespace_prefix!("")
} else {
Prefix::from(&*attr.name.local)
};
if self.scope.contains_key(&ext) && opt_uri.is_some() {
Err(Borrowed("Namespace already defined"))
} else {
self.scope.insert(ext, opt_uri);
Ok(())
}
},
(_, _) => {
Err(Borrowed("Invalid namespace declaration."))
}
};
result
}
}
#[derive(Copy, Clone)]
pub struct XmlTreeBuilderOpts {
pub exact_errors: bool,
pub profile: bool,
}
impl Default for XmlTreeBuilderOpts {
fn default() -> XmlTreeBuilderOpts {
XmlTreeBuilderOpts{
exact_errors: false,
profile: false,
}
}
}
pub struct XmlTreeBuilder<Handle, Sink> {
opts: XmlTreeBuilderOpts,
sink: Sink,
doc_handle: Handle,
next_tokenizer_state: Option<tokenizer::states::XmlState>,
open_elems: Vec<Handle>,
curr_elem: Option<Handle>,
namespace_stack: NamespaceMapStack,
current_namespace: NamespaceMap,
present_attrs: HashSet<(Namespace, LocalName)>,
phase: XmlPhase,
}
impl<Handle, Sink> XmlTreeBuilder<Handle, Sink>
where Handle: Clone,
Sink: TreeSink<Handle=Handle>,
{
pub fn new(mut sink: Sink, opts: XmlTreeBuilderOpts) -> XmlTreeBuilder<Handle, Sink> {
let doc_handle = sink.get_document();
XmlTreeBuilder {
opts: opts,
sink: sink,
doc_handle: doc_handle,
next_tokenizer_state: None,
open_elems: vec!(),
curr_elem: None,
namespace_stack: NamespaceMapStack::new(),
current_namespace: NamespaceMap::empty(),
present_attrs: HashSet::new(),
phase: StartPhase,
}
}
pub fn unwrap(self) -> Sink {
self.sink
}
pub fn sink<'a>(&'a self) -> &'a Sink {
&self.sink
}
pub fn sink_mut<'a>(&'a mut self) -> &'a mut Sink {
&mut self.sink
}
pub fn trace_handles(&self, tracer: &Tracer<Handle=Handle>) {
tracer.trace_handle(self.doc_handle.clone());
for e in self.open_elems.iter() {
tracer.trace_handle(e.clone());
}
self.curr_elem.as_ref().map(|h| tracer.trace_handle(h.clone()));
}
#[cfg(not(for_c))]
#[allow(dead_code)]
fn dump_state(&self, label: String) {
debug!("dump_state on {}", label);
debug!(" open_elems:");
for node in self.open_elems.iter() {
let QName { prefix, local, .. } = self.sink.elem_name(node);
debug!(" {:?}:{:?}", prefix,local);
}
debug!("");
}
#[cfg(for_c)]
fn debug_step(&self, _mode: XmlPhase, _token: &Token) {
}
#[cfg(not(for_c))]
fn debug_step(&self, mode: XmlPhase, token: &Token) {
debug!("processing {:?} in insertion mode {:?}", format!("{:?}", token), mode);
}
fn declare_ns(&mut self, attr: &mut Attribute) {
if let Err(msg) = self.current_namespace.insert_ns(&attr) {
self.sink.parse_error(msg);
} else {
attr.name.namespace_url = ns!(xmlns);
}
}
fn find_uri(&self, prefix: &Prefix) -> Result<Option<Namespace>, Cow<'static, str> >{
let mut uri = Err(Borrowed("No appropriate namespace found"));
for ns in self.namespace_stack.0.iter()
.chain(Some(&self.current_namespace)).rev() {
if let Some(el) = ns.get(prefix) {
uri = Ok(el.clone());
break;
}
}
uri
}
fn bind_qname(&mut self, name: &mut QName) {
match self.find_uri(&name.prefix) {
Ok(uri) => {
let ns_uri = match uri {
Some(e) => e,
None => ns!(),
};
name.namespace_url = ns_uri;
},
Err(msg) => {
self.sink.parse_error(msg);
},
}
}
fn bind_attr_qname(&mut self, name: &mut QName) -> bool {
let mut not_duplicate = true;
if !name.prefix.is_empty() {
self.bind_qname(name);
not_duplicate = self.check_duplicate_attr(name);
}
not_duplicate
}
fn check_duplicate_attr(&mut self, name: &QName) -> bool {
let pair = (name.namespace_url.clone(), name.local.clone());
if self.present_attrs.contains(&pair) {
return false;
}
self.present_attrs.insert(pair);
true
}
fn process_namespaces(&mut self, tag: &mut Tag) {
let mut new_attr = vec![];
for mut attr in tag.attrs.iter_mut()
.filter(|attr| attr.name.prefix == namespace_prefix!("xmlns")
|| attr.name.local == local_name!("xmlns")) {
self.declare_ns(&mut attr);
}
for mut attr in tag.attrs.iter_mut()
.filter(|attr| attr.name.prefix != namespace_prefix!("xmlns")
&& attr.name.local != local_name!("xmlns")) {
if self.bind_attr_qname(&mut attr.name) {
new_attr.push(attr.clone());
}
}
mem::replace(&mut tag.attrs, new_attr);
self.bind_qname(&mut tag.name);
let x = mem::replace(&mut self.current_namespace, NamespaceMap::empty());
if tag.kind == StartTag {
self.namespace_stack.push(x);
}
}
fn process_to_completion(&mut self, mut token: Token) {
let mut more_tokens = VecDeque::new();
loop {
let phase = self.phase;
match self.step(phase, token) {
Done => {
token = unwrap_or_return!(more_tokens.pop_front(), ());
}
Reprocess(m, t) => {
self.phase = m;
token = t;
}
}
}
}
}
impl<Handle, Sink> TokenSink
for XmlTreeBuilder<Handle, Sink>
where Handle: Clone,
Sink: TreeSink<Handle=Handle>,
{
fn process_token(&mut self, token: tokenizer::Token) {
let token = match token {
tokenizer::ParseError(e) => {
self.sink.parse_error(e);
return;
}
tokenizer::DoctypeToken(d) =>DoctypeToken(d),
tokenizer::PIToken(x) => PIToken(x),
tokenizer::TagToken(x) => TagToken(x),
tokenizer::CommentToken(x) => CommentToken(x),
tokenizer::NullCharacterToken => NullCharacterToken,
tokenizer::EOFToken => EOFToken,
tokenizer::CharacterTokens(x) => CharacterTokens(x),
};
self.process_to_completion(token);
}
fn query_state_change(&mut self) -> Option<tokenizer::states::XmlState> {
self.next_tokenizer_state.take()
}
}