| 1 |
//! Contains emitter configuration structure. |
| 2 |
|
| 3 |
use std::io::Write; |
| 4 |
use std::borrow::Cow; |
| 5 |
|
| 6 |
use 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)] |
| 12 |
pub 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 |
/// Whether or not to automatically insert spaces before the trailing `/>` in self-closing |
| 78 |
/// elements. Default is true. |
| 79 |
/// |
| 80 |
/// This option is only meaningful if `normalize_empty_elements` is true. For example, the |
| 81 |
/// element `<a></a>` would be unaffected. When `normalize_empty_elements` is true, then when |
| 82 |
/// this option is also true, the same element would appear `<a />`. If this option is false, |
| 83 |
/// then the same element would appear `<a/>`. |
| 84 |
pub pad_self_closing: bool, |
| 85 |
} |
| 86 |
|
| 87 |
impl EmitterConfig { |
| 88 |
/// Creates an emitter configuration with default values. |
| 89 |
/// |
| 90 |
/// You can tweak default options with builder-like pattern: |
| 91 |
/// |
| 92 |
/// ```rust |
| 93 |
/// use xml::writer::EmitterConfig; |
| 94 |
/// |
| 95 |
/// let config = EmitterConfig::new() |
| 96 |
/// .line_separator("\r\n") |
| 97 |
/// .perform_indent(true) |
| 98 |
/// .normalize_empty_elements(false); |
| 99 |
/// ``` |
| 100 |
#[inline] |
| 101 |
pub fn new() -> EmitterConfig { |
| 102 |
EmitterConfig { |
| 103 |
line_separator: "\n".into(), |
| 104 |
indent_string: " ".into(), // two spaces |
| 105 |
perform_indent: false, |
| 106 |
perform_escaping: true, |
| 107 |
write_document_declaration: true, |
| 108 |
normalize_empty_elements: true, |
| 109 |
cdata_to_characters: false, |
| 110 |
keep_element_names_stack: true, |
| 111 |
autopad_comments: true, |
| 112 |
pad_self_closing: true |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/// Creates an XML writer with this configuration. |
| 117 |
/// |
| 118 |
/// This is a convenience method for configuring and creating a writer at the same time: |
| 119 |
/// |
| 120 |
/// ```rust |
| 121 |
/// use xml::writer::EmitterConfig; |
| 122 |
/// |
| 123 |
/// let mut target: Vec<u8> = Vec::new(); |
| 124 |
/// |
| 125 |
/// let writer = EmitterConfig::new() |
| 126 |
/// .line_separator("\r\n") |
| 127 |
/// .perform_indent(true) |
| 128 |
/// .normalize_empty_elements(false) |
| 129 |
/// .create_writer(&mut target); |
| 130 |
/// ``` |
| 131 |
/// |
| 132 |
/// This method is exactly equivalent to calling `EventWriter::new_with_config()` with |
| 133 |
/// this configuration object. |
| 134 |
#[inline] |
| 135 |
pub fn create_writer<W: Write>(self, sink: W) -> EventWriter<W> { |
| 136 |
EventWriter::new_with_config(sink, self) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl Default for EmitterConfig { |
| 141 |
#[inline] |
| 142 |
fn default() -> EmitterConfig { |
| 143 |
EmitterConfig::new() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
gen_setters!(EmitterConfig, |
| 148 |
line_separator: into Cow<'static, str>, |
| 149 |
indent_string: into Cow<'static, str>, |
| 150 |
perform_indent: val bool, |
| 151 |
write_document_declaration: val bool, |
| 152 |
normalize_empty_elements: val bool, |
| 153 |
cdata_to_characters: val bool, |
| 154 |
keep_element_names_stack: val bool, |
| 155 |
autopad_comments: val bool, |
| 156 |
pad_self_closing: val bool |
| 157 |
); |