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
#![deny(unsafe_code)]
#![warn(clippy::pedantic)]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
use std::{
fs::File,
io::{BufRead, BufReader, BufWriter, Write},
path::Path,
sync::Arc,
};
use namespace::{DepthSensitiveMap, Namespace};
use quick_xml::{
events::{BytesPI, BytesStart, BytesText, Event},
Reader, Writer,
};
use regex::Regex;
mod grammars;
mod namespace;
/// [`Canonicalizer`]s take XML and return the canonicalised form of that XML.
pub struct Canonicalizer<R, W> {
reader: Reader<R>,
writer: Option<Writer<W>>,
}
impl<R, W> Canonicalizer<R, W> {
/// Initialize a new [`Canonicalizer`] that reads from the provided
/// `reader`.
#[must_use]
pub fn read_from_reader(reader: R) -> Self {
tracing::trace!("Canonicalizer initialising with reader");
Self {
reader: Reader::from_reader(reader),
writer: None,
}
}
}
impl<'a, W> Canonicalizer<&'a [u8], W> {
/// Initialize a new [`Canonicalizer`] that reads from the provided
/// `str`.
#[must_use]
pub fn read_from_str(str: &'a str) -> Self {
tracing::trace!("Canonicalizer initialising from &str");
Self {
reader: Reader::from_str(str),
writer: None,
}
}
}
impl<W> Canonicalizer<BufReader<File>, W> {
/// Initialize a new [`Canonicalizer`] that reads from the provided
/// `str`.
///
/// # Errors
///
/// Returns an I/O error if the file couldn't be opened and/or read.
pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, Arc<std::io::Error>> {
tracing::trace!("Canonicalizer initialising from file");
Ok(Self {
reader: Reader::from_file(path).map_err(|e| match e {
quick_xml::Error::Io(e) => e,
_ => unreachable!("no other errors are raised by quick_xml at this point"),
})?,
writer: None,
})
}
}
impl<R, W: Write> Canonicalizer<R, W> {
/// Set the writer for this Canonicalizer to a writer.
#[must_use]
pub fn write_to_writer(mut self, writer: W) -> Self {
self.writer = Some(Writer::new(writer));
self
}
}
impl<R> Canonicalizer<R, BufWriter<File>> {
/// Set the writer for this Canonicalizer to a file.
///
/// # Errors
///
/// Returns an I/O error if the file couldn't be opened and/or read.
pub fn write_to_file<P: AsRef<Path>>(mut self, path: P) -> Result<Self, std::io::Error> {
self.writer = Some(Writer::new(BufWriter::new(File::create(path)?)));
Ok(self)
}
}
impl<R: BufRead, W: Write> Canonicalizer<R, W> {
/// Start canonicalizing the document to the writer.
///
/// # Errors
///
/// Returns an XML error if the XML parsed is invalid.
///
/// # Panics
///
/// This will panic is a writer has not been initialised with
/// `write_to_string`, `write_to_file`, or `write_to_writer`.
pub fn canonicalize(mut self, retain_comments: bool) -> Result<(), quick_xml::Error> {
tracing::debug!("Canonicalisation starting…");
// Set reader config
self.reader.config_mut().allow_unmatched_ends = false;
self.reader.config_mut().check_end_names = true;
self.reader.config_mut().expand_empty_elements = true;
let mut registered_namespaces = DepthSensitiveMap::new();
// Add a default blank namespace.
registered_namespaces.insert_at_depth(0, "_", Namespace { url: String::new() });
let mut writer = self
.writer
.expect("trying to canonicalize without a writer initialised");
let mut buf = vec![];
let mut depth = 0;
let mut hit_pi_rule = false;
let mut text_buf = String::new();
let whitespace_duplicate_regex = Regex::new(r"\n\n*").unwrap();
let pi_tidyup_regex = Regex::new(r"^(\S+)(?:\s*( .*)|\s*$)").unwrap();
loop {
let e = self.reader.read_event_into(&mut buf);
tracing::trace!("Event: {e:?}");
match e {
// The XML declaration and document type declaration (DTD) are removed.
Ok(Event::Decl(_) | Event::DocType(_)) => (),
// Empty should be unreachable as we've instructed the reader to always expand
Ok(Event::Empty(_)) => unreachable!(),
Ok(Event::PI(p)) => {
hit_pi_rule = true;
let p = p.into_inner();
let p = String::from_utf8_lossy(&p).into_owned();
let p = pi_tidyup_regex.replace_all(&p, "$1$2").into_owned();
if !text_buf.is_empty() {
writer.write_event(Event::Text(BytesText::from_escaped(&text_buf)))?;
text_buf.clear();
}
writer.write_event(Event::PI(BytesPI::new(p)))?;
}
// Remove all comments if needed
Ok(Event::Comment(c)) => {
if retain_comments {
if !text_buf.is_empty() {
writer.write_event(Event::Text(BytesText::from_escaped(&text_buf)))?;
text_buf.clear();
}
writer.write_event(Event::Comment(c))?;
}
}
Ok(Event::Text(t)) => {
let text = t.into_inner();
let text = String::from_utf8_lossy(&text);
// Normalise whitespace
let text = text.replace("\r\n", "\n");
text_buf.push_str(
&grammars::character_refs::canonicalize_character_references(
&text,
&grammars::character_refs::Situation::Content,
)
.unwrap(),
);
// Remove whitespace outside of the root tag
if depth == 0 {
if hit_pi_rule {
// Remove duplicates only
text_buf = whitespace_duplicate_regex
.replace_all(&text_buf, "\n")
.into_owned();
} else {
text_buf = text_buf.replace('\n', "");
}
}
}
Ok(Event::CData(c)) => {
let c = c.into_inner();
let c = String::from_utf8_lossy(&c);
let c = c
.replace('&', "&")
.replace('<', "<")
.replace('>', ">");
text_buf.push_str(&c);
}
Ok(Event::Start(s)) => {
depth += 1;
if !text_buf.is_empty() {
writer.write_event(Event::Text(BytesText::from_escaped(&text_buf)))?;
text_buf.clear();
}
let s = String::from_utf8_lossy(&s);
let s = grammars::start::canonicalize_start_tag(
&s,
depth,
&mut registered_namespaces,
)
.unwrap();
writer.write_event(Event::Start(BytesStart::new(s.trim())))?;
}
Ok(Event::End(e)) => {
// Drop all known namespaces at base depth greater than new current
registered_namespaces.remove_depth(depth);
depth -= 1;
if !text_buf.is_empty() {
writer.write_event(Event::Text(BytesText::from_escaped(&text_buf)))?;
text_buf.clear();
}
writer.write_event(Event::End(e))?;
}
Ok(Event::Eof) => break,
Err(e) => return Err(e),
}
}
Ok(())
}
}