use crate::alloc_prelude::*;
use crate::document::Document;
use crate::error::{Result, XmlError};
use crate::node::{NodeId, NodeKind};
use crate::validator::XmlValidator;
#[cfg(not(feature = "std"))]
use alloc::collections::BTreeMap as HashMap;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContentModel {
Empty,
Any,
Mixed(Vec<String>),
Children(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DtdElementRule {
pub name: String,
pub model: ContentModel,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DtdAttributeRule {
pub element_name: String,
pub attr_name: String,
pub attr_type: String,
pub default_decl: String,
pub default_value: Option<String>,
}
pub type ExternalSubsetResolver = Arc<dyn Fn(&str, Option<&str>) -> Option<String> + Send + Sync>;
#[derive(Clone, Default)]
pub struct DtdValidator {
elements: HashMap<String, DtdElementRule>,
attributes: HashMap<String, Vec<DtdAttributeRule>>,
external_resolver: Option<ExternalSubsetResolver>,
}
impl core::fmt::Debug for DtdValidator {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("DtdValidator")
.field("elements", &self.elements)
.field("attributes", &self.attributes)
.field("has_external_resolver", &self.external_resolver.is_some())
.finish()
}
}
impl DtdValidator {
pub fn new() -> Self {
Self::default()
}
pub fn set_external_resolver<F>(&mut self, resolver: F)
where
F: Fn(&str, Option<&str>) -> Option<String> + Send + Sync + 'static,
{
self.external_resolver = Some(Arc::new(resolver));
}
pub fn parse_subset(&mut self, subset: &str) -> Result<()> {
let lines = subset.lines();
for raw_line in lines {
let line = raw_line.trim();
if line.starts_with("<!ELEMENT") {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 3 {
let elem_name = parts[1].to_string();
let model_str = parts[2..].join(" ");
let model = if model_str.contains("EMPTY") {
ContentModel::Empty
} else if model_str.contains("ANY") {
ContentModel::Any
} else {
ContentModel::Children(model_str)
};
self.elements.insert(
elem_name.clone(),
DtdElementRule {
name: elem_name,
model,
},
);
}
} else if line.starts_with("<!ATTLIST") {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 4 {
let elem_name = parts[1].to_string();
let attr_name = parts[2].to_string();
let attr_type = parts[3].to_string();
let (default_decl, default_value) = if parts.len() >= 5 {
let raw_decl = parts[4..]
.join(" ")
.trim_end_matches('>')
.trim()
.to_string();
let def_val = if raw_decl.contains("#FIXED") {
raw_decl
.split_whitespace()
.nth(1)
.map(|v| v.trim_matches(|c| c == '"' || c == '\'').to_string())
} else if raw_decl.starts_with('"') || raw_decl.starts_with('\'') {
Some(raw_decl.trim_matches(|c| c == '"' || c == '\'').to_string())
} else {
None
};
(raw_decl, def_val)
} else {
("#IMPLIED".to_string(), None)
};
self.attributes
.entry(elem_name.clone())
.or_default()
.push(DtdAttributeRule {
element_name: elem_name,
attr_name,
attr_type,
default_decl,
default_value,
});
}
}
}
Ok(())
}
pub fn apply_defaults(&self, doc: &mut Document) -> Result<usize> {
let mut count = 0;
if let Some(root_id) = doc.root_id() {
let mut stack = vec![root_id];
let mut to_insert = Vec::new();
while let Some(nid) = stack.pop() {
if let Some(node) = doc.get_node(nid) {
if let NodeKind::Element { name, attributes } = &node.kind {
if let Some(rules) = self.attributes.get(&**name) {
for rule in rules {
if let Some(def_val) = &rule.default_value {
if !attributes.iter().any(|a| *a.name == rule.attr_name) {
to_insert.push((
nid,
rule.attr_name.clone(),
def_val.clone(),
));
}
}
}
}
}
stack.extend(node.children.iter().copied());
}
}
for (nid, attr_name, attr_val) in to_insert {
doc.set_attribute(nid, attr_name, attr_val)?;
count += 1;
}
}
Ok(count)
}
pub fn validate(&self, doc: &Document) -> Result<()> {
if let Some(dtd_id) = doc.dtd_id() {
if let Some(node) = doc.get_node(dtd_id) {
if let NodeKind::DocTypeDefinition(dt) = &node.kind {
let mut mut_self = self.clone();
if let Some(sys) = &dt.system_id {
if let Some(resolver) = &self.external_resolver {
if let Some(ext_subset) = resolver(sys, dt.public_id.as_deref()) {
mut_self.parse_subset(&ext_subset)?;
}
}
}
if let Some(subset) = &dt.internal_subset {
mut_self.parse_subset(subset)?;
}
return mut_self.validate_doc(doc);
}
}
}
self.validate_doc(doc)
}
pub const MAX_VALIDATION_DEPTH: usize = 512;
fn validate_doc(&self, doc: &Document) -> Result<()> {
let mut id_set = HashMap::new();
let mut idref_list = Vec::new();
if let Some(root_elem_id) = doc.root_element_id() {
self.collect_ids_and_idrefs(doc, root_elem_id, &mut id_set, &mut idref_list, 0)?;
self.validate_element(doc, root_elem_id, 0)?;
}
for (ref_val, elem_name, attr_name) in idref_list {
if !id_set.contains_key(&ref_val) {
return Err(XmlError::DtdError(format!(
"IDREF '{ref_val}' in attribute '{attr_name}' of <{elem_name}> does not match any declared ID"
)));
}
}
Ok(())
}
fn collect_ids_and_idrefs(
&self,
doc: &Document,
elem_id: NodeId,
id_set: &mut HashMap<String, NodeId>,
idref_list: &mut Vec<(String, String, String)>,
depth: usize,
) -> Result<()> {
if depth > Self::MAX_VALIDATION_DEPTH {
return Err(XmlError::DtdError(
"Document exceeds maximum validation nesting depth (512)".into(),
));
}
if let Some(node) = doc.get_node(elem_id) {
if let NodeKind::Element { name, attributes } = &node.kind {
if let Some(attr_rules) = self.attributes.get(&**name) {
for attr in attributes {
if let Some(rule) = attr_rules.iter().find(|r| r.attr_name == *attr.name) {
if rule.attr_type == "ID" {
let id_val = attr.value.to_string();
if id_set.contains_key(&id_val) {
return Err(XmlError::DtdError(format!(
"Duplicate ID value '{id_val}' in element <{name}>"
)));
}
id_set.insert(id_val, elem_id);
} else if rule.attr_type == "IDREF" {
idref_list.push((
attr.value.to_string(),
name.to_string(),
attr.name.to_string(),
));
} else if rule.attr_type == "IDREFS" {
for single_ref in attr.value.split_whitespace() {
idref_list.push((
single_ref.to_string(),
name.to_string(),
attr.name.to_string(),
));
}
}
}
}
}
}
for &child_id in &node.children {
if let Some(child) = doc.get_node(child_id) {
if matches!(child.kind, NodeKind::Element { .. }) {
self.collect_ids_and_idrefs(doc, child_id, id_set, idref_list, depth + 1)?;
}
}
}
}
Ok(())
}
fn validate_element(&self, doc: &Document, elem_id: NodeId, depth: usize) -> Result<()> {
if depth > Self::MAX_VALIDATION_DEPTH {
return Err(XmlError::DtdError(
"Document exceeds maximum validation nesting depth (512)".into(),
));
}
let node = doc
.get_node(elem_id)
.ok_or_else(|| XmlError::DtdError("Invalid node".into()))?;
if let NodeKind::Element { name, attributes } = &node.kind {
if let Some(rule) = self.elements.get(&**name) {
match &rule.model {
ContentModel::Empty => {
let has_child_elems = node.children.iter().any(|&c_id| {
doc.get_node(c_id).map_or(false, |c| {
matches!(c.kind, NodeKind::Element { .. } | NodeKind::Text(_))
})
});
if has_child_elems {
return Err(XmlError::DtdError(format!(
"Element <{name}> is declared EMPTY but contains child content"
)));
}
}
ContentModel::Children(spec) => {
let child_names: Vec<String> = node
.children
.iter()
.filter_map(|&c_id| {
doc.get_node(c_id).and_then(|c| match &c.kind {
NodeKind::Element { name, .. } => Some(name.to_string()),
_ => None,
})
})
.collect();
if spec.contains(',') && !spec.contains('*') && !spec.contains('?') {
let required_names: Vec<String> = spec
.trim_matches(|c| c == '(' || c == ')' || c == '>' || c == ' ')
.split(',')
.map(|s| s.trim().trim_matches('>').trim().to_string())
.filter(|s| !s.is_empty() && !s.starts_with('#'))
.collect();
for req in &required_names {
if !child_names.contains(req) {
return Err(XmlError::DtdError(format!(
"Element <{name}> does not conform to content specification {spec}: missing required child <{req}>"
)));
}
}
}
}
_ => {}
}
}
if let Some(attr_rules) = self.attributes.get(&**name) {
for rule in attr_rules {
if rule.default_decl.contains("#REQUIRED") {
let present = attributes.iter().any(|a| *a.name == rule.attr_name);
if !present {
return Err(XmlError::DtdError(format!(
"Required attribute '{}' missing on element <{name}>",
rule.attr_name
)));
}
}
}
}
for &child_id in &node.children {
if let Some(child) = doc.get_node(child_id) {
if matches!(child.kind, NodeKind::Element { .. }) {
self.validate_element(doc, child_id, depth + 1)?;
}
}
}
}
Ok(())
}
}
impl XmlValidator for DtdValidator {
fn validate(&self, doc: &Document) -> Result<()> {
self.validate(doc)
}
}