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

View File

@@ -1,6 +1,6 @@
use crate::*; use crate::*;
/// Represents /// Represents the end of a closing element tag
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ElementCloseEnd { 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 { if '>' == c {
vec![Token::EndOfFile(EndOfFile::new())] vec![Token::EndOfFile(EndOfFile::new())]
} else { } else {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
use crate::*; 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)] #[derive(Clone, Debug)]
pub struct EndOfFile { pub struct EndOfFile {
} }
@@ -10,7 +10,7 @@ impl EndOfFile {
Self {} Self {}
} }
pub fn append(&self, _c: char) -> Vec<Token> { pub fn append(self, _c: char) -> Vec<Token> {
vec![] vec![]
} }
} }

View File

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

View File

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

View File

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