xml/writer/
config.rs

1//! Contains emitter configuration structure.
2
3use std::io::Write;
4use std::borrow::Cow;
5
6use writer::EventWriter;
7
8/// Emitter configuration structure.
9///
10/// This structure contains various options which control XML document emitter behavior.
11#[derive(Clone, PartialEq, Eq, Debug)]
12pub struct EmitterConfig {
13    /// Line separator used to separate lines in formatted output. Default is `"\n"`.
14    pub line_separator: Cow<'static, str>,
15
16    /// A string which will be used for a single level of indentation. Default is `"  "`
17    /// (two spaces).
18    pub indent_string: Cow<'static, str>,
19
20    /// Whether or not the emitted document should be indented. Default is false.
21    ///
22    /// The emitter is capable to perform automatic indentation of the emitted XML document.
23    /// It is done in stream-like fashion and does not require the knowledge of the whole
24    /// document in advance.
25    ///
26    /// Sometimes, however, automatic indentation is undesirable, e.g. when you want to keep
27    /// existing layout when processing an existing XML document. Also the indentiation algorithm
28    /// is not thoroughly tested. Hence by default it is disabled.
29    pub perform_indent: bool,
30
31    /// Whether or not characters in output events will be escaped. Default is true.
32    ///
33    /// The emitter can automatically escape characters which can't appear in PCDATA sections
34    /// or element attributes of an XML document, like `<` or `"` (in attributes). This may
35    /// introduce some overhead because then every corresponding piece of character data
36    /// should be scanned for invalid characters.
37    ///
38    /// If this option is disabled, the XML writer may produce non-well-formed documents, so
39    /// use `false` value for this option with care.
40    pub perform_escaping: bool,
41
42    /// Whether or not to write XML document declaration at the beginning of a document.
43    /// Default is true.
44    ///
45    /// This option controls whether the document declaration should be emitted automatically
46    /// before a root element is written if it was not emitted explicitly by the user.
47    pub write_document_declaration: bool,
48
49    /// Whether or not to convert elements with empty content to empty elements. Default is true.
50    ///
51    /// This option allows turning elements like `<a></a>` (an element with empty content)
52    /// into `<a />` (an empty element).
53    pub normalize_empty_elements: bool,
54
55    /// Whether or not to emit CDATA events as plain characters. Default is false.
56    ///
57    /// This option forces the emitter to convert CDATA events into regular character events,
58    /// performing all the necessary escaping beforehand. This may be occasionally useful
59    /// for feeding the document into incorrect parsers which do not support CDATA.
60    pub cdata_to_characters: bool,
61
62    /// Whether or not to keep element names to support `EndElement` events without explicit names.
63    /// Default is true.
64    ///
65    /// This option makes the emitter to keep names of written elements in order to allow
66    /// omitting names when writing closing element tags. This could incur some memory overhead.
67    pub keep_element_names_stack: bool,
68
69    /// Whether or not to automatically insert leading and trailing spaces in emitted comments,
70    /// if necessary. Default is true.
71    ///
72    /// This is a convenience option in order for the user not to append spaces before and after
73    /// comments text in order to get more pretty comments: `<!-- something -->` instead of
74    /// `<!--something-->`.
75    pub autopad_comments: bool,
76}
77
78impl EmitterConfig {
79    /// Creates an emitter configuration with default values.
80    ///
81    /// You can tweak default options with builder-like pattern:
82    ///
83    /// ```rust
84    /// use xml::writer::EmitterConfig;
85    ///
86    /// let config = EmitterConfig::new()
87    ///     .line_separator("\r\n")
88    ///     .perform_indent(true)
89    ///     .normalize_empty_elements(false);
90    /// ```
91    #[inline]
92    pub fn new() -> EmitterConfig {
93        EmitterConfig {
94            line_separator: "\n".into(),
95            indent_string: "  ".into(),  // two spaces
96            perform_indent: false,
97            perform_escaping: true,
98            write_document_declaration: true,
99            normalize_empty_elements: true,
100            cdata_to_characters: false,
101            keep_element_names_stack: true,
102            autopad_comments: true
103        }
104    }
105
106    /// Creates an XML writer with this configuration.
107    ///
108    /// This is a convenience method for configuring and creating a writer at the same time:
109    ///
110    /// ```rust
111    /// use xml::writer::EmitterConfig;
112    ///
113    /// let mut target: Vec<u8> = Vec::new();
114    ///
115    /// let writer = EmitterConfig::new()
116    ///     .line_separator("\r\n")
117    ///     .perform_indent(true)
118    ///     .normalize_empty_elements(false)
119    ///     .create_writer(&mut target);
120    /// ```
121    ///
122    /// This method is exactly equivalent to calling `EventWriter::new_with_config()` with
123    /// this configuration object.
124    #[inline]
125    pub fn create_writer<W: Write>(self, sink: W) -> EventWriter<W> {
126        EventWriter::new_with_config(sink, self)
127    }
128}
129
130impl Default for EmitterConfig {
131    #[inline]
132    fn default() -> EmitterConfig {
133        EmitterConfig::new()
134    }
135}
136
137gen_setters!(EmitterConfig,
138    line_separator: into Cow<'static, str>,
139    indent_string: into Cow<'static, str>,
140    perform_indent: val bool,
141    write_document_declaration: val bool,
142    normalize_empty_elements: val bool,
143    cdata_to_characters: val bool,
144    keep_element_names_stack: val bool,
145    autopad_comments: val bool
146);