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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
//! Defines zero-copy XML events used throughout this library.
//!
//! A XML event often represents part of a XML element.
//! They occur both during reading and writing and are
//! usually used with the stream-oriented API.
//!
//! For example, the XML element
//! ```xml
//! <name attr="value">Inner text</name>
//! ```
//! consists of the three events `Start`, `Text` and `End`.
//! They can also represent other parts in an XML document like the
//! XML declaration. Each Event usually contains further information,
//! like the tag name, the attribute or the inner text.
//!
//! See [`Event`] for a list of all possible events.
//!
//! # Reading
//! When reading a XML stream, the events are emitted by
//! [`Reader::read_event`]. You must listen
//! for the different types of events you are interested in.
//!
//! See [`Reader`] for further information.
//!
//! # Writing
//! When writing the XML document, you must create the XML element
//! by constructing the events it consists of and pass them to the writer
//! sequentially.
//!
//! See [`Writer`] for further information.
//!
//! [`Reader::read_event`]: ../reader/struct.Reader.html#method.read_event
//! [`Reader`]: ../reader/struct.Reader.html
//! [`Writer`]: ../writer/struct.Writer.html
//! [`Event`]: enum.Event.html
pub mod attributes;
#[cfg(feature = "encoding_rs")]
use encoding_rs::Encoding;
use std::{borrow::Cow, collections::HashMap, io::BufRead, ops::Deref, str::from_utf8};
use crate::escape::{do_unescape, escape, partial_escape};
use crate::utils::write_cow_string;
use crate::{errors::Error, errors::Result, reader::Reader};
use attributes::{Attribute, Attributes};
#[cfg(feature = "serialize")]
use crate::escape::EscapeError;
use memchr;
/// Opening tag data (`Event::Start`), with optional attributes.
///
/// `<name attr="value">`.
///
/// The name can be accessed using the [`name`], [`local_name`] or [`unescaped`] methods. An
/// iterator over the attributes is returned by the [`attributes`] method.
///
/// [`name`]: #method.name
/// [`local_name`]: #method.local_name
/// [`unescaped`]: #method.unescaped
/// [`attributes`]: #method.attributes
#[derive(Clone, Eq, PartialEq)]
pub struct BytesStart<'a> {
/// content of the element, before any utf8 conversion
buf: Cow<'a, [u8]>,
/// end of the element name, the name starts at that the start of `buf`
name_len: usize,
}
impl<'a> BytesStart<'a> {
/// Creates a new `BytesStart` from the given content (name + attributes).
///
/// # Warning
///
/// `&content[..name_len]` is not checked to be a valid name
#[inline]
pub fn borrowed(content: &'a [u8], name_len: usize) -> Self {
BytesStart {
buf: Cow::Borrowed(content),
name_len,
}
}
/// Creates a new `BytesStart` from the given name.
///
/// # Warning
///
/// `&content` is not checked to be a valid name
#[inline]
pub fn borrowed_name(name: &'a [u8]) -> BytesStart<'a> {
Self::borrowed(name, name.len())
}
/// Creates a new `BytesStart` from the given content (name + attributes)
///
/// Owns its contents.
#[inline]
pub fn owned<C: Into<Vec<u8>>>(content: C, name_len: usize) -> BytesStart<'static> {
BytesStart {
buf: Cow::Owned(content.into()),
name_len,
}
}
/// Creates a new `BytesStart` from the given name
///
/// Owns its contents.
#[inline]
pub fn owned_name<C: Into<Vec<u8>>>(name: C) -> BytesStart<'static> {
let content = name.into();
BytesStart {
name_len: content.len(),
buf: Cow::Owned(content),
}
}
/// Converts the event into an owned event.
pub fn into_owned(self) -> BytesStart<'static> {
Self::owned(self.buf.into_owned(), self.name_len)
}
/// Converts the event into an owned event without taking ownership of Event
pub fn to_owned(&self) -> BytesStart<'static> {
Self::owned(self.buf.to_owned(), self.name_len)
}
/// Converts the event into a borrowed event. Most useful when paired with [`to_end`].
///
/// # Example
///
/// ```rust
/// # use quick_xml::{Error, Writer};
/// use quick_xml::events::{BytesStart, Event};
///
/// struct SomeStruct<'a> {
/// attrs: BytesStart<'a>,
/// // ...
/// }
/// # impl<'a> SomeStruct<'a> {
/// # fn example(&self) -> Result<(), Error> {
/// # let mut writer = Writer::new(Vec::new());
///
/// writer.write_event(Event::Start(self.attrs.to_borrowed()))?;
/// // ...
/// writer.write_event(Event::End(self.attrs.to_end()))?;
/// # Ok(())
/// # }}
/// ```
///
/// [`to_end`]: #method.to_end
pub fn to_borrowed(&self) -> BytesStart {
BytesStart::borrowed(&self.buf, self.name_len)
}
/// Creates new paired close tag
pub fn to_end(&self) -> BytesEnd {
BytesEnd::borrowed(self.name())
}
/// Gets the undecoded raw tag name as a `&[u8]`.
#[inline]
pub fn name(&self) -> &[u8] {
&self.buf[..self.name_len]
}
/// Gets the undecoded raw local tag name (excluding namespace) as a `&[u8]`.
///
/// All content up to and including the first `:` character is removed from the tag name.
#[inline]
pub fn local_name(&self) -> &[u8] {
let name = self.name();
memchr::memchr(b':', name).map_or(name, |i| &name[i + 1..])
}
/// Gets the unescaped tag name.
///
/// XML escape sequences like "`<`" will be replaced by their unescaped characters like
/// "`<`".
///
/// See also [`unescaped_with_custom_entities()`](#method.unescaped_with_custom_entities)
#[inline]
pub fn unescaped(&self) -> Result<Cow<[u8]>> {
self.make_unescaped(None)
}
/// Gets the unescaped tag name, using custom entities.
///
/// XML escape sequences like "`<`" will be replaced by their unescaped characters like
/// "`<`".
/// Additional entities can be provided in `custom_entities`.
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
///
/// See also [`unescaped()`](#method.unescaped)
#[inline]
pub fn unescaped_with_custom_entities<'s>(
&'s self,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<Cow<'s, [u8]>> {
self.make_unescaped(Some(custom_entities))
}
#[inline]
fn make_unescaped<'s>(
&'s self,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<Cow<'s, [u8]>> {
do_unescape(&*self.buf, custom_entities).map_err(Error::EscapeError)
}
/// Returns the unescaped and decoded string value.
///
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
/// instead use one of:
///
/// * [`unescaped()`], as it doesn't allocate when no escape sequences are used.
/// * [`Reader::decode()`], as it only allocates when the decoding can't be performed otherwise.
///
/// [`unescaped()`]: #method.unescaped
/// [`Reader::decode()`]: ../reader/struct.Reader.html#method.decode
#[inline]
pub fn unescape_and_decode<B: BufRead>(&self, reader: &Reader<B>) -> Result<String> {
self.do_unescape_and_decode_with_custom_entities(reader, None)
}
/// Returns the unescaped and decoded string value with custom entities.
///
/// This allocates a `String` in all cases. For performance reasons it might be a better idea to
/// instead use one of:
///
/// * [`unescaped_with_custom_entities()`], as it doesn't allocate when no escape sequences are used.
/// * [`Reader::decode()`], as it only allocates when the decoding can't be performed otherwise.
///
/// [`unescaped_with_custom_entities()`]: #method.unescaped_with_custom_entities
/// [`Reader::decode()`]: ../reader/struct.Reader.html#method.decode
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
#[inline]
pub fn unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<String> {
self.do_unescape_and_decode_with_custom_entities(reader, Some(custom_entities))
}
#[cfg(feature = "encoding")]
#[inline]
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<String> {
let decoded = reader.decode(&*self);
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}
#[cfg(not(feature = "encoding"))]
#[inline]
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<String> {
let decoded = reader.decode(&*self)?;
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}
/// Edit the name of the BytesStart in-place
///
/// # Warning
///
/// `name` is not checked to be a valid name
pub fn set_name(&mut self, name: &[u8]) -> &mut BytesStart<'a> {
let bytes = self.buf.to_mut();
bytes.splice(..self.name_len, name.iter().cloned());
self.name_len = name.len();
self
}
}
/// Attribute-related methods
impl<'a> BytesStart<'a> {
/// Consumes `self` and yield a new `BytesStart` with additional attributes from an iterator.
///
/// The yielded items must be convertible to [`Attribute`] using `Into`.
pub fn with_attributes<'b, I>(mut self, attributes: I) -> Self
where
I: IntoIterator,
I::Item: Into<Attribute<'b>>,
{
self.extend_attributes(attributes);
self
}
/// Add additional attributes to this tag using an iterator.
///
/// The yielded items must be convertible to [`Attribute`] using `Into`.
pub fn extend_attributes<'b, I>(&mut self, attributes: I) -> &mut BytesStart<'a>
where
I: IntoIterator,
I::Item: Into<Attribute<'b>>,
{
for attr in attributes {
self.push_attribute(attr);
}
self
}
/// Adds an attribute to this element.
pub fn push_attribute<'b, A>(&mut self, attr: A)
where
A: Into<Attribute<'b>>,
{
let a = attr.into();
let bytes = self.buf.to_mut();
bytes.push(b' ');
bytes.extend_from_slice(a.key);
bytes.extend_from_slice(b"=\"");
bytes.extend_from_slice(&*a.value);
bytes.push(b'"');
}
/// Remove all attributes from the ByteStart
pub fn clear_attributes(&mut self) -> &mut BytesStart<'a> {
self.buf.to_mut().truncate(self.name_len);
self
}
/// Returns an iterator over the attributes of this tag.
pub fn attributes(&self) -> Attributes {
Attributes::new(&self.buf, self.name_len)
}
/// Returns an iterator over the HTML-like attributes of this tag (no mandatory quotes or `=`).
pub fn html_attributes(&self) -> Attributes {
Attributes::html(self, self.name_len)
}
/// Gets the undecoded raw string with the attributes of this tag as a `&[u8]`,
/// including the whitespace after the tag name if there is any.
#[inline]
pub fn attributes_raw(&self) -> &[u8] {
&self.buf[self.name_len..]
}
/// Try to get an attribute
pub fn try_get_attribute<N: AsRef<[u8]> + Sized>(
&'a self,
attr_name: N,
) -> Result<Option<Attribute<'a>>> {
for a in self.attributes() {
let a = a?;
if a.key == attr_name.as_ref() {
return Ok(Some(a));
}
}
Ok(None)
}
}
impl<'a> std::fmt::Debug for BytesStart<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "BytesStart {{ buf: ")?;
write_cow_string(f, &self.buf)?;
write!(f, ", name_len: {} }}", self.name_len)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// An XML declaration (`Event::Decl`).
///
/// [W3C XML 1.1 Prolog and Document Type Declaration](http://w3.org/TR/xml11/#sec-prolog-dtd)
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BytesDecl<'a> {
element: BytesStart<'a>,
}
impl<'a> BytesDecl<'a> {
/// Creates a `BytesDecl` from a `BytesStart`
pub fn from_start(start: BytesStart<'a>) -> BytesDecl<'a> {
BytesDecl { element: start }
}
/// Gets xml version, excluding quotes (`'` or `"`).
///
/// According to the [grammar], the version *must* be the first thing in the declaration.
/// This method tries to extract the first thing in the declaration and return it.
/// In case of multiple attributes value of the first one is returned.
///
/// If version is missed in the declaration, or the first thing is not a version,
/// [`Error::XmlDeclWithoutVersion`] will be returned.
///
/// # Examples
///
/// ```
/// use std::borrow::Cow;
/// use quick_xml::Error;
/// use quick_xml::events::{BytesDecl, BytesStart};
///
/// // <?xml version='1.1'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" version='1.1'", 0));
/// assert_eq!(
/// decl.version().unwrap(),
/// Cow::Borrowed(b"1.1".as_ref())
/// );
///
/// // <?xml version='1.0' version='1.1'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" version='1.0' version='1.1'", 0));
/// assert_eq!(
/// decl.version().unwrap(),
/// Cow::Borrowed(b"1.0".as_ref())
/// );
///
/// // <?xml encoding='utf-8'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" encoding='utf-8'", 0));
/// match decl.version() {
/// Err(Error::XmlDeclWithoutVersion(Some(key))) => assert_eq!(key, "encoding".to_string()),
/// _ => assert!(false),
/// }
///
/// // <?xml encoding='utf-8' version='1.1'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" encoding='utf-8' version='1.1'", 0));
/// match decl.version() {
/// Err(Error::XmlDeclWithoutVersion(Some(key))) => assert_eq!(key, "encoding".to_string()),
/// _ => assert!(false),
/// }
///
/// // <?xml?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b"", 0));
/// match decl.version() {
/// Err(Error::XmlDeclWithoutVersion(None)) => {},
/// _ => assert!(false),
/// }
/// ```
///
/// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
pub fn version(&self) -> Result<Cow<[u8]>> {
// The version *must* be the first thing in the declaration.
match self.element.attributes().with_checks(false).next() {
Some(Ok(a)) if a.key == b"version" => Ok(a.value),
// first attribute was not "version"
Some(Ok(a)) => {
let found = from_utf8(a.key).map_err(Error::Utf8)?.to_string();
Err(Error::XmlDeclWithoutVersion(Some(found)))
}
// error parsing attributes
Some(Err(e)) => Err(e.into()),
// no attributes
None => Err(Error::XmlDeclWithoutVersion(None)),
}
}
/// Gets xml encoding, excluding quotes (`'` or `"`).
///
/// Although according to the [grammar] encoding must appear before `"standalone"`
/// and after `"version"`, this method does not check that. The first occurrence
/// of the attribute will be returned even if there are several. Also, method does
/// not restrict symbols that can forming the encoding, so the returned encoding
/// name may not correspond to the grammar.
///
/// # Examples
///
/// ```
/// use std::borrow::Cow;
/// use quick_xml::Error;
/// use quick_xml::events::{BytesDecl, BytesStart};
///
/// // <?xml version='1.1'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" version='1.1'", 0));
/// assert!(decl.encoding().is_none());
///
/// // <?xml encoding='utf-8'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" encoding='utf-8'", 0));
/// match decl.encoding() {
/// Some(Ok(Cow::Borrowed(encoding))) => assert_eq!(encoding, b"utf-8"),
/// _ => assert!(false),
/// }
///
/// // <?xml encoding='something_WRONG' encoding='utf-8'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" encoding='something_WRONG' encoding='utf-8'", 0));
/// match decl.encoding() {
/// Some(Ok(Cow::Borrowed(encoding))) => assert_eq!(encoding, b"something_WRONG"),
/// _ => assert!(false),
/// }
/// ```
///
/// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
pub fn encoding(&self) -> Option<Result<Cow<[u8]>>> {
self.element
.try_get_attribute("encoding")
.map(|a| a.map(|a| a.value))
.transpose()
}
/// Gets xml standalone, excluding quotes (`'` or `"`).
///
/// Although according to the [grammar] standalone flag must appear after `"version"`
/// and `"encoding"`, this method does not check that. The first occurrence of the
/// attribute will be returned even if there are several. Also, method does not
/// restrict symbols that can forming the value, so the returned flag name may not
/// correspond to the grammar.
///
/// # Examples
///
/// ```
/// use std::borrow::Cow;
/// use quick_xml::Error;
/// use quick_xml::events::{BytesDecl, BytesStart};
///
/// // <?xml version='1.1'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" version='1.1'", 0));
/// assert!(decl.standalone().is_none());
///
/// // <?xml standalone='yes'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" standalone='yes'", 0));
/// match decl.standalone() {
/// Some(Ok(Cow::Borrowed(encoding))) => assert_eq!(encoding, b"yes"),
/// _ => assert!(false),
/// }
///
/// // <?xml standalone='something_WRONG' encoding='utf-8'?>
/// let decl = BytesDecl::from_start(BytesStart::borrowed(b" standalone='something_WRONG' encoding='utf-8'", 0));
/// match decl.standalone() {
/// Some(Ok(Cow::Borrowed(flag))) => assert_eq!(flag, b"something_WRONG"),
/// _ => assert!(false),
/// }
/// ```
///
/// [grammar]: https://www.w3.org/TR/xml11/#NT-XMLDecl
pub fn standalone(&self) -> Option<Result<Cow<[u8]>>> {
self.element
.try_get_attribute("standalone")
.map(|a| a.map(|a| a.value))
.transpose()
}
/// Constructs a new `XmlDecl` from the (mandatory) _version_ (should be `1.0` or `1.1`),
/// the optional _encoding_ (e.g., `UTF-8`) and the optional _standalone_ (`yes` or `no`)
/// attribute.
///
/// Does not escape any of its inputs. Always uses double quotes to wrap the attribute values.
/// The caller is responsible for escaping attribute values. Shouldn't usually be relevant since
/// the double quote character is not allowed in any of the attribute values.
pub fn new(
version: &[u8],
encoding: Option<&[u8]>,
standalone: Option<&[u8]>,
) -> BytesDecl<'static> {
// Compute length of the buffer based on supplied attributes
// ' encoding=""' => 12
let encoding_attr_len = if let Some(xs) = encoding {
12 + xs.len()
} else {
0
};
// ' standalone=""' => 14
let standalone_attr_len = if let Some(xs) = standalone {
14 + xs.len()
} else {
0
};
// 'xml version=""' => 14
let mut buf = Vec::with_capacity(14 + encoding_attr_len + standalone_attr_len);
buf.extend_from_slice(b"xml version=\"");
buf.extend_from_slice(version);
if let Some(encoding_val) = encoding {
buf.extend_from_slice(b"\" encoding=\"");
buf.extend_from_slice(encoding_val);
}
if let Some(standalone_val) = standalone {
buf.extend_from_slice(b"\" standalone=\"");
buf.extend_from_slice(standalone_val);
}
buf.push(b'"');
BytesDecl {
element: BytesStart::owned(buf, 3),
}
}
/// Gets the decoder struct
#[cfg(feature = "encoding_rs")]
pub fn encoder(&self) -> Option<&'static Encoding> {
self.encoding()
.and_then(|e| e.ok())
.and_then(|e| Encoding::for_label(&*e))
}
/// Converts the event into an owned event.
pub fn into_owned(self) -> BytesDecl<'static> {
BytesDecl {
element: self.element.into_owned(),
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// A struct to manage `Event::End` events
#[derive(Clone, Eq, PartialEq)]
pub struct BytesEnd<'a> {
name: Cow<'a, [u8]>,
}
impl<'a> BytesEnd<'a> {
/// Creates a new `BytesEnd` borrowing a slice
#[inline]
pub fn borrowed(name: &'a [u8]) -> BytesEnd<'a> {
BytesEnd {
name: Cow::Borrowed(name),
}
}
/// Creates a new `BytesEnd` owning its name
#[inline]
pub fn owned(name: Vec<u8>) -> BytesEnd<'static> {
BytesEnd {
name: Cow::Owned(name),
}
}
/// Converts the event into an owned event.
pub fn into_owned(self) -> BytesEnd<'static> {
BytesEnd {
name: Cow::Owned(self.name.into_owned()),
}
}
/// Gets `BytesEnd` event name
#[inline]
pub fn name(&self) -> &[u8] {
&*self.name
}
/// local name (excluding namespace) as &[u8] (without eventual attributes)
/// returns the name() with any leading namespace removed (all content up to
/// and including the first ':' character)
#[inline]
pub fn local_name(&self) -> &[u8] {
if let Some(i) = self.name().iter().position(|b| *b == b':') {
&self.name()[i + 1..]
} else {
self.name()
}
}
}
impl<'a> std::fmt::Debug for BytesEnd<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "BytesEnd {{ name: ")?;
write_cow_string(f, &self.name)?;
write!(f, " }}")
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Data from various events (most notably, `Event::Text`) that stored in XML
/// in escaped form. Internally data is stored in escaped form
#[derive(Clone, Eq, PartialEq)]
pub struct BytesText<'a> {
// Invariant: The content is always escaped.
content: Cow<'a, [u8]>,
}
impl<'a> BytesText<'a> {
/// Creates a new `BytesText` from an escaped byte sequence.
#[inline]
pub fn from_escaped<C: Into<Cow<'a, [u8]>>>(content: C) -> Self {
Self {
content: content.into(),
}
}
/// Creates a new `BytesText` from a byte sequence. The byte sequence is
/// expected not to be escaped.
#[inline]
pub fn from_plain(content: &'a [u8]) -> Self {
Self {
content: escape(content),
}
}
/// Creates a new `BytesText` from an escaped string.
#[inline]
pub fn from_escaped_str<C: Into<Cow<'a, str>>>(content: C) -> Self {
Self::from_escaped(match content.into() {
Cow::Owned(o) => Cow::Owned(o.into_bytes()),
Cow::Borrowed(b) => Cow::Borrowed(b.as_bytes()),
})
}
/// Creates a new `BytesText` from a string. The string is expected not to
/// be escaped.
#[inline]
pub fn from_plain_str(content: &'a str) -> Self {
Self::from_plain(content.as_bytes())
}
/// Ensures that all data is owned to extend the object's lifetime if
/// necessary.
#[inline]
pub fn into_owned(self) -> BytesText<'static> {
BytesText {
content: self.content.into_owned().into(),
}
}
/// Extracts the inner `Cow` from the `BytesText` event container.
#[inline]
pub fn into_inner(self) -> Cow<'a, [u8]> {
self.content
}
/// Returns unescaped version of the text content, that can be written
/// as CDATA in XML
#[cfg(feature = "serialize")]
pub(crate) fn unescape(self) -> std::result::Result<BytesCData<'a>, EscapeError> {
//TODO: need to think about better API instead of dozens similar functions
// Maybe use builder pattern. After that expose function as public API
//FIXME: need to take into account entities defined in the document
Ok(BytesCData::new(match do_unescape(&self.content, None)? {
Cow::Borrowed(_) => self.content,
Cow::Owned(unescaped) => Cow::Owned(unescaped),
}))
}
/// gets escaped content
///
/// Searches for '&' into content and try to escape the coded character if possible
/// returns Malformed error with index within element if '&' is not followed by ';'
///
/// See also [`unescaped_with_custom_entities()`](#method.unescaped_with_custom_entities)
pub fn unescaped(&self) -> Result<Cow<[u8]>> {
self.make_unescaped(None)
}
/// gets escaped content with custom entities
///
/// Searches for '&' into content and try to escape the coded character if possible
/// returns Malformed error with index within element if '&' is not followed by ';'
/// Additional entities can be provided in `custom_entities`.
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
///
/// See also [`unescaped()`](#method.unescaped)
pub fn unescaped_with_custom_entities<'s>(
&'s self,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<Cow<'s, [u8]>> {
self.make_unescaped(Some(custom_entities))
}
fn make_unescaped<'s>(
&'s self,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<Cow<'s, [u8]>> {
do_unescape(self, custom_entities).map_err(Error::EscapeError)
}
/// helper method to unescape then decode self using the reader encoding
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
#[cfg(feature = "encoding")]
pub fn unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &mut Reader<B>,
) -> Result<String> {
self.do_unescape_and_decode_without_bom(reader, None)
}
/// helper method to unescape then decode self using the reader encoding
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
#[cfg(not(feature = "encoding"))]
pub fn unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &Reader<B>,
) -> Result<String> {
self.do_unescape_and_decode_without_bom(reader, None)
}
/// helper method to unescape then decode self using the reader encoding with custom entities
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
#[cfg(feature = "encoding")]
pub fn unescape_and_decode_without_bom_with_custom_entities<B: BufRead>(
&self,
reader: &mut Reader<B>,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<String> {
self.do_unescape_and_decode_without_bom(reader, Some(custom_entities))
}
/// helper method to unescape then decode self using the reader encoding with custom entities
/// but without BOM (Byte order mark)
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
#[cfg(not(feature = "encoding"))]
pub fn unescape_and_decode_without_bom_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<String> {
self.do_unescape_and_decode_without_bom(reader, Some(custom_entities))
}
#[cfg(feature = "encoding")]
fn do_unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &mut Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<String> {
let decoded = reader.decode_without_bom(&*self);
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}
#[cfg(not(feature = "encoding"))]
fn do_unescape_and_decode_without_bom<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<String> {
let decoded = reader.decode_without_bom(&*self)?;
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}
/// helper method to unescape then decode self using the reader encoding
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
pub fn unescape_and_decode<B: BufRead>(&self, reader: &Reader<B>) -> Result<String> {
self.do_unescape_and_decode_with_custom_entities(reader, None)
}
/// helper method to unescape then decode self using the reader encoding with custom entities
///
/// for performance reasons (could avoid allocating a `String`),
/// it might be wiser to manually use
/// 1. BytesText::unescaped()
/// 2. Reader::decode(...)
///
/// # Pre-condition
///
/// The keys and values of `custom_entities`, if any, must be valid UTF-8.
pub fn unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: &HashMap<Vec<u8>, Vec<u8>>,
) -> Result<String> {
self.do_unescape_and_decode_with_custom_entities(reader, Some(custom_entities))
}
#[cfg(feature = "encoding")]
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<String> {
let decoded = reader.decode(&*self);
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}
#[cfg(not(feature = "encoding"))]
fn do_unescape_and_decode_with_custom_entities<B: BufRead>(
&self,
reader: &Reader<B>,
custom_entities: Option<&HashMap<Vec<u8>, Vec<u8>>>,
) -> Result<String> {
let decoded = reader.decode(&*self)?;
let unescaped =
do_unescape(decoded.as_bytes(), custom_entities).map_err(Error::EscapeError)?;
String::from_utf8(unescaped.into_owned()).map_err(|e| Error::Utf8(e.utf8_error()))
}
/// Gets escaped content.
pub fn escaped(&self) -> &[u8] {
self.content.as_ref()
}
}
impl<'a> std::fmt::Debug for BytesText<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "BytesText {{ content: ")?;
write_cow_string(f, &self.content)?;
write!(f, " }}")
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// CDATA content contains unescaped data from the reader. If you want to write them as a text,
/// [convert](Self::escape) it to [`BytesText`]
#[derive(Clone, Eq, PartialEq)]
pub struct BytesCData<'a> {
content: Cow<'a, [u8]>,
}
impl<'a> BytesCData<'a> {
/// Creates a new `BytesCData` from a byte sequence.
#[inline]
pub fn new<C: Into<Cow<'a, [u8]>>>(content: C) -> Self {
Self {
content: content.into(),
}
}
/// Creates a new `BytesCData` from a string
#[inline]
pub fn from_str(content: &'a str) -> Self {
Self::new(content.as_bytes())
}
/// Ensures that all data is owned to extend the object's lifetime if
/// necessary.
#[inline]
pub fn into_owned(self) -> BytesCData<'static> {
BytesCData {
content: self.content.into_owned().into(),
}
}
/// Extracts the inner `Cow` from the `BytesCData` event container.
#[inline]
pub fn into_inner(self) -> Cow<'a, [u8]> {
self.content
}
/// Converts this CDATA content to an escaped version, that can be written
/// as an usual text in XML.
///
/// This function performs following replacements:
///
/// | Character | Replacement
/// |-----------|------------
/// | `<` | `<`
/// | `>` | `>`
/// | `&` | `&`
/// | `'` | `'`
/// | `"` | `"`
pub fn escape(self) -> BytesText<'a> {
BytesText::from_escaped(match escape(&self.content) {
Cow::Borrowed(_) => self.content,
Cow::Owned(escaped) => Cow::Owned(escaped),
})
}
/// Converts this CDATA content to an escaped version, that can be written
/// as an usual text in XML.
///
/// In XML text content, it is allowed (though not recommended) to leave
/// the quote special characters `"` and `'` unescaped.
///
/// This function performs following replacements:
///
/// | Character | Replacement
/// |-----------|------------
/// | `<` | `<`
/// | `>` | `>`
/// | `&` | `&`
pub fn partial_escape(self) -> BytesText<'a> {
BytesText::from_escaped(match partial_escape(&self.content) {
Cow::Borrowed(_) => self.content,
Cow::Owned(escaped) => Cow::Owned(escaped),
})
}
/// Gets content of this text buffer in the specified encoding
#[cfg(feature = "serialize")]
pub(crate) fn decode(&self, decoder: crate::reader::Decoder) -> Result<Cow<'a, str>> {
Ok(match &self.content {
Cow::Borrowed(bytes) => {
#[cfg(feature = "encoding")]
{
decoder.decode(bytes)
}
#[cfg(not(feature = "encoding"))]
{
decoder.decode(bytes)?.into()
}
}
Cow::Owned(bytes) => {
#[cfg(feature = "encoding")]
let decoded = decoder.decode(bytes).into_owned();
#[cfg(not(feature = "encoding"))]
let decoded = decoder.decode(bytes)?.to_string();
decoded.into()
}
})
}
}
impl<'a> std::fmt::Debug for BytesCData<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "BytesCData {{ content: ")?;
write_cow_string(f, &self.content)?;
write!(f, " }}")
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Event emitted by [`Reader::read_event`].
///
/// [`Reader::read_event`]: ../reader/struct.Reader.html#method.read_event
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Event<'a> {
/// Start tag (with attributes) `<tag attr="value">`.
Start(BytesStart<'a>),
/// End tag `</tag>`.
End(BytesEnd<'a>),
/// Empty element tag (with attributes) `<tag attr="value" />`.
Empty(BytesStart<'a>),
/// Character data between `Start` and `End` element.
Text(BytesText<'a>),
/// Comment `<!-- ... -->`.
Comment(BytesText<'a>),
/// CData `<![CDATA[...]]>`.
CData(BytesCData<'a>),
/// XML declaration `<?xml ...?>`.
Decl(BytesDecl<'a>),
/// Processing instruction `<?...?>`.
PI(BytesText<'a>),
/// Doctype `<!DOCTYPE ...>`.
DocType(BytesText<'a>),
/// End of XML document.
Eof,
}
impl<'a> Event<'a> {
/// Converts the event to an owned version, untied to the lifetime of
/// buffer used when reading but incurring a new, separate allocation.
pub fn into_owned(self) -> Event<'static> {
match self {
Event::Start(e) => Event::Start(e.into_owned()),
Event::End(e) => Event::End(e.into_owned()),
Event::Empty(e) => Event::Empty(e.into_owned()),
Event::Text(e) => Event::Text(e.into_owned()),
Event::Comment(e) => Event::Comment(e.into_owned()),
Event::CData(e) => Event::CData(e.into_owned()),
Event::Decl(e) => Event::Decl(e.into_owned()),
Event::PI(e) => Event::PI(e.into_owned()),
Event::DocType(e) => Event::DocType(e.into_owned()),
Event::Eof => Event::Eof,
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
impl<'a> Deref for BytesStart<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.buf
}
}
impl<'a> Deref for BytesDecl<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.element
}
}
impl<'a> Deref for BytesEnd<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.name
}
}
impl<'a> Deref for BytesText<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.content
}
}
impl<'a> Deref for BytesCData<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&*self.content
}
}
impl<'a> Deref for Event<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
match *self {
Event::Start(ref e) | Event::Empty(ref e) => &*e,
Event::End(ref e) => &*e,
Event::Text(ref e) => &*e,
Event::Decl(ref e) => &*e,
Event::PI(ref e) => &*e,
Event::CData(ref e) => &*e,
Event::Comment(ref e) => &*e,
Event::DocType(ref e) => &*e,
Event::Eof => &[],
}
}
}
impl<'a> AsRef<Event<'a>> for Event<'a> {
fn as_ref(&self) -> &Event<'a> {
self
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn local_name() {
use std::str::from_utf8;
let xml = r#"
<foo:bus attr='bar'>foobusbar</foo:bus>
<foo: attr='bar'>foobusbar</foo:>
<:foo attr='bar'>foobusbar</:foo>
<foo:bus:baz attr='bar'>foobusbar</foo:bus:baz>
"#;
let mut rdr = Reader::from_str(xml);
let mut buf = Vec::new();
let mut parsed_local_names = Vec::new();
loop {
match rdr.read_event(&mut buf).expect("unable to read xml event") {
Event::Start(ref e) => parsed_local_names.push(
from_utf8(e.local_name())
.expect("unable to build str from local_name")
.to_string(),
),
Event::End(ref e) => parsed_local_names.push(
from_utf8(e.local_name())
.expect("unable to build str from local_name")
.to_string(),
),
Event::Eof => break,
_ => {}
}
}
assert_eq!(parsed_local_names[0], "bus".to_string());
assert_eq!(parsed_local_names[1], "bus".to_string());
assert_eq!(parsed_local_names[2], "".to_string());
assert_eq!(parsed_local_names[3], "".to_string());
assert_eq!(parsed_local_names[4], "foo".to_string());
assert_eq!(parsed_local_names[5], "foo".to_string());
assert_eq!(parsed_local_names[6], "bus:baz".to_string());
assert_eq!(parsed_local_names[7], "bus:baz".to_string());
}
#[test]
fn bytestart_create() {
let b = BytesStart::owned_name("test");
assert_eq!(b.len(), 4);
assert_eq!(b.name(), b"test");
}
#[test]
fn bytestart_set_name() {
let mut b = BytesStart::owned_name("test");
assert_eq!(b.len(), 4);
assert_eq!(b.name(), b"test");
assert_eq!(b.attributes_raw(), b"");
b.push_attribute(("x", "a"));
assert_eq!(b.len(), 10);
assert_eq!(b.attributes_raw(), b" x=\"a\"");
b.set_name(b"g");
assert_eq!(b.len(), 7);
assert_eq!(b.name(), b"g");
}
#[test]
fn bytestart_clear_attributes() {
let mut b = BytesStart::owned_name("test");
b.push_attribute(("x", "y\"z"));
b.push_attribute(("x", "y\"z"));
b.clear_attributes();
assert!(b.attributes().next().is_none());
assert_eq!(b.len(), 4);
assert_eq!(b.name(), b"test");
}
}