#![forbid(unsafe_code)]
#![warn(missing_docs)]
mod document;
mod limits;
pub mod relaxng;
#[cfg(feature = "schematron")]
pub mod schematron;
pub mod store;
#[cfg(feature = "sync")]
pub mod sync;
#[cfg(feature = "schematron")]
mod xpath;
use std::fmt;
use std::path::{Path, PathBuf};
use crate::relaxng::{
DatatypeRegistry, ResolveError, SchemaResolver, SchemaSource, SchemaSyntax, ValidationErrorKind,
};
pub use document::Position;
pub use limits::MAX_DOCUMENT_DEPTH;
pub use relaxng::ElementPath;
pub use store::SchemaStore;
fn on_large_stack<T: Send>(work: impl FnOnce() -> T + Send) -> T {
const STACK: usize = 256 * 1024 * 1024;
let work = std::sync::Mutex::new(Some(work));
let run = || {
(work
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
.expect("runs once"))()
};
std::thread::scope(|scope| {
match std::thread::Builder::new()
.name("teixml".into())
.stack_size(STACK)
.spawn_scoped(scope, run)
{
Ok(thread) => thread
.join()
.unwrap_or_else(|panic| std::panic::resume_unwind(panic)),
Err(_) => run(),
}
})
}
pub struct Schema {
inner: crate::relaxng::Schema,
registry: DatatypeRegistry,
#[cfg(feature = "schematron")]
schematron: EmbeddedSchematron,
}
#[cfg(feature = "schematron")]
struct EmbeddedSchematron {
enabled: bool,
source: Option<(String, String)>,
compact: String,
compiled: std::sync::OnceLock<Result<schematron::Schematron, String>>,
}
impl Schema {
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, Error> {
let path = path.as_ref();
let text = std::fs::read_to_string(path).map_err(|source| Error::Io {
path: path.to_owned(),
source,
})?;
Self::from_text(&text, &path.to_string_lossy(), syntax_for(path))
}
pub fn from_text(text: &str, base_uri: &str, syntax: SchemaSyntax) -> Result<Self, Error> {
let source = SchemaSource::new(text, base_uri, syntax);
let inner = on_large_stack(|| crate::relaxng::Schema::compile(&source, &FileResolver))
.map_err(|error| Error::Schema(error.to_string()))?;
Ok(Self {
inner,
registry: DatatypeRegistry::new(),
#[cfg(feature = "schematron")]
schematron: EmbeddedSchematron {
enabled: true,
source: (syntax == SchemaSyntax::Xml)
.then(|| (text.to_owned(), base_uri.to_owned())),
compact: if syntax == SchemaSyntax::Compact {
text.to_owned()
} else {
String::new()
},
compiled: std::sync::OnceLock::new(),
},
})
}
#[cfg(feature = "schematron")]
pub fn set_schematron(&mut self, enabled: bool) {
self.schematron.enabled = enabled;
}
#[cfg(feature = "schematron")]
pub fn schematron_enabled(&self) -> bool {
self.schematron.enabled
}
#[cfg(feature = "schematron")]
pub fn schematron(&self) -> Result<&schematron::Schematron, Error> {
let compiled = self.schematron.compiled.get_or_init(|| {
on_large_stack(|| {
let (text, uri) = match &self.schematron.source {
Some((text, uri)) => (text.as_str(), uri.as_str()),
None => {
return Ok(schematron::Schematron::from_compact(
&self.schematron.compact,
));
}
};
schematron::Schematron::from_relaxng(text, uri, &|href, base| {
FileResolver
.resolve(href, base)
.map(|source| (source.text().to_owned(), source.base_uri().to_owned()))
.map_err(|error| error.to_string())
})
})
});
compiled
.as_ref()
.map_err(|error| Error::Schema(format!("Schematron rules: {error}")))
}
pub fn validate_str(&self, xml: &str) -> Result<Vec<Diagnostic>, Error> {
self.validate(xml, None)
}
pub fn validate_str_from(
&self,
xml: &str,
path: impl AsRef<Path>,
) -> Result<Vec<Diagnostic>, Error> {
self.validate(xml, Some(path.as_ref()))
}
pub fn validate_file(&self, path: impl AsRef<Path>) -> Result<Vec<Diagnostic>, Error> {
let path = path.as_ref();
let xml = std::fs::read_to_string(path).map_err(|source| Error::Io {
path: path.to_owned(),
source,
})?;
self.validate(&xml, Some(path))
}
fn validate(&self, xml: &str, path: Option<&Path>) -> Result<Vec<Diagnostic>, Error> {
on_large_stack(|| self.validate_here(xml, path))
}
fn validate_here(&self, xml: &str, path: Option<&Path>) -> Result<Vec<Diagnostic>, Error> {
let options = roxmltree::ParsingOptions {
allow_dtd: true,
..Default::default()
};
if limits::xml_nesting_exceeds(xml, MAX_DOCUMENT_DEPTH) {
return Err(Error::Xml(format!(
"elements nested more than {MAX_DOCUMENT_DEPTH} levels deep"
)));
}
let doc = roxmltree::Document::parse_with_options(xml, options)
.map_err(|error| Error::Xml(error.to_string()))?;
let root = document::Node::root(&doc);
let errors = self
.inner
.validate(&self.registry, &root)
.map_err(|error| Error::Schema(error.to_string()))?;
let mut found: Vec<(Option<usize>, ValidationErrorKind, ElementPath)> = errors
.into_iter()
.map(|error| {
(
error.location().copied(),
error.kind().clone(),
error.path().clone(),
)
})
.collect();
let ids = self.inner.id_types();
if !ids.is_empty() {
let (duplicates, unresolved) = document::id_errors(&doc, ids);
let mut merged = Vec::with_capacity(found.len() + duplicates.len() + unresolved.len());
let mut found_iter = found.into_iter().peekable();
let mut duplicates = duplicates.into_iter().peekable();
while let Some((offset, ..)) = duplicates.peek() {
let offset = *offset;
while found_iter
.peek()
.is_some_and(|(at, ..)| at.is_none_or(|at| at <= offset))
{
merged.extend(found_iter.next());
}
merged.extend(
duplicates
.next()
.map(|(at, kind, path)| (Some(at), kind, path)),
);
merged.extend(
duplicates
.next()
.map(|(at, kind, path)| (Some(at), kind, path)),
);
}
merged.extend(found_iter);
merged.extend(
unresolved
.into_iter()
.map(|(at, kind, path)| (Some(at), kind, path)),
);
found = merged;
}
let lines = std::cell::OnceCell::new();
let diagnostics: Vec<Diagnostic> = found
.into_iter()
.map(|(offset, kind, path)| Diagnostic {
position: offset.map(|offset| {
lines
.get_or_init(|| document::LineIndex::new(xml))
.position(offset)
}),
kind: DiagnosticKind::RelaxNg(kind),
path,
})
.collect();
#[cfg(feature = "schematron")]
let diagnostics = self.check_schematron(&doc, xml, &lines, path, diagnostics)?;
#[cfg(not(feature = "schematron"))]
let _ = path;
Ok(diagnostics)
}
#[cfg(feature = "schematron")]
fn check_schematron<'x>(
&self,
doc: &roxmltree::Document<'_>,
xml: &'x str,
lines: &std::cell::OnceCell<document::LineIndex<'x>>,
path: Option<&Path>,
mut diagnostics: Vec<Diagnostic>,
) -> Result<Vec<Diagnostic>, Error> {
if !self.schematron.enabled {
return Ok(diagnostics);
}
let rules = self.schematron()?;
if rules.is_empty() {
return Ok(diagnostics);
}
let uri = path.map(|path| {
let path = std::path::absolute(path).unwrap_or_else(|_| path.to_owned());
format!("file:{}", path.to_string_lossy().replace('\\', "/"))
});
for (node, message) in rules.check(doc, uri) {
let lines = lines.get_or_init(|| document::LineIndex::new(xml));
diagnostics.push(Diagnostic::schematron(lines, node, message));
}
Ok(diagnostics)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diagnostic {
pub position: Option<Position>,
pub kind: DiagnosticKind,
pub path: ElementPath,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DiagnosticKind {
RelaxNg(ValidationErrorKind),
#[cfg(feature = "schematron")]
Schematron(schematron::Message),
}
impl fmt::Display for DiagnosticKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::RelaxNg(kind) => write!(f, "{kind}"),
#[cfg(feature = "schematron")]
Self::Schematron(message) => write!(f, "{message}"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Severity {
Error,
Warning,
Info,
}
impl fmt::Display for Severity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Error => "error",
Self::Warning => "warning",
Self::Info => "info",
})
}
}
impl Diagnostic {
pub fn severity(&self) -> Severity {
match &self.kind {
DiagnosticKind::RelaxNg(_) => Severity::Error,
#[cfg(feature = "schematron")]
DiagnosticKind::Schematron(message) => message.severity,
}
}
pub fn is_error(&self) -> bool {
self.severity() == Severity::Error
}
#[cfg(feature = "schematron")]
fn schematron(
lines: &document::LineIndex<'_>,
node: xpath::XNode<'_>,
message: schematron::Message,
) -> Self {
let path = node.element().map(document::path).unwrap_or_default();
Self {
position: Some(lines.position(node.source_offset())),
kind: DiagnosticKind::Schematron(message),
path,
}
}
}
impl Diagnostic {
pub fn message(&self) -> impl fmt::Display + '_ {
Message(self)
}
}
struct Message<'d>(&'d Diagnostic);
impl fmt::Display for Message<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let diagnostic = self.0;
write!(f, "{}", diagnostic.kind)?;
if let DiagnosticKind::RelaxNg(ValidationErrorKind::UnexpectedElement(name)) =
&diagnostic.kind
&& diagnostic.path.is_empty()
&& name.namespace.as_deref().unwrap_or_default().is_empty()
{
f.write_str(" (in no namespace)")?;
}
Ok(())
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(position) = self.position {
write!(f, "{position}: ")?;
}
write!(f, "{}", self.message())?;
if !self.path.is_empty() {
write!(f, " (in {})", self.path)?;
}
Ok(())
}
}
#[derive(Debug)]
pub enum Error {
Io {
path: PathBuf,
source: std::io::Error,
},
Schema(String),
Xml(String),
Store(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
Self::Schema(message) => write!(f, "schema error: {message}"),
Self::Xml(message) => write!(f, "XML error: {message}"),
Self::Store(message) => write!(f, "{message}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io { source, .. } => Some(source),
_ => None,
}
}
}
fn syntax_for(path: &Path) -> SchemaSyntax {
match path.extension().and_then(|ext| ext.to_str()) {
Some("rnc") => SchemaSyntax::Compact,
_ => SchemaSyntax::Xml,
}
}
struct FileResolver;
impl SchemaResolver for FileResolver {
fn resolve(&self, href: &str, base_uri: &str) -> Result<SchemaSource, ResolveError> {
if href.contains("://") {
return Err(ResolveError::new(format!(
"refusing to fetch `{href}`: only local files are supported"
)));
}
let path = Path::new(base_uri)
.parent()
.unwrap_or_else(|| Path::new(""))
.join(href);
let text = std::fs::read_to_string(&path)
.map_err(|error| ResolveError::new(format!("{}: {error}", path.display())))?;
Ok(SchemaSource::new(
text,
path.to_string_lossy(),
syntax_for(&path),
))
}
}
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;