1
1
use std:: io;
2
2
use std:: iter;
3
+ use std:: fmt;
3
4
4
5
use common;
5
6
use common:: { XmlVersion , Attribute , Name , escape_str} ;
@@ -20,6 +21,17 @@ pub struct EmitterError {
20
21
cause : Option < io:: IoError >
21
22
}
22
23
24
+ impl fmt:: Show for EmitterError {
25
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
26
+ try!( write ! ( f, "Emitter error: {}" , self . message) ) ;
27
+ if self . cause . is_some ( ) {
28
+ write ! ( f, "; caused by: {}" , * self . cause. get_ref( ) )
29
+ } else {
30
+ Ok ( ( ) )
31
+ }
32
+ }
33
+ }
34
+
23
35
pub fn error ( kind : EmitterErrorKind , message : & ' static str ) -> EmitterError {
24
36
EmitterError {
25
37
kind : kind,
@@ -51,16 +63,18 @@ pub struct Emitter {
51
63
start_document_emitted : bool
52
64
}
53
65
54
- pub fn new ( config : EmitterConfig ) -> Emitter {
55
- Emitter {
56
- config : config,
66
+ impl Emitter {
67
+ pub fn new ( config : EmitterConfig ) -> Emitter {
68
+ Emitter {
69
+ config : config,
57
70
58
- nst : NamespaceStack :: empty ( ) ,
71
+ nst : NamespaceStack :: empty ( ) ,
59
72
60
- indent_level : 0 ,
61
- indent_stack : vec ! ( ) ,
73
+ indent_level : 0 ,
74
+ indent_stack : vec ! ( IndentFlags :: empty ( ) ) ,
62
75
63
- start_document_emitted : false
76
+ start_document_emitted : false
77
+ }
64
78
}
65
79
}
66
80
@@ -261,15 +275,7 @@ impl Emitter {
261
275
namespace : & ' a N ) -> EmitterResult < ( ) > {
262
276
try!( self . emit_start_element_initial ( target, name, attributes, namespace) ) ;
263
277
264
- try!( self . check_document_started ( target) ) ;
265
-
266
- wrapped_with ! ( self ; before_start_element( target) and after_start_element, {
267
- io_try!( write!( target, "<{}" , name. to_str_proper( ) ) ) ;
268
-
269
- try!( self . emit_namespace_attributes( target, namespace) ) ;
270
-
271
- self . emit_attributes( target, attributes)
272
- } )
278
+ io_wrap ( write ! ( target, ">" ) )
273
279
}
274
280
275
281
pub fn emit_namespace_attributes < ' a , W : Writer ,
@@ -278,16 +284,19 @@ impl Emitter {
278
284
> ( & mut self , target : & mut W , namespace : & ' a N ) -> EmitterResult < ( ) > {
279
285
for ( prefix, uri) in namespace. uri_mappings ( ) {
280
286
io_try ! ( match prefix {
287
+ Some ( "xmlns" ) | Some ( "xml" ) => Ok ( ( ) ) , // emit nothing
281
288
Some ( prefix) => write!( target, " xmlns:{}=\" {}\" " , prefix, uri) ,
282
- None => write!( target, " xmlns=\" {}\" " , uri)
289
+ None => if !uri. is_empty( ) { // emit xmlns only if it is overridden
290
+ write!( target, " xmlns=\" {}\" " , uri)
291
+ } else { Ok ( ( ) ) }
283
292
} ) ;
284
293
}
285
294
Ok ( ( ) )
286
295
}
287
296
288
297
pub fn emit_attributes < W : Writer > ( & mut self , target : & mut W , attributes : & [ Attribute ] ) -> EmitterResult < ( ) > {
289
298
for attr in attributes. iter ( ) {
290
- io_try ! ( write!( target, " {}=\" {}\" " , attr. name. to_str_proper( ) , escape_str( attr. value. as_slice( ) ) ) ) ;
299
+ io_try ! ( write!( target, " {}=\" {}\" " , attr. name. to_str_proper( ) , escape_str( attr. value. as_slice( ) ) ) )
291
300
}
292
301
Ok ( ( ) )
293
302
}
0 commit comments