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",
|
"windows-sys",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "anyhow"
|
|
||||||
version = "1.0.98"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "colorchoice"
|
name = "colorchoice"
|
||||||
version = "1.0.4"
|
version = "1.0.4"
|
||||||
|
|
@ -241,7 +235,6 @@ dependencies = [
|
||||||
name = "undeepend"
|
name = "undeepend"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"log",
|
"log",
|
||||||
"regex",
|
"regex",
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0"
|
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
regex="1.11"
|
regex="1.11"
|
||||||
|
|
@ -6,24 +6,23 @@ fn read(xml: &str){
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PomReader{
|
struct PomReader{
|
||||||
|
pom: Pom,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SaxHandler for PomReader{
|
impl SaxHandler for PomReader{
|
||||||
fn start_document(&mut self) {
|
fn start_document(&mut self) {
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end_document(&mut self) {
|
fn end_document(&mut self) {
|
||||||
todo!()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn start_prefix_mapping(&mut self, prefix: &str, uri: &str) {
|
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>) {
|
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) {
|
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>,
|
handler: Box<&'a mut dyn SaxHandler>,
|
||||||
position: usize,
|
position: usize,
|
||||||
current: char,
|
current: char,
|
||||||
|
char_buffer: Vec<char>,
|
||||||
namespace_stack: Vec<(String, isize)>,
|
namespace_stack: Vec<(String, isize)>,
|
||||||
prefix_mapping: HashMap<String, String>,
|
prefix_mapping: HashMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
@ -21,6 +22,7 @@ impl<'a> SAXParser<'a> {
|
||||||
handler,
|
handler,
|
||||||
position: 0,
|
position: 0,
|
||||||
current: '\0',
|
current: '\0',
|
||||||
|
char_buffer: Vec::new(),
|
||||||
namespace_stack: Vec::new(),
|
namespace_stack: Vec::new(),
|
||||||
prefix_mapping: HashMap::new(),
|
prefix_mapping: HashMap::new(),
|
||||||
}
|
}
|
||||||
|
|
@ -40,6 +42,10 @@ impl<'a> SAXParser<'a> {
|
||||||
fn parse_elements(&mut self) -> Result<(), SaxError> {
|
fn parse_elements(&mut self) -> Result<(), SaxError> {
|
||||||
while self.position < self.xml.len() {
|
while self.position < self.xml.len() {
|
||||||
if self.current == '<' {
|
if self.current == '<' {
|
||||||
|
if !self.char_buffer.is_empty() {
|
||||||
|
self.handler.characters(&self.char_buffer);
|
||||||
|
self.char_buffer.clear();
|
||||||
|
}
|
||||||
self.advance()?;
|
self.advance()?;
|
||||||
if self.current == '!' {
|
if self.current == '!' {
|
||||||
self.skip_comment()?;
|
self.skip_comment()?;
|
||||||
|
|
@ -48,6 +54,9 @@ impl<'a> SAXParser<'a> {
|
||||||
} else {
|
} else {
|
||||||
self.parse_end_element()?;
|
self.parse_end_element()?;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
self.char_buffer.push(self.current);
|
||||||
|
self.advance()?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.handler.end_document();
|
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");
|
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)]
|
#[derive(Debug)]
|
||||||
struct TestHandler {
|
struct TestHandler {
|
||||||
start_document_called: usize,
|
start_document_called: usize,
|
||||||
|
|
@ -141,6 +149,7 @@ struct TestHandler {
|
||||||
end_element_called: usize,
|
end_element_called: usize,
|
||||||
elements: Vec<String>,
|
elements: Vec<String>,
|
||||||
mappings: HashMap<String, String>,
|
mappings: HashMap<String, String>,
|
||||||
|
characters: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestHandler {
|
impl TestHandler {
|
||||||
|
|
@ -152,6 +161,7 @@ impl TestHandler {
|
||||||
end_element_called: 0,
|
end_element_called: 0,
|
||||||
elements: vec![],
|
elements: vec![],
|
||||||
mappings: HashMap::new(),
|
mappings: HashMap::new(),
|
||||||
|
characters: vec![],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -192,8 +202,8 @@ impl SaxHandler for TestHandler {
|
||||||
self.end_element_called += 1;
|
self.end_element_called += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn characters(&mut self, _chars: &[char]) {
|
fn characters(&mut self, chars: &[char]) {
|
||||||
todo!()
|
self.characters.push(chars.iter().cloned().collect());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn error(&mut self, _error: &str) {
|
fn error(&mut self, _error: &str) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue