use std::sync::Arc;
use crate::document::XmlDocument;
use crate::error::{Result, StructuredError};
use crate::schema::types::CompiledSchema;
use super::dom::DomSchemaValidator;
use super::streaming::OnePassSchemaValidator;
pub struct XmlSchemaValidationContext {
schema: Arc<CompiledSchema>,
}
impl XmlSchemaValidationContext {
pub fn new(schema: CompiledSchema) -> Self {
Self {
schema: Arc::new(schema),
}
}
pub fn from_arc(schema: Arc<CompiledSchema>) -> Self {
Self { schema }
}
pub fn validate(&self, doc: &XmlDocument) -> Result<Vec<StructuredError>> {
let validator = DomSchemaValidator::new(Arc::clone(&self.schema));
validator.validate(doc)
}
pub fn create_validator(&self) -> OnePassSchemaValidator {
OnePassSchemaValidator::new(Arc::clone(&self.schema))
}
pub fn schema(&self) -> &CompiledSchema {
&self.schema
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_xml_schema_validation_context_new() {
let schema = CompiledSchema::new();
let ctx = XmlSchemaValidationContext::new(schema);
assert!(ctx.schema().elements.is_empty());
}
#[test]
fn test_xml_schema_validation_context_from_arc() {
let schema = Arc::new(CompiledSchema::new());
let ctx = XmlSchemaValidationContext::from_arc(schema);
assert!(ctx.schema().elements.is_empty());
}
#[test]
fn test_xml_schema_validation_context_create_validator() {
let schema = CompiledSchema::new();
let ctx = XmlSchemaValidationContext::new(schema);
let validator = ctx.create_validator();
assert!(validator.is_valid());
}
#[test]
fn test_xml_schema_validation_context_validate_empty_doc() {
let schema = CompiledSchema::new();
let ctx = XmlSchemaValidationContext::new(schema);
let doc = crate::parse("<root/>").unwrap();
let errors = ctx.validate(&doc).unwrap();
assert!(errors.is_empty());
}
#[test]
fn test_xml_schema_validation_context_validate_with_text() {
let schema = CompiledSchema::new();
let ctx = XmlSchemaValidationContext::new(schema);
let doc = crate::parse("<root>some text content</root>").unwrap();
let errors = ctx.validate(&doc).unwrap();
assert!(errors.is_empty());
}
#[test]
fn test_xml_schema_validation_context_validate_nested() {
let schema = CompiledSchema::new();
let ctx = XmlSchemaValidationContext::new(schema);
let doc = crate::parse("<root><child><grandchild/></child></root>").unwrap();
let errors = ctx.validate(&doc).unwrap();
assert!(errors.is_empty());
}
#[test]
fn test_xml_schema_validation_context_schema() {
use crate::schema::types::ElementDef;
let mut schema = CompiledSchema::new();
schema
.elements
.insert("test".to_string(), ElementDef::new("test"));
let ctx = XmlSchemaValidationContext::new(schema);
assert!(ctx.schema().elements.contains_key("test"));
}
}