support for characters event
This commit is contained in:
parent
82be5087b9
commit
427f24cd9f
6 changed files with 28 additions and 16 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
|
@ -61,12 +61,6 @@ dependencies = [
|
|||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.4"
|
||||
|
|
@ -241,7 +235,6 @@ dependencies = [
|
|||
name = "undeepend"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"env_logger",
|
||||
"log",
|
||||
"regex",
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
|||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0"
|
||||
log = "0.4"
|
||||
env_logger = "0.11"
|
||||
regex="1.11"
|
||||
|
|
@ -6,24 +6,23 @@ fn read(xml: &str){
|
|||
}
|
||||
|
||||
struct PomReader{
|
||||
|
||||
pom: Pom,
|
||||
}
|
||||
|
||||
impl SaxHandler for PomReader{
|
||||
fn start_document(&mut self) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn end_document(&mut self) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn start_prefix_mapping(&mut self, prefix: &str, uri: &str) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn start_element(&mut self, uri: Option<String>, local_name: &str, qualified_name: &str, attributes: Vec<Attribute>) {
|
||||
todo!()
|
||||
// match local_name{
|
||||
// "modelVersion" => {self.pom=Pom{}}
|
||||
// }
|
||||
}
|
||||
|
||||
fn end_element(&mut self, uri: Option<String>, local_name: &str, qualified_name: &str) {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ pub struct SAXParser<'a> {
|
|||
handler: Box<&'a mut dyn SaxHandler>,
|
||||
position: usize,
|
||||
current: char,
|
||||
char_buffer: Vec<char>,
|
||||
namespace_stack: Vec<(String, isize)>,
|
||||
prefix_mapping: HashMap<String, String>,
|
||||
}
|
||||
|
|
@ -21,6 +22,7 @@ impl<'a> SAXParser<'a> {
|
|||
handler,
|
||||
position: 0,
|
||||
current: '\0',
|
||||
char_buffer: Vec::new(),
|
||||
namespace_stack: Vec::new(),
|
||||
prefix_mapping: HashMap::new(),
|
||||
}
|
||||
|
|
@ -40,6 +42,10 @@ impl<'a> SAXParser<'a> {
|
|||
fn parse_elements(&mut self) -> Result<(), SaxError> {
|
||||
while self.position < self.xml.len() {
|
||||
if self.current == '<' {
|
||||
if !self.char_buffer.is_empty() {
|
||||
self.handler.characters(&self.char_buffer);
|
||||
self.char_buffer.clear();
|
||||
}
|
||||
self.advance()?;
|
||||
if self.current == '!' {
|
||||
self.skip_comment()?;
|
||||
|
|
@ -48,6 +54,9 @@ impl<'a> SAXParser<'a> {
|
|||
} else {
|
||||
self.parse_end_element()?;
|
||||
}
|
||||
} else {
|
||||
self.char_buffer.push(self.current);
|
||||
self.advance()?;
|
||||
}
|
||||
}
|
||||
self.handler.end_document();
|
||||
|
|
|
|||
2
tests/xml/resources/characters.xml
Normal file
2
tests/xml/resources/characters.xml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<element>Hello World</element>
|
||||
|
|
@ -133,6 +133,14 @@ fn test_namespace_prefixes() {
|
|||
assert_eq!(testhandler.mappings["covers"], "http://example.com/covers");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn characters(){
|
||||
let test_xml = include_str!("resources/characters.xml");
|
||||
let mut testhandler = TestHandler::new();
|
||||
parse_string(test_xml.to_string(), Box::new(&mut testhandler))
|
||||
.expect("Failed to parse test xml");
|
||||
assert_eq!(testhandler.characters[0], "Hello World");
|
||||
}
|
||||
#[derive(Debug)]
|
||||
struct TestHandler {
|
||||
start_document_called: usize,
|
||||
|
|
@ -141,6 +149,7 @@ struct TestHandler {
|
|||
end_element_called: usize,
|
||||
elements: Vec<String>,
|
||||
mappings: HashMap<String, String>,
|
||||
characters: Vec<String>,
|
||||
}
|
||||
|
||||
impl TestHandler {
|
||||
|
|
@ -152,6 +161,7 @@ impl TestHandler {
|
|||
end_element_called: 0,
|
||||
elements: vec![],
|
||||
mappings: HashMap::new(),
|
||||
characters: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -192,8 +202,8 @@ impl SaxHandler for TestHandler {
|
|||
self.end_element_called += 1;
|
||||
}
|
||||
|
||||
fn characters(&mut self, _chars: &[char]) {
|
||||
todo!()
|
||||
fn characters(&mut self, chars: &[char]) {
|
||||
self.characters.push(chars.iter().cloned().collect());
|
||||
}
|
||||
|
||||
fn error(&mut self, _error: &str) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue