Skip to main content

xrust/validators/dtd/
mod.rs

1mod derive;
2
3use crate::Node;
4use crate::item::NodeType;
5use crate::validators::ValidationError;
6use crate::validators::dtd::derive::{child_deriv, is_nullable};
7
8pub(crate) fn validate_dtd(doc: impl Node) -> Result<(), ValidationError> {
9    match doc.node_type() {
10        NodeType::Document => {
11            match doc.get_dtd() {
12                None => Err(ValidationError::DocumentError(
13                    "No DTD Information on the document".to_string(),
14                )),
15                Some(dtd) => {
16                    match &dtd.name {
17                        None => Err(ValidationError::DocumentError(
18                            "Document name not found in DTD".to_string(),
19                        )),
20                        Some(n) => {
21                            match dtd.patterns.get(n) {
22                                None => Err(ValidationError::DocumentError(
23                                    "Element Declaration not found.".to_string(),
24                                )),
25                                Some(pat) => {
26                                    //println!("pat-{:?}", pat);
27                                    //for pt in &dtd.patterns {
28                                    //    println!("{:?}", pt)
29                                    //}
30                                    match is_nullable(child_deriv(
31                                        pat.clone(),
32                                        doc.child_iter()
33                                            .find(|node| {
34                                                node.node_type() != NodeType::ProcessingInstruction
35                                                    && node.node_type() != NodeType::Comment
36                                                    && !(node.node_type() == NodeType::Text
37                                                        && node.value().to_string() == *"")
38                                            })
39                                            .unwrap(),
40                                        dtd,
41                                    )) {
42                                        true => Ok(()),
43                                        false => {
44                                            Err(ValidationError::SchemaError("Invalid".to_string()))
45                                        }
46                                    }
47                                }
48                            }
49                        }
50                    }
51                }
52            }
53        }
54        _ => Err(ValidationError::DocumentError(
55            "Node provided was not a document".to_string(),
56        )),
57    }
58}