Made xml schema validator send

This commit is contained in:
2025-04-07 19:27:03 +02:00
parent 2d4a57a706
commit 3826c45240
12 changed files with 59 additions and 48 deletions

View File

@@ -8,6 +8,7 @@ mod python;
mod schema;
mod token;
use std::sync::Arc;
use error::Error;
use token::*;
@@ -26,8 +27,11 @@ impl XmlSchemaValidator {
/// Create a new validator with the given schema text
pub fn new(schema_text: &str) -> Result<Self> {
let schema: schema::XsSchema = quick_xml::de::from_str(schema_text)?;
let current_tokens = schema.xs_element.iter().map(|element| {
Token::StartOfFile(StartOfFile::new(element.clone()))
let schema_arc = Arc::new(schema);
let current_tokens = schema_arc.xs_element.iter().map(|element| {
let element_arc = Arc::new(element.clone());
Token::StartOfFile(StartOfFile::new(element_arc))
}).collect();
Ok(Self {
@@ -46,7 +50,7 @@ impl XmlSchemaValidator {
/// Append a single character to the validator
fn append_char(&mut self, c: char) -> Result<()> {
let mut new_tokens = vec![];
for token in self.current_tokens.clone() {
for token in self.current_tokens.drain(..) {
new_tokens.append(&mut token.append(c));
}
if new_tokens.is_empty() {

View File

@@ -1,6 +1,6 @@
use crate::*;
/// Represents
/// Represents the end of a closing element tag
#[derive(Clone, Debug)]
pub struct ElementCloseEnd {
}
@@ -11,7 +11,7 @@ impl ElementCloseEnd {
}
}
pub fn append(&self, c: char) -> Vec<Token> {
pub fn append(self, c: char) -> Vec<Token> {
if '>' == c {
vec![Token::EndOfFile(EndOfFile::new())]
} else {

View File

@@ -1,3 +1,4 @@
use std::sync::Arc;
use crate::*;
/// Represents a closing element tag that is in the process of being parsed
@@ -5,22 +6,22 @@ use crate::*;
#[derive(Clone, Debug)]
pub struct ElementCloseName {
/// The element that was previously opened and is now being closed
element: schema::XsElement,
element: Arc<schema::XsElement>,
/// Current buffer of characters processed for the element name
buffer: String,
}
impl ElementCloseName {
/// Create a new instance with the given buffer
pub fn new(element: schema::XsElement, buffer: String) -> Self {
pub fn new(element: Arc<schema::XsElement>, buffer: String) -> Self {
Self {
element,
buffer,
}
}
pub fn append(&self, c: char) -> Vec<Token> {
let new_buffer = self.buffer.clone() + &c.to_string();
pub fn append(self, c: char) -> Vec<Token> {
let new_buffer = self.buffer + &c.to_string();
// If still building the element name
if self.element.name.starts_with(&new_buffer) {
@@ -39,7 +40,7 @@ impl ElementCloseName {
} else {
// Still building the element name
vec![Token::ElementCloseName(ElementCloseName::new(
self.element.clone(),
Arc::clone(&self.element),
new_buffer,
))]
}

View File

@@ -1,31 +1,32 @@
use std::sync::Arc;
use crate::*;
/// Represents the start of an XML element closing tag (the '</' sequence)
#[derive(Clone, Debug)]
pub struct ElementCloseStart {
/// The element that was previously opened and is now being closed
element: schema::XsElement,
element: Arc<schema::XsElement>,
has_slash: bool,
}
impl ElementCloseStart {
pub fn new(element: schema::XsElement) -> Self {
pub fn new(element: Arc<schema::XsElement>) -> Self {
Self {
element,
has_slash: false,
}
}
pub fn append(&self, c: char) -> Vec<Token> {
pub fn append(self, c: char) -> Vec<Token> {
if !self.has_slash && '/' == c {
vec![Token::ElementCloseStart(Self {
element: self.element.clone(),
element: Arc::clone(&self.element),
has_slash: true,
})]
} else {
if self.element.name.starts_with(c) {
vec![Token::ElementCloseName(ElementCloseName::new(
self.element.clone(),
Arc::clone(&self.element),
c.to_string(),
))]
} else {

View File

@@ -1,19 +1,20 @@
use std::sync::Arc;
use crate::*;
/// Represents a fully opened XML element, containing the schema element reference
#[derive(Clone, Debug)]
pub struct ElementOpenEnd {
element: schema::XsElement,
element: Arc<schema::XsElement>,
}
impl ElementOpenEnd {
pub fn new(element: schema::XsElement) -> Self {
pub fn new(element: Arc<schema::XsElement>) -> Self {
Self {
element,
}
}
pub fn append(&self, c: char) -> Vec<Token> {
pub fn append(self, c: char) -> Vec<Token> {
// Check if the element allows text content (mixed="true")
let allows_text = self.element.xs_complex_type.mixed
.as_ref()
@@ -23,10 +24,10 @@ impl ElementOpenEnd {
if allows_text {
if c.is_whitespace() {
vec![Token::Whitespace(Whitespace::new(
vec![Token::TextContent(TextContent::new(self.element.clone()))]
vec![Token::TextContent(TextContent::new(Arc::clone(&self.element)))]
))]
} else {
vec![Token::TextContent(TextContent::new(self.element.clone()))]
vec![Token::TextContent(TextContent::new(Arc::clone(&self.element)))]
}
} else {
vec![]

View File

@@ -1,14 +1,15 @@
use std::sync::Arc;
use crate::*;
/// Represents an XML element that is in the process of being opened, containing the text buffer and schema element reference
#[derive(Clone, Debug)]
pub struct ElementOpenName {
buffer: String,
element: schema::XsElement,
element: Arc<schema::XsElement>,
}
impl ElementOpenName {
pub fn new(element: schema::XsElement, buffer: String) -> Result<Self> {
pub fn new(element: Arc<schema::XsElement>, buffer: String) -> Result<Self> {
if element.name.starts_with(buffer.as_str()) {
Ok(Self {
buffer,
@@ -19,19 +20,19 @@ impl ElementOpenName {
}
}
pub fn append(&self, c: char) -> Vec<Token> {
let buffer = self.buffer.clone() + &c.to_string();
pub fn append(self, c: char) -> Vec<Token> {
let buffer = self.buffer + &c.to_string();
if self.element.name.starts_with(buffer.as_str()) {
vec![Token::ElementName(ElementOpenName {
buffer,
element: self.element.clone(),
element: Arc::clone(&self.element),
})]
} else if c.is_whitespace() {
vec![Token::Whitespace(Whitespace::new(
vec![Token::ElementOpen(ElementOpenEnd::new(self.element.clone()))]
vec![Token::ElementOpen(ElementOpenEnd::new(Arc::clone(&self.element)))]
))]
} else if '>' == c {
vec![Token::ElementOpen(ElementOpenEnd::new(self.element.clone()))]
vec![Token::ElementOpen(ElementOpenEnd::new(Arc::clone(&self.element)))]
} else {
vec![]
}

View File

@@ -1,20 +1,21 @@
use std::sync::Arc;
use crate::*;
/// Represents an XML element that is in the process of being opened, containing the text buffer and schema element reference
#[derive(Clone, Debug)]
pub struct ElementOpenStart {
element: schema::XsElement,
element: Arc<schema::XsElement>,
}
impl ElementOpenStart {
pub fn new(element: schema::XsElement) -> Self {
pub fn new(element: Arc<schema::XsElement>) -> Self {
Self {
element,
}
}
pub fn append(&self, c: char) -> Vec<Token> {
let element_name = ElementOpenName::new(self.element.clone(), c.to_string());
pub fn append(self, c: char) -> Vec<Token> {
let element_name = ElementOpenName::new(Arc::clone(&self.element), c.to_string());
if let Ok(element_name) = element_name {
vec![Token::ElementName(element_name)]
} else {

View File

@@ -1,6 +1,6 @@
use crate::*;
/// Represents the final state at the end of an XML file, containing a reference to the root element schema
/// Represents the final state at the end of an XML file
#[derive(Clone, Debug)]
pub struct EndOfFile {
}
@@ -10,7 +10,7 @@ impl EndOfFile {
Self {}
}
pub fn append(&self, _c: char) -> Vec<Token> {
pub fn append(self, _c: char) -> Vec<Token> {
vec![]
}
}

View File

@@ -36,7 +36,7 @@ pub enum Token {
}
impl Token {
pub fn append(&self, c: char) -> Vec<Token> {
pub fn append(self, c: char) -> Vec<Token> {
match self {
Token::ElementCloseEnd(element_close_end) => element_close_end.append(c),
Token::ElementCloseName(element_close_name) => element_close_name.append(c),

View File

@@ -1,19 +1,20 @@
use std::sync::Arc;
use crate::*;
/// Represents the initial state at the start of an XML file, containing a reference to the root element schema
#[derive(Clone, Debug)]
pub struct StartOfFile {
element: schema::XsElement,
element: Arc<schema::XsElement>,
}
impl StartOfFile {
pub fn new(element: schema::XsElement) -> Self {
pub fn new(element: Arc<schema::XsElement>) -> Self {
Self { element }
}
pub fn append(&self, c: char) -> Vec<Token> {
pub fn append(self, c: char) -> Vec<Token> {
if '<' == c {
vec![Token::ElementStart(ElementOpenStart::new(self.element.clone()))]
vec![Token::ElementStart(ElementOpenStart::new(Arc::clone(&self.element)))]
} else {
vec![]
}

View File

@@ -1,14 +1,15 @@
use std::sync::Arc;
use crate::*;
/// Represents the text content within an XML element
#[derive(Clone, Debug)]
pub struct TextContent {
element: schema::XsElement,
element: Arc<schema::XsElement>,
buffer: String,
}
impl TextContent {
pub fn new(element: schema::XsElement) -> Self {
pub fn new(element: Arc<schema::XsElement>) -> Self {
Self {
element,
buffer: String::new(),
@@ -16,20 +17,20 @@ impl TextContent {
}
/// Create a new TextContent with the given initial buffer
pub fn with_buffer(element: schema::XsElement, buffer: String) -> Self {
pub fn with_buffer(element: Arc<schema::XsElement>, buffer: String) -> Self {
Self {
element,
buffer,
}
}
pub fn append(&self, c: char) -> Vec<Token> {
pub fn append(self, c: char) -> Vec<Token> {
if c == '<' {
vec![Token::ElementCloseStart(ElementCloseStart::new(self.element.clone()))]
vec![Token::ElementCloseStart(ElementCloseStart::new(Arc::clone(&self.element)))]
} else {
vec![Token::TextContent(TextContent::with_buffer(
self.element.clone(),
self.buffer.clone() + &c.to_string(),
Arc::clone(&self.element),
self.buffer + &c.to_string(),
))]
}
}

View File

@@ -13,14 +13,14 @@ impl Whitespace {
}
}
pub fn append(&self, c: char) -> Vec<Token> {
pub fn append(self, c: char) -> Vec<Token> {
if c.is_whitespace() {
vec![Token::Whitespace(Self {
next: self.next.clone()
next: self.next
})]
} else {
let mut tokens = vec![];
for token in self.next.iter() {
for token in self.next {
tokens.append(&mut token.append(c))
}
tokens