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
use std::io;
use std::io::prelude::*;
use std::fmt;
use common;
use name::Name;
use attribute::Attribute;
use escape::escape_str;
use common::XmlVersion;
use namespace::{NamespaceStack, NamespaceIterable, UriMapping};
use writer::config::EmitterConfig;
pub enum EmitterErrorKind {
IoError,
DocumentStartAlreadyEmitted,
UnexpectedEvent,
InvalidWhitespaceEvent
}
pub struct EmitterError {
kind: EmitterErrorKind,
message: &'static str,
cause: Option<io::Error>
}
impl fmt::Debug for EmitterError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "Emitter error: {:?}", self.message));
if self.cause.is_some() {
write!(f, "; caused by: {:?}", *self.cause.as_ref().unwrap())
} else {
Ok(())
}
}
}
pub fn error(kind: EmitterErrorKind, message: &'static str) -> EmitterError {
EmitterError {
kind: kind,
message: message,
cause: None
}
}
#[inline]
fn io_error(err: io::Error) -> EmitterError {
EmitterError { kind: EmitterErrorKind::IoError, message: "Input/output error", cause: Some(err) }
}
pub type EmitterResult<T> = Result<T, EmitterError>;
#[inline]
pub fn io_wrap<T>(result: io::Result<T>) -> EmitterResult<T> {
result.map_err(io_error)
}
pub struct Emitter {
config: EmitterConfig,
nst: NamespaceStack,
indent_level: usize,
indent_stack: Vec<IndentFlags>,
start_document_emitted: bool
}
impl Emitter {
pub fn new(config: EmitterConfig) -> Emitter {
Emitter {
config: config,
nst: NamespaceStack::empty(),
indent_level: 0,
indent_stack: vec!(IndentFlags::empty()),
start_document_emitted: false
}
}
}
macro_rules! io_try(
($e:expr) => (
match $e {
Ok(value) => value,
Err(err) => return Err(io_error(err))
}
)
);
macro_rules! io_chain(
($e:expr) => (io_wrap($e));
($e:expr, $($rest:expr),+) => ({
io_try!($e);
io_chain!($($rest),+)
})
);
macro_rules! wrapped_with(
($_self:ident; $before_name:ident ($arg:expr) and $after_name:ident, $body:expr) => ({
try!($_self.$before_name($arg));
let result = $body;
$_self.$after_name();
result
})
);
macro_rules! if_present(
($opt:ident, $body:expr) => ($opt.map(|$opt| $body).unwrap_or(Ok(())))
);
bitflags!(
flags IndentFlags: u8 {
const WROTE_NOTHING = 0,
const WROTE_MARKUP = 1,
const WROTE_TEXT = 2
}
);
impl Emitter {
#[inline]
pub fn namespace_stack<'a>(&'a self) -> &'a NamespaceStack {
& self.nst
}
#[inline]
fn wrote_text(&self) -> bool {
self.indent_stack.last().unwrap().contains(WROTE_TEXT)
}
#[inline]
fn wrote_markup(&self) -> bool {
self.indent_stack.last().unwrap().contains(WROTE_MARKUP)
}
#[inline]
fn set_wrote_text(&mut self) {
*self.indent_stack.last_mut().unwrap() = WROTE_TEXT;
}
#[inline]
fn set_wrote_markup(&mut self) {
*self.indent_stack.last_mut().unwrap() = WROTE_MARKUP;
}
#[inline]
fn reset_state(&mut self) {
*self.indent_stack.last_mut().unwrap() = WROTE_NOTHING;
}
fn write_newline<W: Write>(&mut self, target: &mut W, level: usize) -> EmitterResult<()> {
io_try!(target.write(self.config.line_separator.as_bytes()));
for _ in (0 .. level) {
io_try!(target.write(self.config.indent_string.as_bytes()));
}
Ok(())
}
fn before_markup<W: Write>(&mut self, target: &mut W) -> EmitterResult<()> {
if !self.wrote_text() && (self.indent_level > 0 || self.wrote_markup()) {
let indent_level = self.indent_level;
try!(self.write_newline(target, indent_level));
if self.indent_level > 0 && self.config.indent_string.len() > 0 {
self.after_markup();
}
}
Ok(())
}
fn after_markup(&mut self) {
self.set_wrote_markup();
}
fn before_start_element<W: Write>(&mut self, target: &mut W) -> EmitterResult<()> {
try!(self.before_markup(target));
self.indent_stack.push(WROTE_NOTHING);
Ok(())
}
fn after_start_element(&mut self) {
self.after_markup();
self.indent_level += 1;
}
fn before_end_element<W: Write>(&mut self, target: &mut W) -> EmitterResult<()> {
if self.indent_level > 0 && self.wrote_markup() && !self.wrote_text() {
let indent_level = self.indent_level;
self.write_newline(target, indent_level - 1)
} else {
Ok(())
}
}
fn after_end_element(&mut self) {
if self.indent_level > 0 {
self.indent_level -= 1;
self.indent_stack.pop();
}
self.set_wrote_markup();
}
fn after_text(&mut self) {
self.set_wrote_text();
}
pub fn emit_start_document<W: Write>(&mut self, target: &mut W,
version: XmlVersion,
encoding: &str,
standalone: Option<bool>) -> EmitterResult<()> {
if self.start_document_emitted {
return Err(error(
EmitterErrorKind::DocumentStartAlreadyEmitted,
"Document start is already emitted"
));
}
self.start_document_emitted = true;
wrapped_with!(self; before_markup(target) and after_markup,
io_chain!(
write!(target, "<?xml version=\"{}\" encoding=\"{}\"", version.to_string(), encoding),
if_present!(standalone,
write!(target, " standalone=\"{}\"",
if standalone { "yes" } else { "no" })),
write!(target, "?>")
)
)
}
fn check_document_started<W: Write>(&mut self, target: &mut W) -> EmitterResult<()> {
if !self.start_document_emitted && self.config.write_document_declaration {
self.emit_start_document(target, common::XmlVersion::Version10, "utf-8", None)
} else {
Ok(())
}
}
pub fn emit_processing_instruction<W: Write>(&mut self,
target: &mut W,
name: &str,
data: Option<&str>) -> EmitterResult<()> {
try!(self.check_document_started(target));
wrapped_with!(self; before_markup(target) and after_markup,
io_chain!(
write!(target, "<?{}", name),
if_present!(data, write!(target, " {}", data)),
write!(target, "?>")
)
)
}
fn emit_start_element_initial<'a, 'b, W, N, I>(&mut self, target: &mut W,
name: Name<'b>,
attributes: &[Attribute],
namespace: &'a N) -> EmitterResult<()>
where W: Write,
N: NamespaceIterable<'a, Iter=I>,
I: Iterator<Item=UriMapping<'a>>
{
try!(self.check_document_started(target));
try!(self.before_start_element(target));
io_try!(write!(target, "<{}", name.to_repr()));
try!(self.emit_namespace_attributes(target, namespace));
self.emit_attributes(target, attributes)
}
pub fn emit_empty_element<'a, 'b, W, N, I>(&mut self, target: &mut W,
name: Name<'b>,
attributes: &[Attribute],
namespace: &'a N) -> EmitterResult<()>
where W: Write,
N: NamespaceIterable<'a, Iter=I>,
I: Iterator<Item=UriMapping<'a>>
{
try!(self.emit_start_element_initial(target, name, attributes, namespace));
io_wrap(write!(target, "/>"))
}
pub fn emit_start_element<'a, 'b, W, N, I>(&mut self, target: &mut W,
name: Name<'b>,
attributes: &[Attribute],
namespace: &'a N) -> EmitterResult<()>
where W: Write,
N: NamespaceIterable<'a, Iter=I>,
I: Iterator<Item=UriMapping<'a>>
{
try!(self.emit_start_element_initial(target, name, attributes, namespace));
io_wrap(write!(target, ">"))
}
pub fn emit_namespace_attributes<'a, W, N, I>(&mut self, target: &mut W,
namespace: &'a N) -> EmitterResult<()>
where W: Write,
N: NamespaceIterable<'a, Iter=I>,
I: Iterator<Item=UriMapping<'a>>
{
for (prefix, uri) in namespace.uri_mappings() {
io_try!(match prefix {
Some("xmlns") | Some("xml") => Ok(()),
Some(prefix) => write!(target, " xmlns:{}=\"{}\"", prefix, uri),
None => if !uri.is_empty() {
write!(target, " xmlns=\"{}\"", uri)
} else { Ok(()) }
});
}
Ok(())
}
pub fn emit_attributes<W: Write>(&mut self, target: &mut W,
attributes: &[Attribute]) -> EmitterResult<()> {
for attr in attributes.iter() {
io_try!(write!(target, " {}=\"{}\"", attr.name.to_repr(), escape_str(attr.value)))
}
Ok(())
}
pub fn emit_end_element<W: Write>(&mut self, target: &mut W,
name: Name) -> EmitterResult<()> {
wrapped_with!(self; before_end_element(target) and after_end_element,
io_wrap(write!(target, "</{}>", name.to_repr()))
)
}
pub fn emit_cdata<W: Write>(&mut self, target: &mut W, content: &str) -> EmitterResult<()> {
if self.config.cdata_to_characters {
self.emit_characters(target, content)
} else {
io_try!(target.write(b"<![CDATA["));
io_try!(target.write(content.as_bytes()));
io_try!(target.write(b"]]>"));
self.after_text();
Ok(())
}
}
pub fn emit_characters<W: Write>(&mut self, target: &mut W,
content: &str) -> EmitterResult<()> {
io_try!(target.write(escape_str(content).as_bytes()));
self.after_text();
Ok(())
}
pub fn emit_comment<W: Write>(&mut self, target: &mut W, content: &str) -> EmitterResult<()> {
Ok(())
}
}