pub XmlReader

Signed-off-by: Jia Chao <jiac13@chinaunicom.cn>
This commit is contained in:
Jia Chao 2024-06-13 15:27:15 +08:00
parent fcdab1f40b
commit 284803e2bd

View File

@ -16,7 +16,7 @@ use xml::reader::{EventReader, XmlEvent};
mod test; mod test;
#[allow(dead_code)] #[allow(dead_code)]
struct XmlReader { pub struct XmlReader {
// an iterator for XmlEvent // an iterator for XmlEvent
events: EventReader<BufReader<File>>, events: EventReader<BufReader<File>>,
@ -32,6 +32,10 @@ impl XmlReader {
XmlReader { events, depth: 0 } XmlReader { events, depth: 0 }
} }
pub fn depth(&self) -> usize {
self.depth
}
// pull next stream from xml, set the depth as well. // pull next stream from xml, set the depth as well.
pub fn next(&mut self) -> Result<xml::reader::XmlEvent, xml::reader::Error> { pub fn next(&mut self) -> Result<xml::reader::XmlEvent, xml::reader::Error> {
let event = self.events.next(); let event = self.events.next();
@ -230,7 +234,7 @@ impl CVRF {
loop { loop {
let event = xmlreader.next(); let event = xmlreader.next();
if xmlreader.depth != 2 { if xmlreader.depth() != 2 {
if event == Ok(XmlEvent::EndDocument) { if event == Ok(XmlEvent::EndDocument) {
trace!("End of the xml, break..."); trace!("End of the xml, break...");
break; break;
@ -267,7 +271,7 @@ impl CVRF {
let mut note = Note::new(); let mut note = Note::new();
note.load_from_xmlreader(xmlreader); note.load_from_xmlreader(xmlreader);
if xmlreader.depth < 2 { if xmlreader.depth() < 2 {
break; break;
} }
self.documentnotes.insert(note.title.clone(), note); self.documentnotes.insert(note.title.clone(), note);
@ -279,7 +283,7 @@ impl CVRF {
let mut reference = Reference::new(); let mut reference = Reference::new();
reference.load_from_xmlreader(xmlreader); reference.load_from_xmlreader(xmlreader);
if xmlreader.depth < 2 { if xmlreader.depth() < 2 {
break; break;
} }
self.documentreferences.insert(reference.r#type.clone(), reference); self.documentreferences.insert(reference.r#type.clone(), reference);
@ -421,7 +425,7 @@ impl DocumentTracking {
let mut revision = Revision::new(); let mut revision = Revision::new();
revision.load_from_xmlreader(xmlreader); revision.load_from_xmlreader(xmlreader);
// 所有 revision 读取完毕 // 所有 revision 读取完毕
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
trace!("RevisionHistory read to end."); trace!("RevisionHistory read to end.");
break; break;
} }
@ -620,14 +624,14 @@ impl Reference {
loop { loop {
match xmlreader.next() { match xmlreader.next() {
Ok(XmlEvent::StartElement { attributes, .. }) => { Ok(XmlEvent::StartElement { attributes, .. }) => {
if xmlreader.depth == 3 { if xmlreader.depth() == 3 {
self.r#type = attributes[0].value.clone(); self.r#type = attributes[0].value.clone();
} else { } else {
self.url.push(xmlreader.next_characters()); self.url.push(xmlreader.next_characters());
} }
} }
Ok(XmlEvent::EndElement { .. }) => { Ok(XmlEvent::EndElement { .. }) => {
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
trace!("Reference read end."); trace!("Reference read end.");
break; break;
} }
@ -703,7 +707,7 @@ impl ProductTree {
} }
} }
Ok(XmlEvent::EndElement { .. }) => { Ok(XmlEvent::EndElement { .. }) => {
if xmlreader.depth < 2 { if xmlreader.depth() < 2 {
trace!("ProductTree read end."); trace!("ProductTree read end.");
break; break;
} }
@ -732,7 +736,7 @@ impl ProductTree {
loop { loop {
let mut product = Product::new(); let mut product = Product::new();
product.load_from_xmlreader(xmlreader); product.load_from_xmlreader(xmlreader);
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
break; break;
} }
self.products.push(product); self.products.push(product);
@ -745,7 +749,7 @@ impl ProductTree {
loop { loop {
let mut package = Product::new(); let mut package = Product::new();
package.load_from_xmlreader(xmlreader); package.load_from_xmlreader(xmlreader);
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
break; break;
} }
packages.push(package); packages.push(package);
@ -893,7 +897,7 @@ impl Vulnerability {
let mut note = Note::new(); let mut note = Note::new();
note.load_from_xmlreader(xmlreader); note.load_from_xmlreader(xmlreader);
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
break; break;
} }
self.notes.insert(note.title.clone(), note); self.notes.insert(note.title.clone(), note);
@ -904,7 +908,7 @@ impl Vulnerability {
loop { loop {
let mut status = ProductStatus::new(); let mut status = ProductStatus::new();
status.load_from_xmlreader(xmlreader); status.load_from_xmlreader(xmlreader);
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
break; break;
} }
self.productstatuses.push(status); self.productstatuses.push(status);
@ -915,7 +919,7 @@ impl Vulnerability {
loop { loop {
let mut threat = Threat::new(); let mut threat = Threat::new();
threat.load_from_xmlreader(xmlreader); threat.load_from_xmlreader(xmlreader);
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
break; break;
} }
self.threats.push(threat); self.threats.push(threat);
@ -926,7 +930,7 @@ impl Vulnerability {
loop { loop {
let mut scoreset = ScoreSet::new(); let mut scoreset = ScoreSet::new();
scoreset.load_from_xmlreader(xmlreader); scoreset.load_from_xmlreader(xmlreader);
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
break; break;
} }
self.cvssscoresets.push(scoreset); self.cvssscoresets.push(scoreset);
@ -937,7 +941,7 @@ impl Vulnerability {
loop { loop {
let mut remediation = Remediation::new(); let mut remediation = Remediation::new();
remediation.load_from_xmlreader(xmlreader); remediation.load_from_xmlreader(xmlreader);
if xmlreader.depth < 3 { if xmlreader.depth() < 3 {
break; break;
} }
self.remediations.push(remediation); self.remediations.push(remediation);
@ -972,13 +976,13 @@ impl ProductStatus {
loop { loop {
match xmlreader.next() { match xmlreader.next() {
Ok(XmlEvent::StartElement { attributes, .. }) => { Ok(XmlEvent::StartElement { attributes, .. }) => {
if xmlreader.depth == 4 { if xmlreader.depth() == 4 {
self.status = attributes[0].value.clone(); self.status = attributes[0].value.clone();
} }
self.products.push(xmlreader.next_characters()); self.products.push(xmlreader.next_characters());
} }
Ok(XmlEvent::EndElement { .. }) => { Ok(XmlEvent::EndElement { .. }) => {
if xmlreader.depth < 4 { if xmlreader.depth() < 4 {
break; break;
} }
} }
@ -1018,14 +1022,14 @@ impl Threat {
loop { loop {
match xmlreader.next() { match xmlreader.next() {
Ok(XmlEvent::StartElement { attributes, .. }) => { Ok(XmlEvent::StartElement { attributes, .. }) => {
if xmlreader.depth == 4 { if xmlreader.depth() == 4 {
self.r#type = attributes[0].value.clone(); self.r#type = attributes[0].value.clone();
} else { } else {
self.description = xmlreader.next_characters().parse::<Severity>().unwrap(); self.description = xmlreader.next_characters().parse::<Severity>().unwrap();
} }
} }
Ok(XmlEvent::EndElement { .. }) => { Ok(XmlEvent::EndElement { .. }) => {
if xmlreader.depth < 4 { if xmlreader.depth() < 4 {
break; break;
} }
} }
@ -1166,13 +1170,13 @@ impl Remediation {
loop { loop {
match xmlreader.next() { match xmlreader.next() {
Ok(XmlEvent::StartElement { attributes, .. }) => { Ok(XmlEvent::StartElement { attributes, .. }) => {
if xmlreader.depth == 4 { if xmlreader.depth() == 4 {
self.r#type = attributes[0].value.clone(); self.r#type = attributes[0].value.clone();
} }
break; break;
} }
Ok(XmlEvent::EndElement { .. }) => { Ok(XmlEvent::EndElement { .. }) => {
if xmlreader.depth < 4 { if xmlreader.depth() < 4 {
break; break;
} }
} }